[R] Multiple area plots to share the same x-axis

Werner Heijstek w.heijstek at gmail.com
Wed Mar 30 16:54:03 CEST 2011


I'm going to go ahead and shamelessly bump this question up the list.
I saw that out of the 34 posts yesterday, only 9 did not receive an
answer. Perhaps someone who finds this question trivial is checking
his e-mail right now :)

Werner

On Tue, Mar 29, 2011 at 10:20 AM, jovian <w.heijstek at gmail.com> wrote:
> Hello,
>
> I asked a similar question before but in an existing thread. I am not sure
> if it is proper etiquette to repost a similar question as a new tread, I
> think in this case, it might be because this way more people can see it and
> perhaps learn from it. (Also, part of the existing thread became private)
>
> I want to know how to plot multiple ggplot area plots on top of one another
> so that the same x-axis is shared?
>
> This solution simply stitches multiple plots on top of each other:
>
> vp.layout <- function(x, y) viewport(layout.pos.row=x, layout.pos.col=y)
> arrange <- function(..., nrow=NULL, ncol=NULL, as.table=FALSE) {
>  dots <- list(...)
>  n <- length(dots)
>  if(is.null(nrow) & is.null(ncol)) { nrow = floor(n/2) ; ncol =
> ceiling(n/nrow)}
>  if(is.null(nrow)) { nrow = ceiling(n/ncol)}
>  if(is.null(ncol)) { ncol = ceiling(n/nrow)}
>        ## NOTE see n2mfrow in grDevices for possible alternative
> grid.newpage()
> pushViewport(viewport(layout=grid.layout(nrow,ncol) ) )
>  ii.p <- 1
>  for(ii.row in seq(1, nrow)){
>  ii.table.row <- ii.row
>  if(as.table) {ii.table.row <- nrow - ii.table.row + 1}
>  for(ii.col in seq(1, ncol)){
>   ii.table <- ii.p
>   if(ii.p > n) break
>   print(dots[[ii.table]], vp=vp.layout(ii.table.row, ii.col))
>   ii.p <- ii.p + 1
>  }
>  }
> }
>
> set <- read.table(file="http://www.jovian.nl/set.csv", head=1,  sep=",")
> set2 <- read.table(file="http://www.jovian.nl/set2.csv", head=1,  sep=",")
> library(ggplot2)
> s <- ggplot(set, aes(x = time, y = hours)) + geom_area(colour = 'red', fill
> = 'red', alpha = 0.5) +
>     geom_area(stat = 'smooth', span = 0.2, alpha = 0.3) + ylim(0,40)
> s1 <- ggplot(set2, aes(x = time, y = hours)) + geom_area(colour = 'red',
> fill = 'red', alpha = 0.5) +
>     geom_area(stat = 'smooth', span = 0.2, alpha = 0.3) + ylim(0,40)
> arrange(s,s1,ncol=1)
>
>
> The arrange() function was taken from
> http://gettinggeneticsdone.blogspot.com/2010/03/arrange-multiple-ggplot2-plots-in-same.html.
> In this example, the x-axes are only similar because the data sets have the
> same range. In effect nothing more happens than that two images are plotted
> on top of one another. Now how to "merge" these two (and later more) area
> plots on top of each other so that they share the same x-axis (so that only
> one x-axis would be necessary on the bottom of the plot)?
>
> Thanks,
>
> Werner
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Multiple-area-plots-to-share-the-same-x-axis-tp3414050p3414050.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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