【问题标题】:Legends labels don't match with assigned colour ggplot2图例标签与分配的颜色 ggplot2 不匹配
【发布时间】:2020-11-19 01:51:08
【问题描述】:

我有医院数据,我试图将每个变量的性能可视化。我指定了像 seagreen3 这样的颜色来代表 >90 出色的性能gold1 颜色代表 80-89 更好的性能 , plum2 表示 60-79 的良好性能红色 表示 60 的差性能。我也为此给出了范围。这是我的完整代码


variables <- c("adm_t","mother_alive","time_b", "adm_t","mother_alive","time_b","adm_t","mother_alive","time_b")
hosp_id <- c('Jotr hosp','jotr hosp','jotr hosp','baggie hosp', 'baggie  hosp', 'baggie hosp','nogi  hosp', 'nogi hosp','nogi hosp' )
document <- c('nar','par','free_text', 'nar','par','free_text','nar','par','free_text')
value <- c(21, 69, 80, 95,87,67, 25, NA, 67)

df <- data.frame(variables,hosp_id, document, value)

df$colour <- ifelse(as.numeric(df$value) >=90, "seagreen3", 
                         ifelse(as.numeric(df$value) >= 80 & as.numeric(df$value) <= 89, "gold1",
                                ifelse(as.numeric(df$value) > 60 & as.numeric(df$value) <= 79, "plum2", "red3")))


#test
df$perfomance <- ifelse(as.numeric(df$value) >=90, ">90 Excellent Perfomance", 
                             ifelse(as.numeric(df$value) >= 80 & as.numeric(df$value) <= 89, "80-89 Better perfomance",
                                    ifelse(as.numeric(df$value) > 60 & as.numeric(df$value) <= 79, "60-79 Good perfomance", "<60 Poor Perfomance")))




# Create a named character vector that relates factor levels to colors.
nam = c("80-89 Better perfomance", "60-79 Good perfomance", "<60 Poor Perfomance", ">90 Excellent Perfomance")
per <- factor(nam, levels = c(">90 Excellent Perfomance", "80-89 Better perfomance", "60-79 Good perfomance","<60 Poor Perfomance"))

grays = c("seagreen3", "gold1", "plum2", "red3")

myplott <- function(df, hospital) {
  ggplot(df %>% filter(hosp_id==hospital), aes(x=variables, y=as.numeric(value),fill=colour,group="variables")) +
    geom_bar(position="stack", stat="identity") +
    #scale_fill_viridis(discrete = T, option = "plasma")+ 
    theme_bw() +
    ylab ("Percentage %") +
    scale_y_continuous(breaks = seq(-10, 100, by = 10)) +
    ggtitle(hospital)+
    scale_fill_identity(guide = 'legend',labels = per)+
    #scale_colour_manual(labels = nam,values=grays) +
    theme(axis.text.x = element_text(size = 13, hjust= 1, angle = 45)) +
    geom_hline(yintercept= 90, linetype="dashed", color = "black", size= 1) + 
    geom_hline(yintercept= 80, linetype="dashed", color = "black", size= 1) +
    geom_hline(yintercept= 60, linetype="dashed", color = "black", size= 1) +
    facet_grid(cols = vars(document), scales = "free", space = "free") +
    geom_text(aes(label= value), vjust=1.6, color="black", size=2.8)+
    theme(plot.title = element_text(face = "bold",  hjust = 0.5, size = 20),legend.position = "top")+
    theme(legend.title=element_blank()) 
}

myplott(df, "baggie hosp")

现在,我的挑战是图例没有显示分配颜色的确切标签,尤其是在缺少一种颜色的情况下。我希望用精确的标签来表示图例颜色,例如如果 seagreen3 在图中可用,那么标签应该是 >90 出色的性能。我尝试过考虑标签名称,但没有提供帮助。我的最终结果应该是每个图例颜色都由确切的标签名称表示。

【问题讨论】:

  • 你想让条形图也改变颜色吗?
  • @Daman deep 不是真的,我已经根据我想要的性能范围手动创建了条形的颜色,请参阅代码中的 if 语句。我只想要图例颜色的标签名称,以根据性能范围表示确切的颜色

标签: r ggplot2


【解决方案1】:

使用setNames()将颜色与名称匹配,那么你应该很好

library(tidyverse)

variables <- c("adm_t","mother_alive","time_b", "adm_t","mother_alive","time_b","adm_t","mother_alive","time_b")
hosp_id <- c('Jotr hosp','jotr hosp','jotr hosp','baggie hosp', 'baggie  hosp', 'baggie hosp','nogi  hosp', 'nogi hosp','nogi hosp' )
document <- c('nar','par','free_text', 'nar','par','free_text','nar','par','free_text')
value <- c(21, 69, 80, 95,87,67, 25, NA, 67)

df <- data.frame(variables, hosp_id, document, value)

df$colour <- ifelse(as.numeric(df$value) >=90, "seagreen3", 
                    ifelse(as.numeric(df$value) >= 80 & as.numeric(df$value) <= 89, "gold1",
                           ifelse(as.numeric(df$value) > 60 & as.numeric(df$value) <= 79, "plum2", "red3")))

#test
df$perfomance <- ifelse(as.numeric(df$value) >=90, ">90 Excellent Perfomance", 
                        ifelse(as.numeric(df$value) >= 80 & as.numeric(df$value) <= 89, "80-89 Better perfomance",
                               ifelse(as.numeric(df$value) > 60 & as.numeric(df$value) <= 79, "60-79 Good perfomance", "<60 Poor Perfomance")))

重要:匹配名称和颜色

# Create a named character vector that relates factor levels to colors.
nam = c("80-89 Better perfomance", "60-79 Good perfomance", "<60 Poor Perfomance", ">90 Excellent Perfomance")
grays = c("gold1", "plum2", "red3", "seagreen3")

my_color <- setNames(grays, nam)
my_color
#>  80-89 Better perfomance    60-79 Good perfomance      <60 Poor Perfomance 
#>                  "gold1"                  "plum2"                   "red3" 
#> >90 Excellent Perfomance 
#>              "seagreen3"

绘图函数:

myplott <- function(df, hospital) {
  
  print(paste0("Plot for hospital: ", hospital))
  
  p <- ggplot(df %>% filter(hosp_id == hospital), 
         aes(x = variables, y = as.numeric(value), 
             fill = perfomance, 
             group = "variables")) +
    facet_grid(cols = vars(document), scales = "free", space = "free") +
    geom_bar(position = "stack", stat = "identity") +
    theme_bw() +
    ylab("Percentage %") +
    scale_y_continuous(breaks = seq(-10, 100, by = 10)) +
    ggtitle(hospital) +

    ### use manual color here
    scale_fill_manual(values = my_color) +

    theme(axis.text.x = element_text(size = 13, hjust = 1, angle = 45)) +
    geom_hline(yintercept = 90, linetype = "dashed", color = "black", size = 1) +
    geom_hline(yintercept = 80, linetype = "dashed", color = "black", size = 1) +
    geom_hline(yintercept = 60, linetype = "dashed", color = "black", size = 1) +
    geom_text(aes(label = value), vjust = 1.6, color = "black", size = 2.8) +
    theme(plot.title = element_text(face = "bold", hjust = 0.5, size = 20), legend.position = "top") +
    theme(legend.title = element_blank())
  
  return(p)
  
}

创建一个循环列表

hospital_list <- df %>% 
  distinct(hosp_id) %>% 
  pull()

performance_plot_list <- hospital_list %>% 
  map(~ myplott(df, .x))
#> [1] "Plot for hospital: Jotr hosp"
#> [1] "Plot for hospital: jotr hosp"
#> [1] "Plot for hospital: baggie hosp"
#> [1] "Plot for hospital: baggie  hosp"
#> [1] "Plot for hospital: nogi  hosp"
#> [1] "Plot for hospital: nogi hosp"

performance_plot_list[[1]]

performance_plot_list[[3]]

performance_plot_list[[6]]
#> Warning: Removed 1 rows containing missing values (position_stack).
#> Warning: Removed 1 rows containing missing values (geom_text).

reprex package (v0.3.0) 于 2020 年 11 月 17 日创建

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-09
  • 2021-08-26
相关资源
最近更新 更多