本文介绍了在ggplot中自定义图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我需要使用ggplot2来定制图表。下面是我使用的代码和结果图: > p = ggplot(a2,aes(x = grid,y = median))+ geom_line(size = 1.3)+ geom_line(aes(x = grid,y = lower,linetype = 2,color ='red' ),size = 1.3)+ geom_line(aes(x = grid,y = upper,linetype = 2,color ='red'),size = 1.3)+ geom_line(aes(x = trueX ,y = trueY,linetype = 1,color ='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) 首先,红色和蓝色的颜色看起来应该是什么样子。其次,我想用四个条目自定义图例: 适合(黑色线条) Conf Int(红色虚线) 真实曲线(蓝色线) X密度(小黑线) 我一直在玩它一段时间,但无法弄清楚。我可能需要重新排列我的数据以得到我想要的,这里是我现在所拥有的预览(忽略数据集a1和点): > (a2 [,c(1,2,4,5,6,7,8)])低于中位数网格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 低于中值网格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 code: d2 ggplot(d2,aes(grid,value,color = variable,linetype = variable,size =变量))+ 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(,value s = c(0.5,0.5,0.5,0.5,0.25),break = levels(d2 $ variable)) 输出: I need help customizing a graph with ggplot2. Below is the code I am using and the resulting graph:> 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)First, the colors red and blue seem off from what they should be. Second, I would like to customize the legend with four entries:Fit (black line)Conf Int (red dashed line)True Curve (blue line)X Density (small black line)I have been playing with it for awhile but can't figure it out. Would appreciate suggestions.I might need to rearrange my data to get what I want, here is a preview of what I have now (ignoring the data set a1 with the points):> head(a2[,c(1,2,4,5,6,7,8)]) lower upper median grid trueX trueY xDens1 26.65933 28.83431 27.54782 -4.845770 -3.998449 27.73697 0.028036922 26.65933 28.77224 27.54284 -4.826273 -3.967264 27.71877 0.029029793 26.65933 28.77224 27.54284 -4.806776 -3.910030 27.68470 0.029535854 26.65933 28.77224 27.54284 -4.787279 -3.898646 27.67782 0.030567215 26.65933 28.77224 27.54284 -4.767782 -3.891712 27.67361 0.031624196 26.65933 28.77224 27.54284 -4.748285 -3.890767 27.67304 0.03270666 解决方案 probably this is near from what you want:sample data:> d lower upper median grid trueX trueY xDens1 26.65933 28.83431 27.54782 -4.845770 -3.998449 27.73697 0.028036922 26.65933 28.77224 27.54284 -4.826273 -3.967264 27.71877 0.029029793 26.65933 28.77224 27.54284 -4.806776 -3.910030 27.68470 0.029535854 26.65933 28.77224 27.54284 -4.787279 -3.898646 27.67782 0.030567215 26.65933 28.77224 27.54284 -4.767782 -3.891712 27.67361 0.031624196 26.65933 28.77224 27.54284 -4.748285 -3.890767 27.67304 0.03270666code: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))output: 这篇关于在ggplot中自定义图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-29 03:53