Hi, I was trying to overlay/combine two freqpoly plots. The sample code below illustrates the problem. Essentially, I want to do is: 1. Have the same colour for all the lines in 'Plot 1' (and 'Plot 2'). Currently, all the lines in Plot 1 have different colours and all the lines in Plot 2 have different colors. I'd like for all lines in Plot 1 to be 'red' and all the lines in Plot 2 to be 'black'. 2. Combine both the plots ('Plot 1' and 'Plot 2' as one combined plot - which I attempt to do in 'Combined Plot'). However, I'm doing something wrong because with the code for 'Combined Plot' I just get two lines. ############ sample code ############ library(ggplot2) ###### Plot 1 - normal distributions with mean = 0 ###### mat <- matrix(rnorm(10000,mean=0),1000,10) colnames(mat) <- paste('a',1:ncol(mat),sep='') rownames(mat) <- 1:nrow(mat) mat2 <- melt(mat) ggplot(mat2) + geom_freqpoly(aes(x = value, y = ..density.., colour = X2)) ###### Plot 2- normal distributions with mean = 1 tab <- matrix(rnorm(10000,mean=1),1000,10) colnames(tab) <- paste('b',1:ncol(tab),sep='') rownames(tab) <- 1:nrow(tab) tab2 <- melt(tab) ggplot(tab2) + geom_freqpoly(aes(x = value, y = ..density.., colour = X2)) ###### Combined plot comb <- cbind(mat,tab) comb2 <- melt(comb) cols <- c(rep('red',ncol(mat)*nrow(mat)),rep('black',ncol(tab)*nrow(tab))) ggplot(comb2) + geom_freqpoly(aes(x = value, y = ..density.., colour = cols)) ################### End code ############### Any help would be appreciated! thanks! [[alternative HTML version deleted]]
Hi Brian On Thu, Sep 8, 2011 at 10:30 AM, Brian Smith <bsmith030465 at gmail.com> wrote:> Hi, > > I was trying to overlay/combine two freqpoly plots. The sample code below > illustrates the problem. Essentially, I want to do is: > > 1. Have the same colour for all the lines in 'Plot 1' (and 'Plot 2').Then don't map the colour to X2!> Currently, all the lines in Plot 1 have different colours and all the lines > in Plot 2 have different colors. I'd like for all lines in Plot 1 to be > 'red' and all the lines in Plot 2 to be 'black'.You will need a variable indicating which values come from mat and which come from tab. Then map color to that variable.> 2. Combine both the plots ('Plot 1' and 'Plot 2' as one combined plot - > which I attempt to do in 'Combined Plot'). However, I'm doing something > wrong because with the code for 'Combined Plot' I just get two lines.use aes(group = X2) Best, Ista> > ############ sample code ############ > library(ggplot2) > > ###### Plot 1 - normal distributions with mean = 0 ###### > ? ?mat <- matrix(rnorm(10000,mean=0),1000,10) > ? ?colnames(mat) <- paste('a',1:ncol(mat),sep='') > ? ?rownames(mat) <- 1:nrow(mat) > ? ?mat2 <- melt(mat) > > ? ?ggplot(mat2) + geom_freqpoly(aes(x = value, > ? ? ? ? ? ? ? ? ? ?y = ..density.., colour = X2)) > > ###### Plot 2- normal distributions with mean = 1 > ? ?tab <- matrix(rnorm(10000,mean=1),1000,10) > ? ?colnames(tab) <- paste('b',1:ncol(tab),sep='') > ? ?rownames(tab) <- 1:nrow(tab) > ? ?tab2 <- melt(tab) > > ? ?ggplot(tab2) + geom_freqpoly(aes(x = value, > ? ? ? ? ? ? ? ? ? ?y = ..density.., colour = X2)) > > > ###### Combined plot > ? ?comb <- cbind(mat,tab) > ? ?comb2 <- melt(comb) > ? ?cols <- > c(rep('red',ncol(mat)*nrow(mat)),rep('black',ncol(tab)*nrow(tab))) > > ? ?ggplot(comb2) + geom_freqpoly(aes(x = value, > ? ? ? ? ? ? ? ? ? ?y = ..density.., colour = cols)) > > > ################### End code ############### > > Any help would be appreciated! > > thanks! > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org
On Thu, Sep 8, 2011 at 12:10 PM, Brian Smith <bsmith030465 at gmail.com> wrote:> Hi, > > Thanks for the reply. For the combined plot, if I use: > > ??? ggplot(comb2) + geom_freqpoly(aes(x = value, > ??? ??? ??? ??? ??? y = ..density.., group = X2)) > > > I get the same colour for both the sets of distributions.That is because you did not take my suggestion to make "a variable indicating which values come from mat and which come from tab. Then map color to that variable" This is mostly my fault for being lazy and not giving you a working example. Try this: mat2$src <- "mat" tab2$src <- "tab" comb <- rbind(mat2, tab2) ggplot(comb) + geom_freqpoly(aes(x = value, y = ..density.., group = X2, color = src)) Best, Ista What I want is one> colour for the first set of distributions, and a different colour for the > second set of distributions. Does that make sense? > > thanks! > > > > On Thu, Sep 8, 2011 at 11:26 AM, Ista Zahn <izahn at psych.rochester.edu> > wrote: >> >> Hi Brian >> >> On Thu, Sep 8, 2011 at 10:30 AM, Brian Smith <bsmith030465 at gmail.com> >> wrote: >> > Hi, >> > >> > I was trying to overlay/combine two freqpoly plots. The sample code >> > below >> > illustrates the problem. Essentially, I want to do is: >> > >> > 1. Have the same colour for all the lines in 'Plot 1' (and 'Plot 2'). >> >> Then don't map the colour to X2! >> >> > Currently, all the lines in Plot 1 have different colours and all the >> > lines >> > in Plot 2 have different colors. I'd like for all lines in Plot 1 to be >> > 'red' and all the lines in Plot 2 to be 'black'. >> >> You will need a variable indicating which values come from mat and >> which come from tab. Then map color to that variable. >> >> > 2. Combine both the plots ('Plot 1' and 'Plot 2' as one combined plot - >> > which I attempt to do in 'Combined Plot'). However, I'm doing something >> > wrong because with the code for 'Combined Plot' I just get two lines. >> >> use aes(group = X2) >> >> Best, >> Ista >> >> > >> > ############ sample code ############ >> > library(ggplot2) >> > >> > ###### Plot 1 - normal distributions with mean = 0 ###### >> > ? ?mat <- matrix(rnorm(10000,mean=0),1000,10) >> > ? ?colnames(mat) <- paste('a',1:ncol(mat),sep='') >> > ? ?rownames(mat) <- 1:nrow(mat) >> > ? ?mat2 <- melt(mat) >> > >> > ? ?ggplot(mat2) + geom_freqpoly(aes(x = value, >> > ? ? ? ? ? ? ? ? ? ?y = ..density.., colour = X2)) >> > >> > ###### Plot 2- normal distributions with mean = 1 >> > ? ?tab <- matrix(rnorm(10000,mean=1),1000,10) >> > ? ?colnames(tab) <- paste('b',1:ncol(tab),sep='') >> > ? ?rownames(tab) <- 1:nrow(tab) >> > ? ?tab2 <- melt(tab) >> > >> > ? ?ggplot(tab2) + geom_freqpoly(aes(x = value, >> > ? ? ? ? ? ? ? ? ? ?y = ..density.., colour = X2)) >> > >> > >> > ###### Combined plot >> > ? ?comb <- cbind(mat,tab) >> > ? ?comb2 <- melt(comb) >> > ? ?cols <- >> > c(rep('red',ncol(mat)*nrow(mat)),rep('black',ncol(tab)*nrow(tab))) >> > >> > ? ?ggplot(comb2) + geom_freqpoly(aes(x = value, >> > ? ? ? ? ? ? ? ? ? ?y = ..density.., colour = cols)) >> > >> > >> > ################### End code ############### >> > >> > Any help would be appreciated! >> > >> > thanks! >> > >> > ? ? ? ?[[alternative HTML version deleted]] >> > >> > ______________________________________________ >> > R-help at r-project.org mailing list >> > https://stat.ethz.ch/mailman/listinfo/r-help >> > PLEASE do read the posting guide >> > http://www.R-project.org/posting-guide.html >> > and provide commented, minimal, self-contained, reproducible code. >> > >> >> >> >> -- >> Ista Zahn >> Graduate student >> University of Rochester >> Department of Clinical and Social Psychology >> http://yourpsyche.org > >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org