3

I have hospital data which I am trying to visualize perfomance of each variable. I have assigned colours like seagreen3 to represent >90 excellent perfomance, gold1 colour to represent 80-89 better perfomance , plum2 to represent 60-79 good perfomance and red to represent <60 poor perfomance. I have given ranges for this too.Here is my full code


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")

Now, my challenge is the legends are not showing exact label for the colours assigned especially if one colour is missing . I want legend colour to be represented by exact label, like if seagreen3 is available on the plot then the label should be >90 excellent perfomance. I have tried factoring label names but not working kindly assist. My end result should each legend colour be represent by exact label name.

LivingstoneM
  • 1,088
  • 10
  • 28
  • You want bars to change color as well ? – Daman deep Nov 18 '20 at 04:26
  • @Daman deep Not really, I have manually created the colours for the bars based on perfomance ranges I want, see the *if statement* in the code. What I only want is the label name of legend colours to represent the exact colour as per the perfomance ranges – LivingstoneM Nov 18 '20 at 04:30

1 Answers1

3

Use setNames() to match the colors with the names then you should be good

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")))

Important: match names and colors

# 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"

Plot function:

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)
  
}

Create a list to loop through

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).

Created on 2020-11-17 by the reprex package (v0.3.0)

Tung
  • 26,371
  • 7
  • 91
  • 115
  • Btw you can save that plot list to files using `iwalk()` https://stackoverflow.com/a/52902877/786542 & https://stackoverflow.com/a/50437356/786542 – Tung Nov 18 '20 at 04:55