ggplot图例 - 更改标签,顺序和标题

62

我非常困难地修改我的图例。这里是一个可重现的示例:

dtt <- structure(list(model = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("ma", "mb", "mc"), class = "factor"), year = c(2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L), V = c(0.16, 0.14, 0.11, 0.13, 0.15, 0.16, 0.24, 0.17, 0.12, 0.13, 0.15, 0.15, 0.2, 0.16, 0.11, 0.12, 0.12, 0.15), lower = c(0.11, 0.11, 0.07, 0.09, 0.11, 0.12, 0.16, 0.12, 0.04, 0.09, 0.09, 0.11, 0.14, 0.1, 0.07, 0.08, 0.05, 0.1), upper = c(0.21, 0.19, 0.17, 0.17, 0.19, 0.2, 0.29, 0.23, 0.16, 0.17, 0.16, 0.2, 0.26, 0.27, 0.15, 0.16, 0.15, 0.19)), .Names = c("model", "year", "V", "lower", "upper"), class = "data.frame", row.names = c(NA, -18L))

我的图表是这样生成的:

ggplot(dtt, aes(x=year, y=V, group = model, colour = model, ymin = lower, ymax = upper)) +
    geom_ribbon(alpha = 0.35, linetype=0)+ 
    geom_line(aes(linetype=model), size = 1.5) +       
    geom_point(aes(shape=model), fill = "white", size = 4)  +      
    theme(legend.position=c(.6,0.8)) +
    theme(legend.background = element_rect(colour = 'black', fill = 'grey90', size = 1, linetype='solid'))

这将生成以下内容: enter image description here

现在,我想要做的是:

  1. 更改图例的标题
  2. 更改图例项出现的顺序
  3. 更改图例项的文本。

我已经尝试了很多方法,但没有取得太大的成功。到目前为止,最好的结果是添加了以下内容:

scale_colour_hue(name = "Model 1",
    breaks=c("mb", "ma", "mc"),
    labels=c("MBB", "MAA", "MCC"))

但它生成了这样一个丑陋的东西: enter image description here

你会发现,现在多了一个不必要的图例,并且图例中的形状与绘图中的不匹配!

最后,我想让图例中的图形指示蓝色和绿色线条是虚线,而不是实线 - 但我完全不知道如何做到这一点。

任何帮助都将不胜感激,

1个回答

86

您需要执行两个步骤:

  1. 在绘图之前重新命名和重新排序因子水平
  2. 将每个图例的标题重命名为相同的标题

代码:

dtt$model <- factor(dtt$model, levels=c("mb", "ma", "mc"), labels=c("MBB", "MAA", "MCC"))

library(ggplot2)
ggplot(dtt, aes(x=year, y=V, group = model, colour = model, ymin = lower, ymax = upper)) +
  geom_ribbon(alpha = 0.35, linetype=0)+ 
  geom_line(aes(linetype=model), size = 1) +       
  geom_point(aes(shape=model), size=4)  +      
  theme(legend.position=c(.6,0.8)) +
  theme(legend.background = element_rect(colour = 'black', fill = 'grey90', size = 1, linetype='solid')) +
  scale_linetype_discrete("Model 1") +
  scale_shape_discrete("Model 1") +
  scale_colour_discrete("Model 1")

在此输入图片描述

然而,我认为这种方式十分丑陋且难以解释。更好的方法是使用facets:

ggplot(dtt, aes(x=year, y=V, group = model, colour = model, ymin = lower, ymax = upper)) +
  geom_ribbon(alpha=0.2, colour=NA)+ 
  geom_line() +       
  geom_point()  +      
  facet_wrap(~model)

在此输入图像描述


1
谢谢。 这非常有帮助 +1。 我也会考虑您对我的图形丑陋的评论。 但我之所以这样做是因为实际上我有超过6年的数据,因此图形将更加压缩。 我一开始确实尝试了类似于您的建议的方法,但我的老师说很难确定置信区间重叠的程度 - 这也是这样做的部分原因。 - Joe King
+1(@ROLO你的答案有一种不同的因子水平重排方式,是一个很好的替代方案--我希望你能保留它!) - smillig
2
小心 - 因子本身没有标签,labels参数用于重命名因子水平。 - Andrie
3
好的,这是重新排序因子的另一种方法:“dtt $ model <- relevel(dtt $ model, ref =“ MBB” )”。这将把“ref”所表示的因子放在最前面。 - ROLO
你如何在这里动态地确定 lowerupper?我认为学习价值是在 dtt 中实验定义的。我在这里开了一个新线程 https://stackoverflow.com/q/44167480/54964 - Léo Léopold Hertz 준영

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接