我需要使用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
最佳答案
可能这离您想要的地方很近:
样本数据:
> 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))
输出: