我在让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的传说不是很灵活(?)。理想情况下,一个传奇

  • title ='type'
  • 钥匙:黑色填充点和黑线
  • 钥匙标签:“实际”,“近似”
  • legend.position ='potright'

谢谢。

有帮助吗?

解决方案

尝试这个让你开始

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

其他提示

这是通过操纵网格对象来修改传奇的某种骇客:

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top