首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在r中使用ggtext来显示轴数据标签?

如何在r中使用ggtext来显示轴数据标签?
EN

Stack Overflow用户
提问于 2021-01-10 11:10:27
回答 2查看 411关注 0票数 1

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

代码(没有ggtext)

代码语言:javascript
复制
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 添加到x-axis数据标签中,则会得到错误

代码语言:javascript
复制
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")
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-01-10 11:31:26

问题是,您尝试使用长度为105的向量glue长度为3的color向量和Cases_type列。相反,让color成为一个命名的向量,并使用color[Cases_type],以确保您分配正确的颜色。此外,您的glue语句中缺少一个引号。

代码语言:javascript
复制
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")

票数 0
EN

Stack Overflow用户

发布于 2021-01-10 11:29:31

我认为你需要改变

代码语言:javascript
复制
"<i style='color:{color}>{Cases_type}</i>'"

代码语言:javascript
复制
"<i style='color:{color}'>{Cases_type}</i>"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65652600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档