[R] A problem of splitting the right screen in 3 or more independent vertical boxes:

Aldi Kraja aldi at wustl.edu
Sat May 11 16:01:48 CEST 2013


For those that may have this question in the future, here are two solutions:
As suggested from David and Sarah,
One has to remove par function from defining screen splits, instead use 
layout function.
For example:
  layout(matrix(c(1,1,2,3),2,2,byrow=T))
which says, split the screen in 4 blocks, of them use block space 1 and 
2 oriented by row for picture 1, and the two remaining blocks for 
picture 2 and picture 3. Similarly, one can split the screen into 6, 8 
blocks and so on by changing also how many blocks of space you want to 
assign to a specific picture for example.
layout(matrix(c(1,1,1,2,3,4,5),2,3,byrow=T)) ## six splits, of those 
first 3 belong to picture 1
layout(matrix(c(1,1,2,3,4,5,6,7),2,4,byrow=T))  ## 8 splits of those 
first 2 belong to picture 1 and so on.

Dennis Murphey provided also another beautiful solution via ggplot2. See 
following.
Thank you,
Aldi
On 5/3/2013 7:57 PM, Dennis Murphy wrote:
> Hi:
>
> Here are a couple of ways you could go using the ggplot2 and gridExtra
> packages, but it requires that you rearrange your data.
>
> ggplot2 prefers data frame input, so we convert the histogram data to
> a simple data frame and apply a couple of tricks to get an appropriate
> data frame for the box plot data.
>
> # Cast x into a data frame
> DF0 <- data.frame(x = rnorm(1000,mean=0,sd=1))
>
>
> wheat1 = rnorm(100,mean=0,sd=1)
> wheat2 = rnorm(150,mean=0,sd=2)
> tomatos3 = rnorm(200,mean=0,sd=3)
> tomatos4 = rnorm(250,mean=0,sd=4)
> cucumbers5 <- rnorm(300,mean=0,sd=5)
> cucumbers6 <- rnorm(400,mean=0,sd=6)
>
> # commodity is defined so that products are paired
> DF1 <- data.frame(
>    commodity = factor(rep(c("wheat", "tomatos", "cucumbers"), c(250, 450, 700))),
>    product = factor(rep(c("wheat1", "wheat2", "tomatos3", "tomatos4",
>                           "cucumbers5", "cucumbers6"),
>                         times = c(100, 150, 200, 250, 300, 400))),
>    value = c(wheat1, wheat2, tomatos3, tomatos4, cucumbers5, cucumbers6) )
>
> library(ggplot2)
>
> # Simple histogram with ggplot2
> p0 <- ggplot(DF0, aes(x = x)) + geom_histogram()
>
> # Paired box plots by commodity
> p1 <- ggplot(DF1, aes(x = product, y = value)) +
>            geom_boxplot() +
>            facet_wrap( ~ commodity, scales = "free_x", ncol = 3)
>
> # gridExtra lets you combine multiple grid graphics plots together
> # (ggplot2 and lattice are both written in grid)
> library(gridExtra)
> grid.arrange(p0, p1, nrow = 2)   # stack histogram on top of boxplots
> grid.arrange(p0, p1, nrow = 1)   # what you asked for
>
> I prefer the former, but it's easy enough to fix the problems with the latter.
>
> Dennis


On 5/3/2013 6:07 PM, David Winsemius wrote:
> On May 3, 2013, at 3:21 PM, Sarah Goslee wrote:
>
>> Hi Aldi,
>>
>> You might want
>> ?layout
>> instead.
>>
> Indeed. In particular a matrix argument might be:
>
> matrix(c(1,2,3, 4,4,4)
>
>
>> Sarah
>>
>> On Fri, May 3, 2013 at 5:54 PM, Aldi Kraja <aldi at wustl.edu> wrote:
>>> Hmm,
>>> I had a typo paste by mistake in my x vector
>>> It has to be:
>>>
>>> x<-rnorm(1000,mean=0,sd=1)
>>> wheat1<-rnorm(100,mean=0,sd=1)
>>> wheat2<-rnorm(150,mean=0,sd=2)
>>> tomatos3<-rnorm(200,mean=0,sd=3)
>>> tomatos4<-rnorm(250,mean=0,sd=4)
>>> cucumbers5<-rnorm(300,mean=0,sd=5)
>>> cucumbers6<-rnorm(400,mean=0,sd=6)
>>> par(mfrow=c(1,2))
>>>
>>> hist(x, main="Left screen OK")
>>>
>>> boxplot(wheat1,wheat2,tomatos3,tomatos4,cucumbers5,cucumbers6)
> I think you will need a separate call to boxplot for each grouping. The `boxplot` function will nto be able to access the device specifications.
>
>


-- 
--------------------------------------------------------------------------------
Aldi T. Kraja, DSc, PhD        aldi at dsgmail.wustl.edu
Research Associate Professor of Genetics
Division of Statistical Genomics http://dsgweb.wustl.edu/aldi
Washington University School of Medicine,    Phone:(314)362-2498 Fax:(314)362-4227
4444 Forest Park Blvd., Campus Box 8506   Saint Louis, MO, 63110
_______________________________________________________________________________
The materials contained in this e-mail are private and confidential and are the property  of the sender. If you are not the intended recipient, be advised that any unauthorized use, disclosure, copying, distribution, or the taking of any action in reliance on the contents of this information is strictly prohibited. If you have received this e-mail transmission in error, please immediately notify the sender.



More information about the R-help mailing list