[英]Legends labels don't match with assigned colour ggplot2
我有醫院數據,我試圖將每個變量的性能可視化。 我已分配的顏色一樣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 優秀性能。 我試過分解標簽名稱,但沒有很好地幫助。 我的最終結果應該是每個圖例顏色都由確切的標簽名稱表示。
使用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 包(v0.3.0) 於 2020 年 11 月 17 日創建
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.