[R] ggplot2 reorder factors for faceting

Dennis Murphy djmuser at gmail.com
Tue Nov 8 20:11:39 CET 2011


Hi:

(1) Here is one way to reorganize the levels of a factor:
plotData[['infection']] <- factor(plotData[['infection']],
                               levels = c('InfA', 'InfC', 'InfB', 'InfD'))

Do this ahead of the call to ggplot(), preferably after plotData is defined.

relevel() resets the baseline category of a factor, but here you want
to make multiple changes.

(2) You probably want a better title for the legend. Assuming you want
'Scale' as the title, you can add the following to labs:

labs(..., fill = 'Scale')

HTH,
Dennis


On Tue, Nov 8, 2011 at 3:51 AM, Iain Gallagher
<iaingallagher at btopenworld.com> wrote:
>
>
> Dear List
>
> I am trying to draw a heatmap using ggplot2. In this heatmap I have faceted my data by 'infection' of which I have four. These four infections break down into two types and I would like to reorder the 'infection' column of my data to reflect this.
>
> Toy example below:
>
> library(ggplot2)
>
> # test data for ggplot reordering
> genes <- (rep (c(rep('a',4), rep('b',4), rep('c',4), rep('d',4), rep('e',4), rep('f',4)) ,4))
> fcData <- rnorm(96)
> times <- rep(rep(c(2,6,24,48),6),4)
> infection <- c(rep('InfA', 24), rep('InfB', 24), rep('InfC', 24), rep('InfD', 24))
> infType <- c(rep('M', 24), rep('D',24), rep('M', 24), rep('D', 24))
>
> # data is long format for ggplot2
> plotData <- as.data.frame(cbind(genes, as.numeric(fcData), as.numeric(times), infection, infType))
>
> hp2 <- ggplot(plotData, aes(factor(times), genes)) + geom_tile(aes(fill = scale(as.numeric(fcData)))) + facet_wrap(~infection, ncol=4)
>
> # set scale
> hp2 <- hp2 + scale_fill_gradient2(name=NULL, low="#0571B0", mid="#F7F7F7", high="#CA0020", midpoint=0, breaks=NULL, labels=NULL, limits=NULL, trans="identity")
>
> # set up text (size, colour etc etc)
> hp2 <- hp2 + labs(x = "Time", y = "") + scale_y_discrete(expand = c(0, 0)) + opts(axis.ticks = theme_blank(), axis.text.x = theme_text(size = 10, angle = 360, hjust = 0, colour = "grey25"), axis.text.y = theme_text(size=10, colour = 'gray25'))
>
> hp2 <- hp2 + theme_bw()
>
> In the resulting plot I would like infections infA and infC plotted next to each other and likewise for infB and infD. I have a column in the data - infType - which I could use to reorder the infection column but so far I have no luck getting this to work.
>
> Could someone give me a pointer to the best way to reorder the infection factor and accompanying data into the order I would like?
>
> Best
>
> iain
>
>> sessionInfo()
> R version 2.13.2 (2011-09-30)
> Platform: x86_64-pc-linux-gnu (64-bit)
>
> locale:
>  [1] LC_CTYPE=en_GB.utf8       LC_NUMERIC=C
>  [3] LC_TIME=en_GB.utf8        LC_COLLATE=en_GB.utf8
>  [5] LC_MONETARY=C             LC_MESSAGES=en_GB.utf8
>  [7] LC_PAPER=en_GB.utf8       LC_NAME=C
>  [9] LC_ADDRESS=C              LC_TELEPHONE=C
> [11] LC_MEASUREMENT=en_GB.utf8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] grid      stats     graphics  grDevices utils     datasets  methods
> [8] base
>
> other attached packages:
> [1] ggplot2_0.8.9 proto_0.3-9.2 reshape_0.8.4 plyr_1.6
>
> loaded via a namespace (and not attached):
> [1] digest_0.5.0 tools_2.13.2
>>
>
> ______________________________________________
> 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