Change size of a line plot, understand how the size argument works

天大地大妈咪最大 提交于 2019-12-20 04:11:39

问题


I'm making a multiple lines plot with errorbars. If I don't use the size argument, everything is fine:

# sample data
Response=runif(4)
ResponseMin=Response-Response/5
ResponseMax=Response+Response/5 
Cases=rep(c("Case1","Case2"),each=2)    
df=data.frame(x=1:2,Average=Response,Lower=ResponseMin,Upper=ResponseMax,Case=Cases)
# let's plot
library(ggplot2)
ggplot(df,aes(x=x,y=Average,colour=Case)) +
geom_line(aes(group=Case)) + 
geom_point() +
geom_errorbar(aes(ymin=Lower,ymax=Upper,width=0.25)) +
labs(y="foo",title="Some plot fu")

However, when I modify the line size, I start getting weird stuff:

ggplot(df,aes(x=x,y=Average,colour=Case)) +
geom_line(aes(group=Case, size = 1)) + 
geom_point() +
geom_errorbar(aes(ymin=Lower,ymax=Upper,width=0.25)) +
labs(y="foo",title="Some plot fu")

Why the extra legend entry "1"? And when I add a size argument for the errobars, it looks like the size of the lines stays the same, whatever the value of size is:

ggplot(df,aes(x=x,y=Average,colour=Case)) +
geom_line(aes(group=Case, size = 1)) + 
geom_point() +
geom_errorbar(aes(ymin=Lower,ymax=Upper,width=0.25, size = 1)) +
labs(y="foo",title="Some plot fu")

ggplot(df,aes(x=x,y=Average,colour=Case)) +
geom_line(aes(group=Case, size = 2)) + 
geom_point() +
geom_errorbar(aes(ymin=Lower,ymax=Upper,width=0.25, size = 2)) +
labs(y="foo",title="Some plot fu")

Can you help me figure out what's happening here?


回答1:


If you set size inside aes you are mapping it to a variable

`1` = 1

and ggplot2 creates a legend. If you just want to set the size, you can do that outside of aes:

geom_line(aes(group=Case), size = 1)



回答2:


try this, size outside aes()

ggplot(df,aes(x=x,y=Average,colour=Case)) +
    geom_line(aes(group=Case), size = 1) + 
    geom_point() +
    geom_errorbar(aes(ymin=Lower,ymax=Upper,width=0.25)) +
    labs(y="foo",title="Some plot fu")


来源:https://stackoverflow.com/questions/34354473/change-size-of-a-line-plot-understand-how-the-size-argument-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!