[R] Overlay Histogram

baptiste auguie baptiste.auguie at gmail.com
Thu Aug 9 03:01:01 CEST 2012


Hi,

On 9 August 2012 08:40, li li <hannah.hlx at gmail.com> wrote:
> Dear all,
>    I have a few extra questions regarding this. Below is my code.
> My questions are:
>  (1), How can I remove the labels, tick marks and numbers, and the word
> "density" for the histgrams.
>   (2) On the top right corner, there is a empty grid. How can I remove it.

You don't have to make a dummy plot; either replace it with a
nullGrob(), or simply use the as.table argument,

grid.arrange(none, yhist, xhist, ncol=2, as.table=TRUE, widths=c(3,
1), heights=c(1, 3))


HTH,

b.
>
> Thank you in advance.
>       Hannah
>
>
>
> require(ggplot2)
> require(gridExtra)
> x <- rnorm(10000,10, 2)
> y <- rnorm(10000, 20, 6)
> ##x<-xbar
> ##y <- ybar
> xy<-data.frame(x,y)
>
> xhist  <-  ggplot(xy, aes(x=x)) +
>         geom_histogram(aes(y=..density..), fill="red", colour="black")+
>         geom_density(colour="black", adjust=4) +
>         opts(title="")
> yhist  <-  ggplot(xy, aes(x=y)) +
>         geom_histogram(aes(y=..density..), fill="blue", colour="black")+
>         geom_density(colour="black", adjust=4) +
>         opts(title="") +
>         coord_flip()
> none <- qplot(x,y, data=xy) + geom_blank()
>
> empty <- ggplot()+geom_point(aes(1,1), colour="white")+
> opts(axis.ticks=theme_blank(),
> panel.background=theme_blank(),
> axis.text.x=theme_blank(),
> axis.text.y=theme_blank(),
> axis.title.x=theme_blank(), axis.title.y=theme_blank())
> grid.arrange(xhist, empty, none, yhist, ncol=2, nrow=2, widths=c(3, 1),
>  heights=c(1, 3))
>
>
>
>
>
>
>
>
>
>
> 2012/8/7 li li <hannah.hlx at gmail.com>
>
>> Please ignore my previous message. The limits are correct.
>>     Thanks again.
>>       Hannah
>>
>> 2012/8/7 li li <hannah.hlx at gmail.com>
>>
>>> Hi John,
>>>   Is it possible to set the xlim and ylim in the histograms. It seems
>>> that the limits are not the true ranges.
>>>   Thanks.
>>>      Hannah
>>>
>>>  2012/8/7 li li <hannah.hlx at gmail.com>
>>>
>>>> Thanks John.
>>>>
>>>>
>>>> 2012/8/7 John Kane <jrkrideau at inbox.com>
>>>>
>>>>> I cannot see any obvious way to do this.  Ben Gunter's suggestion re
>>>>> layout makes sense.  Here is a version using grid and ggplot2.  Note I
>>>>> shamelessly stole code to due it.
>>>>>
>>>>>
>>>>> library(ggplot2)
>>>>> library(grid)
>>>>>
>>>>> dd <- data.frame(x =  rnorm(1000, 10, 10),
>>>>>                    y =  rnorm(1000, 3, 20))
>>>>> #  From https://stat.ethz.ch/pipermail/r-help/2011-June/280588.html
>>>>>
>>>>> p1  <-  ggplot(dd, aes(x=x)) +
>>>>>         geom_histogram(aes(y=..density..), fill="red", colour="black")+
>>>>>         geom_density(colour="black", adjust=4) +
>>>>>         opts(title="Normal Random Sample")
>>>>>
>>>>> p2  <-  ggplot(dd, aes(x=y)) +
>>>>>         geom_histogram(aes(y=..density..), fill="blue", colour="black")+
>>>>>         geom_density(colour="black", adjust=4) +
>>>>>         opts(title="Normal Random Sample") +
>>>>>         coord_flip()
>>>>>
>>>>> # From StackOverflow
>>>>> http://stackoverflow.com/questions/9490482/combined-plot-of-ggplot2-not-in-a-single-plot-using-par-or-layout-functio
>>>>>
>>>>> # this simplifies the vp statement
>>>>> # otherwise we would have to use something like
>>>>> # print(plot5 , vp = viewport(layout.pos.row = 2, layout.pos.col = 1:2))
>>>>> # and so on for the rest of the plots.
>>>>> vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col
>>>>> = y)
>>>>>
>>>>> grid.newpage()
>>>>> pushViewport(viewport(layout = grid.layout(2, 1)))
>>>>> print(p1, vp = vplayout(1, 1))
>>>>> print(p2, vp = vplayout(2, 1))
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> John Kane
>>>>> Kingston ON Canada
>>>>>
>>>>>
>>>>> > -----Original Message-----
>>>>> > From: hannah.hlx at gmail.com
>>>>> > Sent: Mon, 6 Aug 2012 15:40:55 -0400
>>>>> > To: r-help at r-project.org
>>>>> > Subject: [R] Overlay Histogram
>>>>> >
>>>>> > Dear all,
>>>>> >   For two sets of random variables, say, x <-  rnorm(1000, 10, 10)
>>>>> and  y
>>>>> > <- rnorm(1000. 3, 20).
>>>>> > Is there any way to overlay the histograms (and density curves) of x
>>>>> and
>>>>> > y
>>>>> > on the plot of y vs. x?
>>>>> > The histogram of x is on the x axis and that of y is on the y axis.
>>>>> >   The density curve here is to approximate the shape of the
>>>>> distribution
>>>>> > and does not have to have area 1.
>>>>> >    Thank you in advance.
>>>>> >       Hannah
>>>>> >
>>>>> >       [[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<http://www.r-project.org/posting-guide.html>
>>>>> > and provide commented, minimal, self-contained, reproducible code.
>>>>>
>>>>> ____________________________________________________________
>>>>> FREE ONLINE PHOTOSHARING - Share your photos online with your friends
>>>>> and family!
>>>>> Visit http://www.inbox.com/photosharing to find out more!
>>>>>
>>>>>
>>>>>
>>>>
>>>
>>
>
>         [[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