Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ggplot2中使用geom_pointrange显示图例时出现问题_R_Ggplot2_Legend - Fatal编程技术网

在ggplot2中使用geom_pointrange显示图例时出现问题

在ggplot2中使用geom_pointrange显示图例时出现问题,r,ggplot2,legend,R,Ggplot2,Legend,我在R中使用带有geom_pointrange的ggplot2绘制六种场景下五组的灵敏度(95%CI)(总共30个灵敏度图)。这六个场景源自一组3x2的场景。我可以成功地绘制图表,但我对图例有问题。我使用了三种不同的颜色以及实线和虚线来表示这六种场景。但是,我无法在图例上显示虚线(只有三种颜色,每种颜色重复两次)。起初我使用了六种不同的颜色,但同事们要求我改变这一点 我在ggplot2中使用了geom_pointrange,但在正确显示图例时遇到问题: sens <- cbind.data

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

sens您可以删除图例中的点,以使线条更可见并增加图例的大小。键:

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

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