gpt4 book ai didi

r - ggplot2:适合 geom_smooth() 像分类变量是连续的

转载 作者:行者123 更新时间:2023-12-02 20:55:05 25 4
gpt4 key购买 nike

我在 y 轴上有一个连续变量,在 x 轴上有一个分类变量。在分类变量中,顺序是有意义的,并且通过其索引来拟合回归也是有意义的,我的意思是使用索引而不是 c('a', 'b', 'c') (order(c('a', 'b', 'c')),即 c(1, 2, 3)),并根据模型拟合这。但是,如果一个变量不是数字,则 ggplot 会拒绝拟合 geom_smooth(method = lm)。好的,那么我可以告诉它使用命令:

geom_smooth(aes(x = order(hgcc), y = rtmean), method = lm)

但是它会从数据框中获取整个列的索引,当只有 级别的子集时,这对于使用 scales = 'free' 进行分面来说并不好。 x 变量出现在一张图上。整个数据框中的索引平均要高得多,因此回归将绘制在最右侧:

regression pushed to right

这是一个最小的工作示例:

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 可以让生活变得更容易,这些变量无论如何都是通过分面进行子集化的。

最佳答案

我想我已经找到了解决方案,但我不确定你想要什么......

主要问题是您的 x 值标签已被 uhgroup 分割如果你看看这个因素,它们是 PC-O(38.7) PC(38.7 等...

因此,第一件事就是为 x 轴创建一个新的 hgcc 值。

adata12cc$hgcc_value <-as.factor(substr(adata12cc$hgcc, (nchar(levels(adata12cc$hgcc)[adata12cc$hgcc])-5), nchar(levels(adata12cc$hgcc)[adata12cc$hgcc])))

另一个问题是 geom_pointgeom_smooth 有不同的 x 轴。一个是hgcc,另一个是order(hgcc_value)

解决方案是使用相同的值,这里我使用 as.numeric(hgcc_value) (而不是 order())并在 scale_x_continuous 中精确 中断的标签。

ggplot(adata12cc, aes(x = as.numeric(hgcc_value), y = rtmean, color = cls, size = log10(intensity))) +
geom_point(stat = 'sum', alpha = 0.33) +
geom_smooth(
aes(x = as.numeric(hgcc_value), 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')) +
scale_x_continuous(name = "Carbon count unsaturation",
breaks=as.numeric(adata12cc$hgcc_value),
labels = adata12cc$hgcc_value,
minor_breaks = NULL)+
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())

enter image description here

这是您要找的吗?

关于r - ggplot2:适合 geom_smooth() 像分类变量是连续的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40741068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com