繁体   English   中英

在 ggplot2 中使用 geom_pointrange 显示图例的问题

[英]Problems with legend display using geom_pointrange in ggplot2

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

我在 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 种不同线型)的图例。

您可以删除图例中的点以使线条更明显并增加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) 
  )

在此处输入图像描述

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM