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

发布于 2024-08-24 01:09:06 字数 1216 浏览 2 评论 0原文

使用ggplot2我通常使用geom_textposition=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")

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

闻呓 2024-08-31 01:09:06

我有一个类似的问题并通过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")) 

绘制了 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))

I had a similar problem and solved it with JD Long answer. But as a results of ggplot2 updating to version 0.9.0 I noticed that all geom_text()calls rendered somewhat blurred on the plots.

Thanks to kohske I discovered that this code

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

plots the geom_text nrow(data2)times!

The correct way for supplying data to geom_text is building a different data.frame holding coordinates, labels and colors for the strings you want to be plotted:

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))
旧城烟雨 2024-08-31 01:09:06

如果您使用 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")) 

因此使用 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

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

好多鱼好多余 2024-08-31 01:09:06

现在有一些软件包添加了标签:

  1. directlabel 软件包中的 geom_dl 具有自动数据过滤功能,以避免重复标签,
  2. ggrepel 软件包中的 geom_label_repel 和 geom_text_repel 制作的标签尽量避免位于数据之上或彼此重叠,在 geom_label_repel 的情况下有边界和不透明的背景。

These days there are packages adding labeling:

  1. geom_dl from the directlabel package with automatic data filtering to avoid repeated labels and
  2. geom_label_repel and geom_text_repel from package ggrepel make labels that try to avoid sitting on top of the data or each other and which in the case of geom_label_repel have a boundary and a non-transparant background.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文