在ggplot2中添加构面标题并更改图例标题 [英] Adding facet titles and changing legend title in ggplot2

查看:1426
本文介绍了在ggplot2中添加构面标题并更改图例标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在ggplot2中添加构面标题

  ggplot(diamonds,aes(cut))+ geom_bar()+ facet_grid(颜色〜清晰度)

并更改图例标题

  ggplot(diamonds,aes(cut,fill = cut))+ geom_bar()+ facet_grid(。〜clarity)

感谢您的帮助。

解决方案

使用所使用因子的等级进行标记。因此,如果您只是简单地改变关卡,比如

 关卡(钻石$透明度)<  -  letters [1:8] 

现在将使用这些字母标记这些方面。图例标题与该美学贴图的标签相匹配,您可以通过设置:

  + labs(fill =填充图例标签)

作为一个额外的珍闻,我注意到我可以设置 x y 轴标签在 labs 中为NULL,但不包含图例标题;对于那些使用空字符的用户,如果您不想使用标题。



编辑

根据您的说明,您可以使用 grid.text 添加绘图区域以外的文本:

  print(qplot(1,1),vp = viewport(width = 0.9))
grid.text(unit(0.95,npc),0.5,label =Right label,rot = 270)


I wonder how to add facet titles in ggplot2

ggplot(diamonds, aes(cut)) + geom_bar() +   facet_grid(color ~ clarity)

and change the legend title

ggplot(diamonds, aes(cut, fill=cut)) + geom_bar() +   facet_grid(. ~ clarity)

Thanks for your help.

解决方案

The facets are labeled using the levels of the factor used. So if you simply change the levels , such as

levels(diamonds$clarity) <- letters[1:8]

those facets will now be labeled using those letters. The legend title matches the label for that aesthetic mapping, which you can set via:

+ labs(fill = "Fill legend label")

As an extra tidbit, I've noticed that I can set the x and y axis labels to NULL in labs but not the legend titles; for those you use an empty character if you want no title.

Edit

Given your clarifications, you can add text outside the plotting area using grid.text:

print(qplot(1,1),vp = viewport(width = 0.9))
grid.text(unit(0.95,"npc"),0.5,label = "Right label", rot = 270)

这篇关于在ggplot2中添加构面标题并更改图例标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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