ggplot传说 - 更改标签,订单和标题 [英] ggplot legends - change labels, order and title

查看:192
本文介绍了ggplot传说 - 更改标签,订单和标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力修改我的情节中的传奇。这是一个可重复的例子:

  dtt < -  structure(list(model = structure(c(1L,1L,1L, 1L,1L,1L,2L,2L,2L,2L,2L,2L,3L,3L,3L,3L,3L,3L),标签= c(ma,mb =因子),年= c(2005L,2006L,2007L,2008L,2009L,2010L,2005L,2006L,2007L,2008L,2009L,2010L,2005L,2006L,2007L,2008L,2009L,2010L) (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),下式= 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 (model,year,V,lower,upper),class =0,0.17,0.16,0.2,0.26,0.27,0.15,0.16,0.15,0.19) data.frame,row.names = c(NA,-18L))

像这样:

  ggplot(dtt,aes(x = year,y = V,group = model,color = model,ymin =较低,ymax =较高))+ 
geom_ribbon(alpha = 0.35,linetype = 0)+
geom_line(aes(linetype = model),size = 1.5)+
geom_point(aes(shape = model),fill =white 4)+
主题(legend.position = c(.6,0.8))+
主题(legend.background = element_rect(color ='black',fill ='grey90',size = 1, linetype ='solid'))

产生这个结果:

现在,我想要做的是 p>


  1. 更改图例标题

  2. 更改图例项目显示的顺序

  3. 更改图例项目的文本。

没有太大的成功。迄今为止我已经管理的最好的是添加这个:

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

但它产生了这种可憎的感觉:
>

最后,我想在图例中显示蓝色和绿色线条是虚线的,而不是固定的 - 但我不知道如何去做。



任何帮助都将得到高度赞赏, 您需要做两件事事情:


  1. 重命名并重新排列因素等级之前

  2. 重命名相同标题的每个图例

代码:

 <$ C $ (mB),ma,mc),标签= c(MBB,MAA,MCC))$ b(dtt $ model, 
$ b库(ggplot2)
ggplot(dtt,aes(x = year,y = V,group = model,color = 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(color ='black',fill ='grey90',size = 1,linetype ='solid') )+
scale_linetype_discrete(Model 1)+
scale_shape_discrete(Model 1)+
scale_colour_discrete(Model 1)
pre>



然而,我觉得这很难看,也很难解释。使用构面要好得多:

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


I'm struggling a great deal to modify the legend in my plot. Here is a reproducible example:

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

My plot is generated like this:

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

which produces this:

Now, what I would like to do is

  1. change the title of the legend
  2. change the order in which the legend items appear
  3. change the text of the legend items.

I have fiddled around for hours trying to do this, but without much success. The best I have managed so far is to add this:

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

But it produces this abomination:

As you see, there is now an extra unneeded legend, and the shapes in the legend do not match those in the plot !

Finally, I would like to graphics in the legend to indicate that the blue and green lines are dashed, not solid - but I have no idea at all how to do that.

Any assistance would be highly appreciated,

解决方案

You need to do two things:

  1. Rename and re-order the factor levels before the plot
  2. Rename the title of each legend to the same title

The code:

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

However, I think this is really ugly as well as difficult to interpret. It's far better to use 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)

这篇关于ggplot传说 - 更改标签,订单和标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆