How to smooth (ggplot2) 3 samples in the same plot
1
0
Entering edit mode
7.3 years ago
Lila M ★ 1.2k

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 • 4.7k views
ADD COMMENT
1
Entering edit mode
7.3 years ago

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.

ADD COMMENT
0
Entering edit mode

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!

ADD REPLY
1
Entering edit mode

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).

ADD REPLY
0
Entering edit mode

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!

ADD REPLY
3
Entering edit mode

Take a look at the tidyr package. In the vignette you have a nice introduction.

ADD REPLY
1
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 2750 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6