如何很好地注释ggplot2(手动)

And*_*eas 42 r ggplot2

使用ggplot2我通常使用geom_text和类似的东西position=jitter注释我的情节.

但是 - 对于一个不错的情节,我经常发现值得手动注释.如下:

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")
Run Code Online (Sandbox Code Playgroud)

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

mba*_*ask 64

我遇到了类似的问题并用JD Long回答解决了这个问题.但是,作为ggplot2更新到0.9.0版本的结果,我注意到所有geom_text()调用在图上都有些模糊.

感谢kohske,我发现了这段代码

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")) 
Run Code Online (Sandbox Code Playgroud)

绘制geom_text nrow(data2)时间!

向geom_text提供数据的正确方法是构建一个不同的data.frame,用于保存要绘制的字符串的坐标,标签和颜色:

data2.labels <- data.frame(
  time = c(7, 15), 
  value = c(.9, .6), 
  label = c("correct color", "another correct color!"), 
  type = c("NA*", "MVH")
  )

ggplot(data2, aes(x=time, y=value, group=type, col=type))+
  geom_line()+
  geom_point()+
  theme_bw() +
  geom_text(data = data2.labels, aes(x = time, y = value, label = label))
Run Code Online (Sandbox Code Playgroud)

  • 这是一个更好的答案,特别是对于大型数据集.geom_text重复可以真正杀死PDF,使得渲染速度非常慢. (8认同)
  • 陷入同样的​​问题,这解决了它!使用geom_text()而不使用自己的数据映射将导致文本的过度绘图和较差的分辨率.提供数据映射将解决问题. (2认同)
  • 那是正确的@ shil88.自ggplot 2.0.0([发布](https://github.com/tidyverse/ggplot2/releases/tag/v2.0.0)2015/12/18):"`check_overlap = TRUE`提供了一种避免过度绘图的简单方法标签:否则将重叠的标签被省略" (2认同)

JD *_*ong 48

如果使用geom_text()而不是annotate(),则可以将组颜色传递给绘图:

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")) 
Run Code Online (Sandbox Code Playgroud)

所以使用annotate()它看起来像这样: alt text http://www.cerebralmastication.com/wp-content/uploads/2010/03/before.png

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

  • 你注意到了右边的传说吗?通常它应该是一个圆而不是线上的'a'.这看起来不太好. (6认同)
  • 你可以通过在geom_text中添加"show_guide = F"来摆脱'a'.(mbask的答案通过复制防止了丑陋的渲染,应该使用它.还有show_guide选项应该设置为F.) (2认同)