如何很好地注释ggplot2(手动) [英] How to nicely annotate a ggplot2 (manual)

查看:154
本文介绍了如何很好地注释ggplot2(手动)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 ggplot2 我通常使用 geom_text 以及类似于 position = jitter

但是 - 对于一个好的情节,我经常发现值得手动注释。如下所示:

  data2<  - 结构(列表(类型=结构(c(5L,1L,2L,4L,3L ,5L,1L,
2L,4L,3L,5L,1L,2L,4L,3L,5L,1L,2L,4L,3L)。标签= c(EDS,
KIU,LAK,MVH,NA *),class =factor),值= c(0.9,
0.01,0.01,0.09,0,0.8,0.05,0,0.15, 0,0.41,0.04,0.03,
0.52,0,0.23,0.111,0.02,0.64,0.01),时间= c(3L,3L,3L,
3L,3L,6L,6L,6L ,6L,6L,15L,15L,15L,15L,15L,27L,27L,
27L,27L,27L),year = c(2008L,2008L,2008L,2008L,2008L,2007L,
2007L,2007L,2007L,2007L,2007L,2007L,2007L,2007L,2007L,
2006L,2006L,2006L,2006L,2006L)),.Names = c(type,value,$ b $ (1L,3L,4L,5L,6L,7L,9L,10L,
11L,12L,13L,15L,16L,17L,18L, (a)(x =时间,y =值,组=类型,列=类型))+ $ b $($)
ggplot(data2,aes b geom_line()+
geom_point()+
theme_bw()+
annotate(text,x = 6,y = 0.9,label =this i一个错误的颜色)+
注释(text,x = 15,y = 0.6,label =这是带有错误颜色的第二个注释)

问题是,我无法使文本注释颜色与线条颜色匹配。我想我可以用手动缩放来解决这个问题,但我希望有更好的方法吗?

解决方案

如果你使用geom_text )而不是annotate(),你可以传递一个组的颜色到你的情节:

  ggplot(data2,aes(x = time, ()+ 
geom_point()+
theme_bw()+
geom_text(aes(7,.9 ,label =correct color,color =NA *))+
geom_text(aes(15,.6,label =another correct color!,color =MVH))

因此使用annotate()它看起来像这样:
盐文本http://www.cerebralmastication.com/wp-content/uploads/2010/03/before.png

然后在使用geom_text()后,它看起来像这样:
盐文字http://www.cerebralmastication.com/wp-content/uploads/2010/03/after.png

Using ggplot2 I normally use geom_text and something like position=jitter to annotate my plots.

However - for a nice plot I often finds it worthwhile to annotate manually. like below:

data2 <- structure(list(type = structure(c(5L, 1L, 2L, 4L, 3L, 5L, 1L, 
2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L), .Label = c("EDS", 
"KIU", "LAK", "MVH", "NA*"), class = "factor"), value = c(0.9, 
0.01, 0.01, 0.09, 0, 0.8, 0.05, 0, 0.15, 0, 0.41, 0.04, 0.03, 
0.52, 0, 0.23, 0.11, 0.02, 0.64, 0.01), time = c(3L, 3L, 3L, 
3L, 3L, 6L, 6L, 6L, 6L, 6L, 15L, 15L, 15L, 15L, 15L, 27L, 27L, 
27L, 27L, 27L), year = c(2008L, 2008L, 2008L, 2008L, 2008L, 2007L, 
2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 
2006L, 2006L, 2006L, 2006L, 2006L)), .Names = c("type", "value", 
"time", "year"), row.names = c(1L, 3L, 4L, 5L, 6L, 7L, 9L, 10L, 
11L, 12L, 13L, 15L, 16L, 17L, 18L, 19L, 21L, 22L, 23L, 24L), class = "data.frame")
ggplot(data2, aes(x=time, y=value, group=type, col=type))+
geom_line()+
geom_point()+
theme_bw()+
annotate("text", x=6, y=0.9, label="this is a wrong color")+
annotate("text", x=15, y=0.6, label="this is a second annotation with a wrong color")

The problem is, that I can't get the text annotations color to match the line color. I assume I could fix this with a manual scale, but I hope there is a better way?

解决方案

If you use geom_text() instead of annotate() you can pass a group color to your plot:

ggplot(data2, aes(x=time, y=value, group=type, col=type))+
geom_line()+
geom_point()+
theme_bw() +
geom_text(aes(7, .9, label="correct color", color="NA*")) +
geom_text(aes(15, .6, label="another correct color!", color="MVH")) 

So using annotate() it looks like this: alt text http://www.cerebralmastication.com/wp-content/uploads/2010/03/before.png

then after using geom_text() it looks like this: alt text http://www.cerebralmastication.com/wp-content/uploads/2010/03/after.png

这篇关于如何很好地注释ggplot2(手动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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