从两个 data.frame 绘制数据时出现图例问题

发布于 2024-08-12 09:07:17 字数 548 浏览 4 评论 0原文

我在让 ggplot2 按我想要的方式工作时遇到了一些麻烦。基本上,我想通过将实际观察结果与近似值放在一个图中来进行比较。例如,

> library(ggplot2)
> df.actual <- data.frame(x = 1:100, y = (1:100) * 2)
> df.approx <- data.frame(x = 1:150, y = (1:150) * 2 + 5  + rnorm(150, mean = 3) )
> ggplot() + geom_point(aes(x, y), data = df.actual) + geom_line(aes(x,y), data = df.approx)

我的问题是无法显示图例。我在某处读到 ggplot2 的图例不是很灵活(?)。理想情况下,图例的

  • 标题 = '类型'
  • 键:黑色填充点,黑色线条
  • 键标签:'实际'、'近似'
  • legend.position = 'topright'

谢谢。

I'm having a little trouble with getting ggplot2 to work as I want. Basically, I'd like to compare actual observations vs. approximated by putting them in one single plot. For example,

> library(ggplot2)
> df.actual <- data.frame(x = 1:100, y = (1:100) * 2)
> df.approx <- data.frame(x = 1:150, y = (1:150) * 2 + 5  + rnorm(150, mean = 3) )
> ggplot() + geom_point(aes(x, y), data = df.actual) + geom_line(aes(x,y), data = df.approx)

My problem is that I can't display a legend. I read somewhere that ggplot2's legend is not very flexible(?). Ideally, a legend with

  • title = 'Type'
  • key: a black filled point, and a black line
  • key label: 'Actual', 'Approximate'
  • legend.position = 'topright'

Thanks.

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

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

发布评论

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

评论(2

爱情眠于流年 2024-08-19 09:07:17

尝试这个来帮助您入门

ggplot() + 
  geom_point(aes(x, y, colour = "actual"), data = df.actual) + 
  geom_line(aes(x, y, colour = "approximate"), data = df.approx) + 
  scale_colour_discrete("Type")

Try this to get you started

ggplot() + 
  geom_point(aes(x, y, colour = "actual"), data = df.actual) + 
  geom_line(aes(x, y, colour = "approximate"), data = df.approx) + 
  scale_colour_discrete("Type")
灯角 2024-08-19 09:07:17

这是某种通过操纵网格对象来修改图例的黑客:

library("ggplot2")
df.actual <- data.frame(x=1:100, y=(1:100)*2)
df.approx <- data.frame(x=1:150, y=(1:150)*2 + 5 + rnorm(150, mean=3))
p <- ggplot() +
     geom_point(aes(x, y, colour="Actual"), data=df.actual) +
     geom_line(aes(x, y, colour="Approximate"), data=df.approx) +
     scale_colour_manual(name="Type",
                         values=c("Actual"="black", "Approximate"="black"))
library("grid")
grob <- ggplotGrob(p)
tmp <- grid.ls(getGrob(grob, "key.segments", grep=TRUE, global=TRUE))$name
grob <- removeGrob(grob, tmp[1])  # remove first line segment in legend key
tmp <- grid.ls(getGrob(grob, "key.points", grep=TRUE, global=TRUE))$name
grob <- removeGrob(grob, tmp[2])  # remove second point in legend key
grid.draw(grob)

ggplot2 输出 http ://img134.imageshack.us/img134/8427/ggplotlegend.png

This is some kind of hack to modify the legend by manipulation of the grid object:

library("ggplot2")
df.actual <- data.frame(x=1:100, y=(1:100)*2)
df.approx <- data.frame(x=1:150, y=(1:150)*2 + 5 + rnorm(150, mean=3))
p <- ggplot() +
     geom_point(aes(x, y, colour="Actual"), data=df.actual) +
     geom_line(aes(x, y, colour="Approximate"), data=df.approx) +
     scale_colour_manual(name="Type",
                         values=c("Actual"="black", "Approximate"="black"))
library("grid")
grob <- ggplotGrob(p)
tmp <- grid.ls(getGrob(grob, "key.segments", grep=TRUE, global=TRUE))$name
grob <- removeGrob(grob, tmp[1])  # remove first line segment in legend key
tmp <- grid.ls(getGrob(grob, "key.points", grep=TRUE, global=TRUE))$name
grob <- removeGrob(grob, tmp[2])  # remove second point in legend key
grid.draw(grob)

ggplot2 output http://img134.imageshack.us/img134/8427/ggplotlegend.png

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文