[R] ggplot geom_freqpoly() layers ..?

Ista Zahn izahn at psych.rochester.edu
Thu Sep 8 17:26:14 CEST 2011

```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
> and provide commented, minimal, self-contained, reproducible code.
>

--
Ista Zahn