【问题标题】:Renaming X-axis variables to separate current month data and previous month data重命名 X 轴变量以分隔当前月份数据和上个月数据
【发布时间】:2020-12-02 16:05:04
【问题描述】:

大家好,我有这样的医院数据;


hospitals <- c('Johnd Hospital','Johnd Hospital','Jolie Hope Hospital','Jolie Hope Hospital','Hope Hospital','Hope Hospital')
variables <- c('temperature', 'temperature', 'pulse rate','pulse rate', 'resp rate', 'resp rate')
score <- c(92,82,63,78, 23, 59)
months <- c('october', 'september', 'october', 'september', 'october', 'september')

datat <- data.frame(hospitals, variables, score, months)

datat$colour <- ifelse(datat$score >=90, "seagreen3", 
                           ifelse(datat$score > 80 & datat$score <= 89, "gold1",
                                  ifelse(datat$score > 60 & datat$score <= 79, "plum2", "red3")))
#performance scores
datat$perfomance <- ifelse(as.numeric(datat$score) >=90, "Excellent Perfomance (>=90)%", 
                               ifelse(as.numeric(datat$score) >= 80 & as.numeric(datat$score) <= 89, "Good Perfomance (80-89)%",
                                      ifelse(as.numeric(datat$score) > 60 & as.numeric(datat$score) <= 79, "Some Perfomance (60-79)%", "Poor Perfomance (<60)%")))

nam = c("Good Perfomance (80-89)%", "Some Perfomance (60-79)%", "Poor Perfomance (<60)%", "Excellent Perfomance (>=90)%")
grays = c("gold1", "plum2", "red3", "seagreen3")

my_color <- setNames(grays, nam)
my_color

现在这个数据我想绘制当月和上月的变量性能。到目前为止,我能够可视化当前月份和上个月的可变分数的闪避条形图。一个例子是一个变量,比如温度,这个月和上个月我有两个条形图。 这是我的可视化方式

myplott <- function(datat, hospital) {
  
  print(paste0("Plot for hospital: ", hospital))
  
  p <- ggplot(datat%>% filter(hospitals == hospital), 
              aes(x = variables, y = as.numeric(score))) +
    #facet_grid(cols = vars(Group), scales = "free", space = "free") +
    geom_bar(position = "dodge2", stat = "identity") +
    theme_bw() +
    ylab("Percentage %") +
    scale_y_continuous(breaks = seq(-10, 100, by = 10)) +
    ggtitle(hospital) +
    labs(caption = "Please note; -5 (red paint below 0) indicates 0% documentation for that indicator") +
    ### 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 = 0.5) +
    geom_hline(yintercept = 80, linetype = "dashed", color = "black", size = 0.5) +
    geom_hline(yintercept = 60, linetype = "dashed", color = "black", size = 0.5) +
    geom_hline(yintercept = 0, linetype = "dashed", color = "black", size = 0.5) +
    #geom_text(aes(label = value), vjust = 0, color = "black", size = 2.8) +
    theme(plot.title = element_text(face = "bold", hjust = 0.5, size = 20), legend.position = "top") +
    #theme(plot.subtitle.title = element_text('Admission and during admission vitals monitored')) +
    theme(legend.title = element_blank())
  
  return(p)
  
}
myplott(comb.data, "Jolie Hope Hospital")

我现在的难题是如何命名 X 轴上的条形图,以便人们可以知道上个月和当前月的条形图,例如将 temperature 命名为 temperature(october) 和 温度(11 月)。最终结果是在 X 轴上重命名当前月份和上个月的变量分数。如果有人知道我如何命名 X 轴上的条形,请提供帮助。谢谢

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    一种方法是使用interaction()

    library(ggplot2)
    #Function
    myplott <- function(datat, hospital) {
      
      print(paste0("Plot for hospital: ", hospital))
      
      p <- ggplot(datat%>% filter(hospitals == hospital), 
                  aes(x = interaction(variables,months), y = as.numeric(score))) +
        #facet_grid(cols = vars(Group), scales = "free", space = "free") +
        geom_bar(position = "dodge2", stat = "identity") +
        theme_bw() +
        ylab("Percentage %") +
        scale_y_continuous(breaks = seq(-10, 100, by = 10)) +
        ggtitle(hospital) +
        labs(caption = "Please note; -5 (red paint below 0) indicates 0% documentation for that indicator") +
        ### use manual color here
        scale_fill_manual(values = my_color) +
        #geom_text(aes(label=months),position = position_dodge2(0.9),vjust=-0.5)+
        theme(axis.text.x = element_text(size = 13, hjust = 1, angle = 45)) +
        geom_hline(yintercept = 90, linetype = "dashed", color = "black", size = 0.5) +
        geom_hline(yintercept = 80, linetype = "dashed", color = "black", size = 0.5) +
        geom_hline(yintercept = 60, linetype = "dashed", color = "black", size = 0.5) +
        geom_hline(yintercept = 0, linetype = "dashed", color = "black", size = 0.5) +
        theme(plot.title = element_text(face = "bold", hjust = 0.5, size = 20), legend.position = "top") +
        #theme(plot.subtitle.title = element_text('Admission and during admission vitals monitored')) +
        theme(legend.title = element_blank())+
        xlab('')
      
      return(p)
      
    }
    #Apply
    myplott(datat, "Jolie Hope Hospital")
    

    输出:

    或者从我的角度来看,最优雅:

    #Function 2
    myplott <- function(datat, hospital) {
      
      print(paste0("Plot for hospital: ", hospital))
      
      p <- ggplot(datat%>% filter(hospitals == hospital), 
                  aes(x = variables, y = as.numeric(score))) +
        #facet_grid(cols = vars(Group), scales = "free", space = "free") +
        geom_bar(position = "dodge2", stat = "identity") +
        theme_bw() +
        ylab("Percentage %") +
        scale_y_continuous(breaks = seq(-10, 100, by = 10)) +
        ggtitle(hospital) +
        labs(caption = "Please note; -5 (red paint below 0) indicates 0% documentation for that indicator") +
        ### use manual color here
        scale_fill_manual(values = my_color) +
        geom_text(aes(label=months),position = position_dodge2(0.9),vjust=-0.5)+
        theme(axis.text.x = element_text(size = 13, hjust = 1, angle = 45)) +
        geom_hline(yintercept = 90, linetype = "dashed", color = "black", size = 0.5) +
        geom_hline(yintercept = 80, linetype = "dashed", color = "black", size = 0.5) +
        geom_hline(yintercept = 60, linetype = "dashed", color = "black", size = 0.5) +
        geom_hline(yintercept = 0, linetype = "dashed", color = "black", size = 0.5) +
        theme(plot.title = element_text(face = "bold", hjust = 0.5, size = 20), legend.position = "top") +
        #theme(plot.subtitle.title = element_text('Admission and during admission vitals monitored')) +
        theme(legend.title = element_blank())+
        xlab('')
      
      return(p)
      
    }
    #Apply
    myplott(datat, "Jolie Hope Hospital")
    

    输出:

    更新:

    #Function 3
    myplott <- function(datat, hospital) {
      
      print(paste0("Plot for hospital: ", hospital))
      
      p <- ggplot(datat%>% filter(hospitals == hospital), 
                  aes(x = variables, y = as.numeric(score),fill=months)) +
        #facet_grid(cols = vars(Group), scales = "free", space = "free") +
        geom_bar(position = "dodge2", stat = "identity") +
        theme_bw() +
        ylab("Percentage %") +
        scale_y_continuous(breaks = seq(-10, 100, by = 10)) +
        ggtitle(hospital) +
        labs(caption = "Please note; -5 (red paint below 0) indicates 0% documentation for that indicator") +
        ### use manual color here
        #scale_fill_manual(values = my_color) +
        # geom_text(aes(label=months),position = position_dodge2(0.9),vjust=-0.5)+
        theme(axis.text.x = element_text(size = 13, hjust = 1, angle = 45)) +
        geom_hline(yintercept = 90, linetype = "dashed", color = "black", size = 0.5) +
        geom_hline(yintercept = 80, linetype = "dashed", color = "black", size = 0.5) +
        geom_hline(yintercept = 60, linetype = "dashed", color = "black", size = 0.5) +
        geom_hline(yintercept = 0, linetype = "dashed", color = "black", size = 0.5) +
        theme(plot.title = element_text(face = "bold", hjust = 0.5, size = 20), legend.position = "top") +
        #theme(plot.subtitle.title = element_text('Admission and during admission vitals monitored')) +
        theme(legend.title = element_blank())+
        xlab('')
      
      return(p)
      
    }
    #Apply
    myplott(datat, "Jolie Hope Hospital")
    

    输出:

    【讨论】:

    • 谢谢你...我很想使用 interaction 但我注意到它按字母顺序排列名称,是否有可能旁边有两个条形的变量为了比较?
    • @LivingstoneM 我可以修改代码以保留栏但按月填充?你同意吗?
    • @LivingstoneM 我已经添加了一个更新,如果您觉得满意,请告诉我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 2020-11-04
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多