0

我正在尝试按条件为 x 轴文本标签着色并使用ggtext相同但出现错误。

健康)状况: If State == India then color otherwise black color

库和数据:

library(tidyverse)
library(lubridate)
library(scales)
library(ggtext)
library(wesanderson) # for color palette & can skip this as I have commented scale_color_manual()

file_url1 <- url("https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/df_vaccination.csv")

df_vaccination <- read.csv(url(file_url1))

df_vaccination <- df_vaccination %>%
  mutate(Updated.On = as.Date(Updated.On))

代码:

df_vaccination %>% 
  filter(
         Updated.On > ymd("2021-02-28"),
         Updated.On < ymd("2021-06-01")
         ) %>% 
  
  mutate(month_abbr = month(Updated.On, label = TRUE, abbr = TRUE)

         # adding color to India State label, otherwise keep black

         ,State = ifelse(State == "India",
                        "<strong><span style='color:#ff8c00'></span></strong>",
                        "<span style='color:black'></span>")
         ) %>% 
  
  group_by(month_abbr, State) %>% 
  
  summarise(monthly_ind_vaccinated = sum(Total.Individuals.Vaccinated_Dailycalc, 
                                      na.rm = TRUE),
            Population = first(Population), .groups = "drop") %>% 
  group_by(State) %>% 
  mutate(prc_vaccinated_per_pop = monthly_ind_vaccinated / Population,
         state_max = max(prc_vaccinated_per_pop),
         state_min = min(prc_vaccinated_per_pop)) %>% 
  na.omit() %>%
  ungroup() %>% 
  mutate(State = fct_reorder(State, state_max, max, .desc = TRUE) # , State = fct_relevel(State, "Dadra and Nagar Haveli and Daman and Diu")
         ) %>% 
  
  ggplot(aes(x = State, y = prc_vaccinated_per_pop, col = as.factor(month_abbr))) +
  geom_segment(aes(xend = State, y = state_min, yend = state_max), 
               col = "darkgrey", show.legend = FALSE) +
  geom_point(aes(size = month_abbr), alpha = 0.75) +
  geom_rect(aes(xmin = which(levels(as.factor(State)) == "India") - 0.4 , 
                xmax = which(levels(as.factor(State)) == "India") + 0.4, 
                ymin = 0.0001, ymax = 0.2), col = "midnightblue", 
            fill = NA, linetype = 2, size = .6) +
  
  scale_y_continuous(labels = percent) +
  scale_color_manual(values = wes_palette("Darjeeling1", type = "discrete")) +
  scale_size_manual(values = c(2,4,2)) +
  guides(size = FALSE) +
  
  theme(axis.text.x = element_markdown(angle = 90, vjust = -.001),
        # ,colour = ifelse(df_vaccination$State == "India", "red","black")
        strip.text = element_blank(),
        panel.grid.major = element_blank(),
        legend.position = c(0.33,.99),
        axis.line = element_line(),
        plot.caption = element_text(hjust=c(1, 0))
        ) +
  
  labs(title = "States with % Vaccination by months",
       caption = c("created by ViSa", 
       "For Most of States Apr % Vaccination was higher 
       than May, specially for good size States"),
       color = "% Vaccinated each month",
       x = "", y = "")

也试过: ,State = ifelse(State == "India", glue( "<strong><span style='color:#ff8c00'>{State}</span></strong>"), glue("<span style='color:black'>{State}</span>"))

但仍然出现错误。

输出:(不使用ggtext, span, element_markdown()

在此处输入图像描述

4

1 回答 1

0

错误是由于geom_rectwhere 条件仍然适用于"India"where asIndia不再可用但已更改为<strong><span style='color:#ff8c00'>India</span></strong>

通过编辑下面的部分代码工作:

geom_rect(aes(xmin = which(levels(as.factor(State)) == "<strong><span style='color:#ff8c00'>India</span></strong>") - 0.4 ,
                xmax = which(levels(as.factor(State)) == "<strong><span style='color:#ff8c00'>India</span></strong>") + 0.4,
                ymin = 0.0001, ymax = 0.2), col = "midnightblue",
            fill = NA, linetype = 2, size = .6) 

在此处输入图像描述

于 2021-06-11T14:24:33.010 回答