我有一些医院的数据,想要将每个变量的表现可视化。我已经分配了颜色,例如seagreen3表示>90优秀的表现,gold1表示80-89较好的表现,plum2表示60-79良好的表现,red表示<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")
现在,我的挑战是图例没有显示分配的颜色的确切标签,特别是如果缺少一种颜色。我希望图例颜色可以由确切的标签表示,例如,如果海绿色3在图表上可用,则标签应为>90优秀性能。我尝试过对标签名称进行分组,但不起作用,请协助解决。我的最终结果应该是每个图例颜色都由确切的标签名称表示。