This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to smooth (ggplot2) 3 samples in the same plot

Hi everybody,

I want to smooth 3 different samples and see the result in the same plot. For that propose I'm using ggplot. I can plot the 3 curves in the same plot , but when I've tried to smoothing them, I have to make it separately, as follow:

c <- ggplot(d3, aes(x = bins, y = sample1, color="sample1")) + labs(y = "Coverage", x = "distance to TSS")
d <- ggplot(d3, aes(x = bins, y =sample2, color="sample2"))+ labs(y = "Coverage", x = "distance to TSS")
e <- ggplot(d3, aes(x = bins, y = sample3,  color="sample3"))+ labs(y = "Coverage", x = "distance to TSS")

    c + stat_smooth(span = 0.2) 
    d + stat_smooth(span = 0.2)
    e + stat_smooth(span = 0.2)

Is there any way to plot the 3 smooth samples in the same plot?

Thank you very much

r ggplot2 smooth plot chip-seq

1 answer

Have you tried further collapsing the d3 data frame such that sample is a column with levels sample1, sample2, and sample3? That would solve this (you'd color or fill by sample then). You can use the tidyr package to tidy your data frame.

Now I have a data frame like this:

Bins   Sample1    Sample2  Sample3
1         0.54            0.59            0.41
2         0.62            0.75            0.13 
3         0.76            0.97            0.50
4         0.21            0.43            0.53

Are you suggesting me to perform a data frame like:

Bins     Samples      Coverage
1           Sample3        0.13
2           Sample1        0.21  
3           Sample3        0.41
4           Sample2        0.42

.....

Thanks!

If you're just trying to add a smoothed series to the plot, have a look at the geom_smooth() aesthetic.

I have a look, but I didn't find anything useful...

Yes, then you can use either stat_smooth() or geom_smooth() (they're essentially the same thing) with aes(color=Samples, fill=Samples). ggplot works best with "tidy data" (google "R tidy data" if you're not familiar with that concept).

Thank you Davon, I know hot to plot them after tidy my data, the problem is, that I don't know how to tidy it. Is the first time that I come across with this problem. Any suggestion for my starting?

Thanks!

Thank you, I've used gather and arrange functions and now it works perfectly!!!!! thank you very much for your advice!

Log in to answer this question.