如何使用 ggtext 为 r 中的轴数据标签着色?

Posted

技术标签:

【中文标题】如何使用 ggtext 为 r 中的轴数据标签着色?【英文标题】:how to use ggtext to color axis data labels in r? 【发布时间】:2021-04-15 13:17:48 【问题描述】:

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

代码(不带 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")

现在,如果我添加 ggtext 来为 x-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")

【问题讨论】:

【参考方案1】:

我认为你需要改变

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

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

【讨论】:

感谢@Stéphane Laurent,这是我犯的一个愚蠢的错误,但还有更多问题,如下所述。【参考方案2】:

问题是您尝试使用长度为 105 的向量的 Cases_type 列来 glue 长度为 3 的 color 向量。而是将 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")

【讨论】:

感谢@stefan 帮助我。我不确定如何根据案例类型分配颜色。谢谢你告诉我正确的方法!

以上是关于如何使用 ggtext 为 r 中的轴数据标签着色?的主要内容,如果未能解决你的问题,请参考以下文章

使用 ggtext 在标签中的两个单词之间添加空格

R:绘图轨迹图的轴标签中的下标

重新排序相关矩阵图的轴标签

在 R plotly 子图上获取单独的轴标签

如何解决重叠的轴刻度标签

如何使用 R 为多路复用网络中的边缘着色