[R] ggplot2 freqpoly() layers..?

Dennis Murphy djmuser at gmail.com
Thu Sep 8 22:38:13 CEST 2011


Hi:

You could try something like this:

# rbind the two melted data frames:
comb <- rbind(mat2, tab2)
# create a group factor to distinguish the two data sets
comb <- mutate(comb, gp = factor(rep(1:2, each = 10000)))

# Generate the plot:
 ggplot(comb) +
    geom_freqpoly(aes(x = value, y = ..density.., group = X2, colour = gp)) +
    scale_colour_manual('Group', values = c('1' = 'orange', '2' = 'blue'))

The group = argument allows for separate density estimates for each
variable, colour distinguishes the groups. Within one of the melted
data frames, you can set a color by defining it outside the aes()
statement; e.g., for tab2,

ggplot(tab2) + geom_freqpoly(aes(x = value,
                    y = ..density.., group = X2), colour = 'blue')

HTH,
Dennis


On Thu, Sep 8, 2011 at 7:33 AM, Tim Smith <tim_smith_666 at yahoo.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').
> 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]]
>
>
> ______________________________________________
> 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.
>
>



More information about the R-help mailing list