Search code examples
rplotggplot2kernel-densitydensity-plot

ggplot2: line up x limits on two density plots


I've got a series of density estimates that I would like to plot to compare in ggplot2. I'm not attached to any of the particulars I have chosen so far (e.g. should these be all on one plot, should I use facets, grid.arrange, etc) I am open to suggestions:

First Attempt:

p1<-ggplot(data, aes(TPM,fill=col))+scale_x_log10()+scale_fill_brewer(type="div") + geom_density(alpha=.7) 
p2<-ggplot(data, aes(RPKM,fill=col))+scale_x_log10()+scale_fill_brewer(type="div") + geom_density(alpha=.7) 
grid.arrange(p1,p2,ncol=1)

enter image description here

Good, but I would like the axis to be the same so that they are comparable.

I tried setting the limits using coord_cartesian(xlim=c(0,5)) but I get errors such as

Error in seq.default(min, max, by = by) : 
  'from' cannot be NA, NaN or infinite

I also tried setting the limits in the scale_x_log10(limits=c(0,5) but I get

Error in seq.default(range[1], range[2], length = 200) : 
  'from' cannot be NA, NaN or infinite
Error in exists(name, envir = env, mode = mode) : 
  argument "env" is missing, with no default

Is there a better way to line up these graphs so that they are more easily comparable? I would be up for any solution.

My data is of this form:

         RPKM         TPM col
1 0.129335235 0.602873040   3
2 0.395073341 1.724916453   4
3 0.004909958 0.003465248   1
4 0.466517328 0.557687694   1
5 0.522773169 0.486767563   1
6 0.179694966 0.180683888   1

Solution

  • If you do a wide to long transformation on the data frame, you can use ggplot facets for your plots. By default, you'll have the same scales for x & y unless you override them. I generated some data for the example below):

    library(ggplot2)
    library(reshape2)
    library(gridExtra)
    
    set.seed(1492)
    data <- data.frame(RPKM=runif(2000, min=0, max=1),
                       TPM=runif(2000, min=0, max=1),
                       col=factor(sample(1:9, 2000, replace=TRUE)))
    
    data_m <- melt(data)
    data_m$col <- factor(data_m$col) # need to refactor "col"
    
    gg <- ggplot(data_m)
    gg <- gg + geom_density(aes(value, fill=col), alpha=.7) 
    gg <- gg + scale_fill_brewer(type="div")
    gg <- gg + facet_wrap(~variable, ncol=1)
    gg
    

    enter image description here