简体   繁体   English

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

[英]Problems with legend display using geom_pointrange in ggplot2

I am using ggplot2 with geom_pointrange in R to plot the sensitivity (95% CI) of five groups under six scenarios (in total, 30 plots of sensitivity).我正在使用 ggplot2 和 geom_pointrange 在 R 到 plot 在六种情况下五组的灵敏度(95%CI)(灵敏度总图,30) The six scenarios are derived from a 3x2 set of scenarios.这六个场景源自一组 3x2 的场景。 I can plot the graph successfully, but I am having problems with the legend.我可以成功地 plot 图表,但我遇到了图例问题。 I have used three different colours and solid and dashed lines to represent the six scenarios.我使用了三种不同的颜色以及实线和虚线来表示这六个场景。 However, I am unable to display the dashed lines on the legend (only the three colours, each of which repeats twice).但是,我无法在图例上显示虚线(只有三种颜色,每种颜色重复两次)。 Initially I had used six different colours, but colleagues have asked me to change this.最初我使用了六种不同的颜色,但同事要求我改变它。

I have used geom_pointrange within ggplot2, but have had problems getting the legend to display properly:我在 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) 
  )

I wish to see a legend with six distinct labels (3 different colours x 2 different linetypes).我希望看到一个带有六个不同标签(3 种不同颜色 x 2 种不同线型)的图例。

You can remove the points in the legend to make the lines more visible and increase the size of the legend.key:您可以删除图例中的点以使线条更明显并增加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) 
  )

在此处输入图像描述

A different approach might be to change the shape of the points:一种不同的方法可能是改变点的形状:

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

在此处输入图像描述

This can also be combined with the different line types, but this might get to messy...这也可以与不同的线型结合使用,但这可能会变得混乱......

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

相关问题 如何使用ggplot2为geom_pointrange()类型图形获取图例键中的垂直线 - How to get vertical lines in legend key using ggplot2 for geom_pointrange() type graphic R,GGPlot2和geom_pointrange - R, GGPlot2 & geom_pointrange 如何在 ggplot2 中自动使用 geom_pointrange() 从线型图例和线型中去除形状? - How the get rid off shape from linetype legend and linetype from legend using geom_pointrange() in ggplot2 automatically? 删除ggplot2 geom_pointrange中的添加剂透明度 - Remove additive transparency in ggplot2 geom_pointrange 在ggplot中处理geom_pointrange中的因子 - Dealing with factors in geom_pointrange in ggplot ggplot2:geom_pointrange()facet_grid()与coord_flip()和自由秤 - ggplot2: geom_pointrange() facet_grid() with coord_flip() and free scales 如何通过颜色、形状和线型控制 ggplot2::geom_pointrange 元素的顺序 - How to control the order of ggplot2::geom_pointrange elements by colour, shape and linetype scale_color_manual 无法与 geom_pointrange ggplot2 一起正常工作 - scale_color_manual not working properly with geom_pointrange ggplot2 如何将 geom_pointrange() 和 ggplot2 美学添加到 plot_cme? - How to add geom_pointrange() and ggplot2 aesthetics to plot_cme? ggplot2 geom_pointrange中具有重要性注释的facet的不同颜色值 - Different colored values by facet in ggplot2 geom_pointrange with significance annotation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM