本文介绍了在多方面ggplot上为每个geom_smooth()设置唯一的跨度参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否可以为ggplot对象的每个方面设置不同的span参数.我想在单个ggplot对象上比较四组相关的行业数据.我想修改每行 geom_smooth()
的跨度,以更准确地为我的数据建模.
I was wondering if it's possible to set a different span parameter for each facet of my ggplot object. I have four sets of related industry data that I would like to compare on a single ggplot object. I would like to modify the span for each geom_smooth()
line to more accurately model my data.
library(ggplot2)
library(reshape2)
a=rnorm(50,0,1)
b=rnorm(50,0,3)
ind=1:100
df=data.frame(ind,sort(a),sort(b))
df1=melt(df, id='ind')
t=ggplot(df1, aes(x=ind,y=value, color=variable))+
geom_smooth(color='black', span=.5)+
geom_point(color='black')+
facet_wrap(~variable,ncol=2)
例如,第一个构面的跨度为.5,第二个构面的跨度为.8吗?
For instance, is it possible to have a span of .5 for the first facet and a span of .8 for the second facet?
推荐答案
您可以过滤数据,仅将过滤后的子集提供给每个 geom_smooth
You can filter your data and only provide the filtered subset to each geom_smooth
ggplot(df1, mapping = aes(x=ind, y=value, color=variable)) +
geom_point(color='black') +
geom_smooth(data = df1 %>% filter(variable=='sort.a'), span=0.5, method='loess') +
geom_smooth(data = df1 %>% filter(variable=='sort.b'), span=0.3, method='loess') +
facet_wrap(~variable,ncol=2)
这篇关于在多方面ggplot上为每个geom_smooth()设置唯一的跨度参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!