我想用ggplot创建一个显示方法比较数据的散点图。这些地块应该有原始数据,理想线和错误的拟合线。图例应显示理想线和拟合线的线型/线宽/线条颜色。ggplot散点图中的传奇故事
我可以得到大部分的我想要什么,但有这些问题与传说:
的图例显示2行的每一行的类型,为什么?如何解决?
我宁愿在传说中的矩形没有粉红色的背景(如果我没有指定填充颜色则rectanglebackground成为默认的灰色,这是我不喜欢任何更好)
示例代码:
set.seed(603)
x.raw=rnorm(n=30, mean=50, sd=20)
y.raw=x.raw+rnorm(n=30, mean=2, sd=2)
x.raw=round(x.raw, 2); y.raw=round(y.raw, 2)
df=data.frame(x=x.raw, y=y.raw)
require(ggplot2, quietly=TRUE)
theme_set(theme_bw())
xy.range=range(df$x, df$y)
p=ggplot(df, aes(x=x, y=y)) +
geom_point(shape=ifelse(nrow(df)>49, 1, 16)) +
geom_smooth(method=lm, fill="red1", aes(colour="Fitted", linetype="Fitted")) +
geom_abline(intercept=0, slope=1, aes(colour="Ideal", linetype="Ideal")) +
scale_colour_manual(name="Lines", values=c("Ideal"="blue", "Fitted"="red")) +
scale_linetype_manual(name="Lines",
values=c("Ideal"="solid", "Fitted"="twodash")) +
scale_x_continuous(name="Control", limits=xy.range) +
scale_y_continuous(name="Evaluation", limits=xy.range) +
opts(title="Method Comparison")
p
我真的很感谢大家花时间回复。虽然有什么有效的逻辑,我不会在那里反复试验。我确实改变一下代码进行最后的:最后
- 做出geom_point使点不被覆盖
- 保持通话不断扩展,使X和Y轴的限制被迫成为同
- 类似的说明,加入aspect.ratio = 1,现在理想线从角变为45°角人LA克利夫兰
最终代码到角:
ggplot(df, aes(x=x, y=y)) +
geom_smooth(method=lm, se=FALSE, size=1, aes(colour="Fitted", linetype="Fitted")) +
geom_smooth(method=lm, fill="red", colour="red", linetype="twodash", size=1) +
geom_line(data = data.frame(x=0, y=0), aes(colour = "Ideal", linetype = "Ideal"), size=1) +
#geom_abline(intercept=0, slope=1, aes(colour = "Ideal", linetype = "Ideal"), size=0) +
geom_abline(intercept=0, slope=1, colour = "blue", linetype = "solid", size=1) +
geom_point(shape=ifelse(nrow(df)>49, 1, 16)) +
scale_colour_manual(name="Lines", values=c("Ideal"="blue", "Fitted"="red")) +
scale_linetype_manual(name="Lines", values=c("Ideal"="solid", "Fitted"="twodash")) +
scale_x_continuous(name="Control", limits=xy.range) +
scale_y_continuous(name="Evaluation", limits=xy.range) +
opts(title="Method Comparison", aspect.ratio=1) +
theme_bw()
一号线是顺利,另一个是abline。至于如何解决这个问题,我想要做的就是不用任何关于实际情节的传说。然后伪造一些数据,并使用geom_line将其绘制成图标。但这只是一个解决方法。 – 2011-06-07 13:53:27