我需要帮助使用ggplot2自定义图形。下面是我正在使用的代码和结果图:
> p=ggplot(a2,aes(x=grid,y=median))+geom_line(size=1.3)+
geom_line(aes(x=grid,y=lower,linetype=2,colour='red'),size=1.3)+
geom_line(aes(x=grid,y=upper,linetype=2,colour='red'),size=1.3)+
geom_line(aes(x=trueX,y=trueY,linetype=1,colour='blue'),size=.8)+
geom_line(aes(x=grid,y=xDens,linetype=1),size=.5)+
geom_point(data=a1,aes(x=X,y=Y),size=1)
首先,红色和蓝色的颜色似乎应该是它们的颜色。其次,我想用四个条目自定义图例:
我已经玩了一段时间,但无法理解。会很感激的建议。
我可能需要重新安排我的数据以获得我想要的内容,这里是我现在所拥有的预览(忽略带有点的数据集a1):
> head(a2[,c(1,2,4,5,6,7,8)])
lower upper median grid trueX trueY xDens
1 26.65933 28.83431 27.54782 -4.845770 -3.998449 27.73697 0.02803692
2 26.65933 28.77224 27.54284 -4.826273 -3.967264 27.71877 0.02902979
3 26.65933 28.77224 27.54284 -4.806776 -3.910030 27.68470 0.02953585
4 26.65933 28.77224 27.54284 -4.787279 -3.898646 27.67782 0.03056721
5 26.65933 28.77224 27.54284 -4.767782 -3.891712 27.67361 0.03162419
6 26.65933 28.77224 27.54284 -4.748285 -3.890767 27.67304 0.03270666
答案 0 :(得分:4)
可能这很接近你想要的东西:
示例数据:
> d
lower upper median grid trueX trueY xDens
1 26.65933 28.83431 27.54782 -4.845770 -3.998449 27.73697 0.02803692
2 26.65933 28.77224 27.54284 -4.826273 -3.967264 27.71877 0.02902979
3 26.65933 28.77224 27.54284 -4.806776 -3.910030 27.68470 0.02953585
4 26.65933 28.77224 27.54284 -4.787279 -3.898646 27.67782 0.03056721
5 26.65933 28.77224 27.54284 -4.767782 -3.891712 27.67361 0.03162419
6 26.65933 28.77224 27.54284 -4.748285 -3.890767 27.67304 0.03270666
代码:
d2 <- melt(subset(d, select = -c(trueX)), id = "grid")
ggplot(d2, aes(grid, value, colour = variable, linetype = variable, size = variable)) +
geom_line() +
scale_colour_manual("", values = c("red", "red", "black", "blue", "black"), breaks = levels(d2$variable)) +
scale_linetype_manual("", values = c(2, 2, 1, 1, 1), breaks = levels(d2$variable)) +
scale_size_manual("", values = c(0.5, 0.5, 0.5, 0.5, 0.25), breaks = levels(d2$variable))
输出: