使geom_smooth()适合ggplot2中的多个类别 [英] fitting geom_smooth() to multiple categories in ggplot2

查看:70
本文介绍了使geom_smooth()适合ggplot2中的多个类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些看起来像这样的数据

I have some data which looks like:

sample  diff    chromosome  haploid coverage
B   90.9963099631   7   b   0.513
A   91.7019475021   12  a   8.234
C   90.3855783676   14  a   6.211
D   91.3407821229   17  b   4.321
E   91.5740740741   11  b   0.213
F   90.9963099631   7   b   0.513
G   91.7019475021   12  a   8.234
H   90.3855783676   14  a   6.211
I   91.3407821229   17  b   4.321
J   91.5740740741   11  b   0.213

我想可视化不同染色体上不同覆盖率值对'diff'的变化,所以我这样做了:

And I want to visualise the change for 'diff' for different coverage value, across different chromosomes, so I did this:

plot = ggplot(dat, aes(x = coverage, y = diff, group  = chromosome, colour = chromosome, ylim(0, 100)) ) + geom_point(colour = chromosome) + stat_smooth(se=FALSE) + ylim(0, 100)

`geom_smooth()` using method = 'loess'

现在,我认为这不是绘制多条趋势线的最佳方法,但我制作了此代码:

Now I think this isn't the best way to draw mutliple trendlines, but I produced this:

这是我所追求的.但是我认为实际上,根据我的数据,所有曲线都不应该完全相同,但是它们的形状基本上是相同的.那么geom_smooth()是否为所有类别绘制一条LOWESS曲线,然后针对不同类别只是上下移动同一条曲线?

Which is kind of what I'm after. But I think in reality, all the curves shouldn't be exactly the same shape given my data, but the shape of all of them is basically identical. So has geom_smooth() plot a single LOWESS curve for all categories and then just shifted the same curve up and down for different categories?

如果是这样,是否有一种方法可以使它为每个类别绘制独立的线条?

If so, is there a way to make it plot independent lines for each category?

推荐答案

无法使用您提供的代码复制绘图.但是,使用 mtcars 数据集,获取所需内容的正确语法如下.

Cannot replicate your plot with the code you have provided. However, the correct syntax to obtain what you're looking for is the following, using the mtcars dataset.

library(ggplot2)

df = mtcars

df$cyl = factor(df$cyl)

ggplot(df, aes(x = mpg, y = disp, colour = cyl, group = cyl)) + 
  geom_point() + 
  geom_smooth(se=F)

尝试根据此代码调整示例/数据集.

Try to adjust your example / dataset on this code.

这篇关于使geom_smooth()适合ggplot2中的多个类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆