Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

r - fitting geom_smooth() to multiple categories in 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

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:

enter image description here

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...