我有一个连续变量和,并在x轴的分类。在分类变量中,顺序是有意义的,根据其索引来拟合回归是有意义的,我的意思是代替c('a', 'b', 'c')
使用索引(order(c('a', 'b', 'c'))
,即c(1, 2, 3)
),并使模型与此相适应。但是,如果一个变量不是数字,ggplot会拒绝匹配geom_smooth(method = lm)
。好吧,那么我可以告诉它使用的顺序:ggplot2:拟合geom_smooth()像分类变量是连续的
geom_smooth(aes(x = order(hgcc), y = rtmean), method = lm)
但随后它需要整列的索引从数据帧,这是不好用scales = 'free'
刻面,当只有水平的一个子集x
变量的变量出现在一个图上。在整个数据帧的指标是平均高得多,所以回归将远远右侧绘制:
这里是一个最小的工作例如:
require(ggplot2)
load(url('http://www.ebi.ac.uk/~denes/54b510889336eb2591d8beff/sample_data.RData'))
ggplot(adata12cc, aes(x = hgcc, y = rtmean, color = cls, size = log10(intensity))) +
geom_point(stat = 'sum', alpha = 0.33) +
geom_smooth(
aes(x = order(hgcc), y = rtmean),
method = 'glm') +
facet_wrap(~ uhgroup, scales = 'free') +
scale_radius(guide = guide_legend(title = 'Intensity (log)')) +
scale_color_discrete(guide = guide_legend(title = 'Class')) +
xlab('Carbon count unsaturation') +
ylab('Mean RT [min]') +
ggtitle('RT vs. carbon count & unsaturation by headgroup') +
theme(axis.title = element_text(size = 24),
axis.text.x = element_text(angle = 90, vjust = 0.5, size = 9, hjust = 1),
axis.text.y = element_text(size = 11),
plot.title = element_text(size = 21),
strip.text = element_text(size = 18),
panel.grid.minor.x = element_blank())
我知道这是不是做事的好方法,但是ggplot可以让生活变得如此简单,如果我可以引用这些变量并对它们进行一些操作,而这些变量是通过分面来进行子集化的。
哇,这真的有用!非常感谢! :) – deeenes