【问题标题】:Problems with legend display using geom_pointrange in ggplot2在 ggplot2 中使用 geom_pointrange 显示图例的问题
【发布时间】:2020-01-20 11:06:26
【问题描述】:

我在 R 中使用带有 geom_pointrange 的 ggplot2 来绘制六种情况下五组的灵敏度 (95% CI)(总共 30 个灵敏度图)。这六个场景源自一组 3x2 的场景。我可以成功绘制图表,但我遇到了图例问题。我使用了三种不同的颜色以及实线和虚线来表示这六个场景。但是,我无法在图例上显示虚线(只有三种颜色,每种颜色重复两次)。最初我使用了六种不同的颜色,但同事要求我改变它。

我在 ggplot2 中使用了 geom_pointrange,但是在正确显示图例时遇到了问题:

sens <- cbind.data.frame(
  group = rep(c("Grp1", "Grp2", "Grp3", "Grp4", "Grp5"), 6),
  mean  = c(0.70, 0.70, 0.65, 0.60, 0.72, 0.85, 0.84, 0.77, 0.68, 0.77,
            0.71, 0.71, 0.69, 0.65, 0.76, 0.90, 0.88, 0.82, 0.82, 0.87,
            0.78, 0.78, 0.79, 0.73, 0.83, 0.92, 0.92, 0.92, 0.90, 0.93) * 100, 
  lower = c(0.64, 0.64, 0.59, 0.55, 0.68, 0.73, 0.73, 0.65, 0.55, 0.68,
            0.66, 0.66, 0.63, 0.59, 0.72, 0.80, 0.78, 0.70, 0.70, 0.79,
            0.73, 0.72, 0.74, 0.68, 0.80, 0.84, 0.84, 0.82, 0.79, 0.87) * 100,
  upper = c(0.75, 0.75, 0.70, 0.66, 0.76, 0.92, 0.91, 0.87, 0.80, 0.84,
            0.77, 0.76, 0.74, 0.70, 0.79, 0.95, 0.94, 0.90, 0.90, 0.92,
            0.82, 0.82, 0.83, 0.79, 0.86, 0.97, 0.97, 0.97, 0.96, 0.97) * 100,
  type = rep(c("A1&B1", "A1&B2", "A2&B1", "A2&B2", "A3&B1", "A3&B2"), each=5),
  type2 = rep(c(rep("B1", 5), rep("B2", 5)), 3),
  type3 = rep(c(rep("A1", 10), rep("A2", 10), rep("A3", 10))))


ggplot(sens, aes(group, mean, colour = type, group = type, linetype = type)) +
  geom_pointrange(aes(ymin = lower, ymax = upper), position = position_dodge(width = 0.5), size=1.25) + 
  coord_flip() +  
  xlab("Group") + 
  ylab("Sensitivity (95% CI)") +
  scale_color_manual(values = c("red", "red", "dark grey", "dark grey", "blue", "blue")) + 
  scale_linetype_manual(values=c(1,2,1,2,1,2)) +
  theme(axis.text.x=element_text(size=15, vjust=0.5, color = 'black'),  # x-axis labels
        axis.text.y=element_text(size=15, vjust=0.5, color = 'black'),  # y-axis labels
        axis.title.x=element_text(size=17.5, vjust=0.1),                # x-title justification  
        axis.title.y=element_text(size=17.5, vjust=1.5),                 # y-title justification
        legend.title=element_blank(),
        legend.position= "bottom",       
        legend.text = element_text(size = 14.5) 
  )

我希望看到一个带有六个不同标签(3 种不同颜色 x 2 种不同线型)的图例。

【问题讨论】:

    标签: r ggplot2 legend


    【解决方案1】:

    可以去掉图例中的点,让线条更明显,增加legend.key的大小:

    ggplot(sens, aes(group, mean, colour = type, group = type, linetype = type)) +
      geom_pointrange(aes(ymin = lower, ymax = upper), position = position_dodge(width = 0.5), size=1.25) + 
      coord_flip() +  
      xlab("Group") + 
      ylab("Sensitivity (95% CI)") +
      scale_color_manual(values = c("red", "red", "dark grey", "dark grey", "blue", "blue"), 
                         ########### CHANGE HERE ###############
                         guide = guide_legend(override.aes = list(shape = NA))) + 
      scale_linetype_manual(values=c(1,2,1,2,1,2)) +
      theme(axis.text.x=element_text(size=15, vjust=0.5, color = 'black'),  # x-axis labels
            axis.text.y=element_text(size=15, vjust=0.5, color = 'black'),  # y-axis labels
            axis.title.x=element_text(size=17.5, vjust=0.1),                # x-title justification  
            axis.title.y=element_text(size=17.5, vjust=1.5),                 # y-title justification
            legend.title=element_blank(),
            legend.key.size = unit(2.5, "lines"), ########### CHANGE HERE ###############
            legend.position= "bottom",       
            legend.text = element_text(size = 14.5) 
      )
    

    另一种方法可能是改变点的形状:

    ggplot(sens, aes(group, mean, colour = type, group = type, shape = type)) +
      geom_pointrange(aes(ymin = lower, ymax = upper), position = position_dodge(width = 0.5), size=1.25) + 
      coord_flip() +  
      xlab("Group") + 
      ylab("Sensitivity (95% CI)") +
      scale_color_manual(values = c("red", "red", "dark grey", "dark grey", "blue", "blue")) + 
      scale_shape_manual(values = c(16,17,16,17, 16,17)) +
      theme(axis.text.x=element_text(size=15, vjust=0.5, color = 'black'),  # x-axis labels
            axis.text.y=element_text(size=15, vjust=0.5, color = 'black'),  # y-axis labels
            axis.title.x=element_text(size=17.5, vjust=0.1),                # x-title justification  
            axis.title.y=element_text(size=17.5, vjust=1.5),                 # y-title justification
            legend.title=element_blank(),
            legend.position= "bottom",       
            legend.text = element_text(size = 14.5) 
      )
    

    这也可以与不同的线型组合,但这可能会变得混乱......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-23
      • 1970-01-01
      • 2023-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多