0

我遇到了ggtexthttps://wilkelab.org/ggtext/到使用颜色轴数据标签 的包,element_markdown()并尝试为我的轴标签着色但失败了。

代码(没有ggtext)

library(tidyverse)

read.csv("https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/df_stack_limited.csv") %>% 
  
  ggplot(aes(x = Cases_type, y = Country.Region)) +
  geom_point(shape = 21, aes(size = Cases_size, color = Cases_type), fill="#f8f2e4", stroke=3) +
  # theme_minimal() +
  theme_wsj() +
  theme(axis.text.x = element_text(angle = 90),
        axis.text = element_text(size = 8),
        legend.position = "none",
        plot.title = element_text(face = "bold", family = "serif", size = 20),
        plot.subtitle = element_text(face = "plain", family = "serif", size = 12),
        plot.caption = element_text(face = "plain", family = "serif", size = 9)) +

  scale_color_tableau(palette = "Tableau 10") +
  # scale_color_wsj(palette = "colors6") +
  
  facet_wrap(~Date) +
  labs(title = "Top 5 Countries for all type of Daily cases",
       subtitle = "For Latest 7 days separated by each date",
       caption = "created by ViSa (SciArt!!)") +
  coord_cartesian(clip = "off")

在此处输入图像描述

现在,如果我添加ggtextx-axis数据标签着色,那么我会收到错误

library(tidyverse)
library(ggtext)

color = c("#2596be", "#f28e2b", "#e15759")

read.csv("https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/df_stack_limited.csv") %>% 
  
  mutate(Cases_type = glue("<i style='color:{color}>{Cases_type}</i>'")) %>% 
  
  ggplot(aes(x = Cases_type, y = Country.Region)) +
  geom_point(shape = 21, aes(size = Cases_size, color = Cases_type), fill="#f8f2e4", stroke=3) +
  # theme_minimal() +
  theme_wsj() +
  theme(axis.text.x = element_text(angle = 90),
        axis.text = element_text(size = 8),
        legend.position = "none",
        plot.title = element_text(face = "bold", family = "serif", size = 20),
        plot.subtitle = element_text(face = "plain", family = "serif", size = 12),
        plot.caption = element_text(face = "plain", family = "serif", size = 9)) +
  
  theme(axis.text.x = element_markdown()) +
  scale_color_tableau(palette = "Tableau 10") +
  # scale_color_wsj(palette = "colors6") +
  
  facet_wrap(~Date) +
  labs(title = "Top 5 Countries for all type of Daily cases",
       subtitle = "For Latest 7 days separated by each date",
       caption = "created by ViSa (SciArt!!)") +
  coord_cartesian(clip = "off")
4

2 回答 2

0

我认为你需要改变

"<i style='color:{color}>{Cases_type}</i>'"

"<i style='color:{color}'>{Cases_type}</i>"
于 2021-01-10T11:29:31.133 回答
0

问题是您尝试使用glue长度为 105color的列的长度为 3Cases_type的向量。而是创建color一个命名向量并使用color[Cases_type]它确保您分配正确的颜色。glue此外,您的陈述中缺少引用。

library(tidyverse)
library(ggtext)
library(glue)
library(ggthemes)

color = c(Daily_cases = "#2596be", Daily_deaths = "#f28e2b", Daily_recovered = "#e15759")

d <- read.csv("https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/df_stack_limited.csv") %>% 
  mutate(Cases_type = glue("<i style='color: {color[Cases_type]}'>{Cases_type}</i>'")) 

d %>% 
  ggplot(aes(x = Cases_type, y = Country.Region)) +
  geom_point(shape = 21, aes(size = Cases_size, color = Cases_type), fill="#f8f2e4", stroke=3) +
  #theme_minimal() +
  theme_wsj() +
  theme(axis.text.x = element_text(angle = 90),
        axis.text = element_text(size = 8),
        legend.position = "none",
        plot.title = element_text(face = "bold", family = "serif", size = 20),
        plot.subtitle = element_text(face = "plain", family = "serif", size = 12),
        plot.caption = element_text(face = "plain", family = "serif", size = 9)) +
  theme(axis.text.x = element_markdown()) +
  scale_color_tableau(palette = "Tableau 10") +
  # scale_color_wsj(palette = "colors6") +
  facet_wrap(~Date) +
  labs(title = "Top 5 Countries for all type of Daily cases",
       subtitle = "For Latest 7 days separated by each date",
       caption = "created by ViSa (SciArt!!)") +
  coord_cartesian(clip = "off")

于 2021-01-10T11:31:26.897 回答