multiplot

101 views
Skip to first unread message

gireesh bogu

unread,
Oct 2, 2010, 8:01:24 AM10/2/10
to ggplot2
Dear all

Here is a 2column file, where I plotted Distance a x-axis and Signal1
as Y-axis by using ggplot with my own defined sizes and colour. It
works pretty well!
Is there any way to plot multiple Signal columns as shown in the last
file like Signal Signal2 ... on Y-axis by using Distance as X-axis
with specific user defined colour and other settings ?

Thanx a ton
Gireesh



Distance Signal1
-10000 0.03
-9998 0.03
-9996 0.02
-9995 0.02
.......................
......
10000 0.14
*****************************************************************************
library(ggplot2)
c <- ggplot(datax, aes(Distance, Signal1))
c + stat_smooth()
c + stat_smooth(fill=”darkgrey”, colour=”red”, size=2, alpha = 0.2)
*****************************************************************************

Distance Signal1 Signal2 Signal3 ...
-10000 0.03 0.12 0.9
-9998 0.03 0.4 0.6
-9996 0.02 0.22 0.2
-9995 0.02 0.99 0.8
.......................
......
10000 0.14 0.9 0.77

takahashi kohske

unread,
Oct 2, 2010, 12:29:05 PM10/2/10
to gireesh bogu, ggplot2
hi

here is an example:

x<- 1:10
df<-data.frame(x=x, s1=runif(length(x)),s2=runif(length(x)))

ggplot(melt(df,id="x"),aes(x=x,y=value,colour=variable))+stat_smooth(fill="darkgrey",
size=2, alpha = 0.2)

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

Brandon Hurr

unread,
Oct 2, 2010, 12:30:23 PM10/2/10
to gireesh bogu, ggplot2
Should be able to do it just by having a stat_smooth() for each set of Y data.

c <- ggplot(datax, aes(x=Distance))

c+
stat_smooth(aes(y=Signal1), fill=”darkgrey”, colour=”red”, size=2, alpha = 0.2)+
stat_smooth(aes(y=Signal2), fill=”grey80”, colour=”blue”, size=2, alpha = 0.2)

If it starts to get too complex consider using reshape/melt.

datax2 <- melt(datax, id.vars=Distance)

d <- ggplot(datax, aes(x=Distance, y=value))

d + stat_smooth(aes(colour = variable), size = 2, alpha = 0.2)

Should work...
###############
x
Distance Signal1 Signal2 Signal3
1 -10000 0.03 0.12 0.9
2 -9998 0.03 0.40 0.6
3 -9996 0.02 0.22 0.2
4 -9995 0.02 0.99 0.8
> melt(x, id.vars=Distance)
Error in melt_check(data, id.vars, measure.vars) :
object 'Distance' not found
> melt(x, id.vars="Distance")
Distance variable value
1 -10000 Signal1 0.03
2 -9998 Signal1 0.03
3 -9996 Signal1 0.02
4 -9995 Signal1 0.02
5 -10000 Signal2 0.12
6 -9998 Signal2 0.40
7 -9996 Signal2 0.22
8 -9995 Signal2 0.99
9 -10000 Signal3 0.90
10 -9998 Signal3 0.60
11 -9996 Signal3 0.20
12 -9995 Signal3 0.80

gireesh bogu

unread,
Oct 2, 2010, 9:04:43 PM10/2/10
to Brandon Hurr, ggplot2
Thanx alot Brandon. Working very fine (2nd one is not because it is not recognizing Y variables-Signal1 and 2). Anyways 1st method is doing the job very well!

Just a small addition. Is there any way to include small colour squares on the top right of the figure indicating what different colours representing in the figures
ex: Signal1 - red  it should look like red box - Signal1

thanx again
Gireesh

Brandon Hurr

unread,
Oct 3, 2010, 3:29:00 AM10/3/10
to gireesh bogu, ggplot2
What you want is a legend and the easiest way to do that is using melt
and setting the colour by variable which is what Takahashi and I
suggested. You can make your own legend but it's not something I have
done before.

I'm not sure why it didn't work though.

If you run:

#I didn't have "" around the variable name in my example last time
#that may have been the problem

datax2 <- melt(datax, id.vars="Distance")

d <- ggplot(datax, aes(x=Distance, y=value))

d + stat_smooth(aes(colour = variable), size = 2, alpha = 0.2)

What is the error that you get?

Run:

head(datax2, 25)

Does the output look similar to?

Distance variable value
1 -10000 Signal1 0.03
2 -9998 Signal1 0.03
3 -9996 Signal1 0.02
4 -9995 Signal1 0.02
5 -10000 Signal2 0.12
6 -9998 Signal2 0.40
7 -9996 Signal2 0.22
8 -9995 Signal2 0.99
9 -10000 Signal3 0.90
10 -9998 Signal3 0.60
11 -9996 Signal3 0.20
12 -9995 Signal3 0.80

Brandon

Brandon Hurr

unread,
Oct 3, 2010, 3:43:47 AM10/3/10
to gireesh bogu, ggplot2
Yes, sorry.

B

On Sun, Oct 3, 2010 at 08:36, gireesh bogu <girees...@gmail.com> wrote:
> datax2 <- melt(datax, id.vars="Distance")
>
> d <- ggplot(datax, aes(x=Distance, y=value))
>
> d + stat_smooth(aes(colour = variable), size = 2, alpha = 0.2)
>

> #################################
> datax2 data is looking fine but no output
> it should be this d <- ggplot(datax2, aes(x=Distance, y=value)) ??
>
>
> -G

Brandon Hurr

unread,
Oct 3, 2010, 3:46:38 AM10/3/10
to gireesh bogu, ggplot2
No warning?

What about with Takahashi's code?

ggplot(melt(datax,id="Distance"),aes(x=Distance,y=value,colour=variable))+
stat_smooth(fill="darkgrey",size=2, alpha = 0.2)

Brandon

On Sun, Oct 3, 2010 at 08:44, gireesh bogu <girees...@gmail.com> wrote:
> I'm afraid still no output ..

Brandon Hurr

unread,
Oct 3, 2010, 3:53:39 AM10/3/10
to gireesh bogu, ggplot2
run:

sessionInfo()

and paste the output in here.

On Sun, Oct 3, 2010 at 08:50, gireesh bogu <girees...@gmail.com> wrote:
> It is also same no warning..no output..

Brandon Hurr

unread,
Oct 3, 2010, 4:00:22 AM10/3/10
to gireesh bogu, ggplot2
Well... you're up to date.

The code seems to work for me and Takahasi.

I'm guessing your problem is with your data.

Does your datax2 dataframe look like mine looked?

It should have three columns: Distance, variable, and value.

On Sun, Oct 3, 2010 at 08:54, gireesh bogu <girees...@gmail.com> wrote:
>> sessionInfo()
> R version 2.11.1 (2010-05-31)
> i386-pc-mingw32
> locale:
> [1] LC_COLLATE=English_United States.1252
> [2] LC_CTYPE=English_United States.1252
> [3] LC_MONETARY=English_United States.1252
> [4] LC_NUMERIC=C
> [5] LC_TIME=English_United States.1252
> attached base packages:
> [1] grid      stats     graphics  grDevices utils     datasets  methods
> [8] base
> other attached packages:
> [1] mgcv_1.6-2    ggplot2_0.8.8 proto_0.3-8   reshape_0.8.3 plyr_1.2.1
> loaded via a namespace (and not attached):
> [1] digest_0.4.2       lattice_0.18-8     Matrix_0.999375-39 nlme_3.1-96

Brandon Hurr

unread,
Oct 3, 2010, 4:11:25 AM10/3/10
to gireesh bogu, ggplot2
Seemed to work. The original example used "Distance," but your dataset
had "distance." That may or may not have been the issue, but if it
were the issue, it should have given you a warning

> datax<-read.delim("/Users/bhurr/Desktop/result_skip.txt")
> head(datax)
distance skip_h3k36me3 skip_ptb_clusters ts_h3k36me3 ts_ptb_clusters
1 -999 0.0025 3e-04 0.0049 4e-04
2 -998 0.0025 3e-04 0.0049 3e-04
3 -996 0.0025 3e-04 0.0049 3e-04
4 -994 0.0025 3e-04 0.0049 2e-04
5 -991 0.0025 3e-04 0.0049 1e-04
6 -989 0.0025 3e-04 0.0049 1e-04
> datax2 <- melt(datax, id.vars= "distance")
> head(datax2)
distance variable value
1 -999 skip_h3k36me3 0.0025
2 -998 skip_h3k36me3 0.0025
3 -996 skip_h3k36me3 0.0025
4 -994 skip_h3k36me3 0.0025
5 -991 skip_h3k36me3 0.0025
6 -989 skip_h3k36me3 0.0025
> d <- ggplot(datax2, aes(x=distance, y=value))


>
> d + stat_smooth(aes(colour = variable), size = 2, alpha = 0.2)

Warning messages:
1: Removed 120 rows containing missing values (stat_smooth).
2: Removed 120 rows containing missing values (stat_smooth).
3: Removed 120 rows containing missing values (stat_smooth).
4: Removed 120 rows containing missing values (stat_smooth).
>

On Sun, Oct 3, 2010 at 09:06, gireesh bogu <girees...@gmail.com> wrote:
> Here you go
> thanx
> Gireesh
>
> On Sun, Oct 3, 2010 at 4:05 PM, Brandon Hurr <bhi...@gmail.com> wrote:
>>
>> Can you share your dataset or a subset of it so I can see if I have
>> the same problem?
>>
>> Something seems fishy about this.
>>
>> B
>>
>> On Sun, Oct 3, 2010 at 09:01, gireesh bogu <girees...@gmail.com> wrote:
>> > yes
>> >> datax2[1,]
>> >   distance      variable  value
>> > 1     -999 skip_h3k36me3 0.0025

output.png

Brandon Hurr

unread,
Oct 3, 2010, 4:17:14 AM10/3/10
to gireesh bogu, ggplot2
I also noticed that if you add your fill in that you won't hardly see
the SE of the smooth with a line size of 2. You can barely make it out
with the default size.

d <- ggplot(datax2, aes(x=distance, y=value))
>

> d + stat_smooth(aes(colour = variable), fill="darkgrey", alpha = 0.2) + theme_bw()


Warning messages:
1: Removed 120 rows containing missing values (stat_smooth).
2: Removed 120 rows containing missing values (stat_smooth).
3: Removed 120 rows containing missing values (stat_smooth).
4: Removed 120 rows containing missing values (stat_smooth).


On Sun, Oct 3, 2010 at 09:13, gireesh bogu <girees...@gmail.com> wrote:
> thank you. some thing wrong with R. I closed it and ran the same command
> worked fine.
> Thanx for your time!
> really appreciated!
> Gireesh

output2.png

gireesh bogu

unread,
Oct 3, 2010, 3:44:51 AM10/3/10
to Brandon Hurr, ggplot2
I'm afraid still no output ..

gireesh bogu

unread,
Oct 3, 2010, 4:26:51 AM10/3/10
to Brandon Hurr, ggplot2
thanx man!

gireesh bogu

unread,
Oct 3, 2010, 3:54:33 AM10/3/10
to Brandon Hurr, ggplot2
> sessionInfo()
R version 2.11.1 (2010-05-31) 
i386-pc-mingw32 

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
[1] mgcv_1.6-2    ggplot2_0.8.8 proto_0.3-8   reshape_0.8.3 plyr_1.2.1   

loaded via a namespace (and not attached):
[1] digest_0.4.2       lattice_0.18-8     Matrix_0.999375-39 nlme_3.1-96  



gireesh bogu

unread,
Oct 3, 2010, 3:36:16 AM10/3/10
to Brandon Hurr, ggplot2
datax2 <- melt(datax, id.vars="Distance")

d <- ggplot(datax, aes(x=Distance, y=value))

d + stat_smooth(aes(colour = variable), size = 2, alpha = 0.2)

#################################
datax2 data is looking fine but no output

it should be this d <- ggplot(datax2, aes(x=Distance, y=value)) ??



-G

On Sun, Oct 3, 2010 at 3:29 PM, Brandon Hurr <bhi...@gmail.com> wrote:

gireesh bogu

unread,
Oct 3, 2010, 3:50:29 AM10/3/10
to Brandon Hurr, ggplot2
It is also same no warning..no output..

gireesh bogu

unread,
Oct 3, 2010, 4:13:59 AM10/3/10
to Brandon Hurr, ggplot2
thank you. some thing wrong with R. I closed it and ran the same command worked fine.

Thanx for your time!
really appreciated!
Gireesh
Reply all
Reply to author
Forward
0 new messages