[R] Arranging ggplot2 objects with ggplotGrob()
H
@gent@ @end|ng |rom medd@t@|nc@com
Wed Jul 29 02:32:34 CEST 2020
On 07/28/2020 12:35 PM, Ulrik Stervbo wrote:
> Would this work:
>
> ```
> library(ggplot2)
> library(cowplot)
>
> p1 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point()
> p2 <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width * 1000)) + geom_point()
>
> plot_grid(p1, p2, ncol = 1, align = "hv", rel_heights = c(2, 1))
> ```
>
> Best,
> Ulrik
>
> On 2020-07-24 21:58, Bert Gunter wrote:
>> ?grid.frame, etc. should be straightforward for this I would think.
>> But of course you have to resort to the underlying grid framework rather
>> than the ggplot2 interface.
>>
>> Bert Gunter
>>
>> "The trouble with having an open mind is that people keep coming along and
>> sticking things into it."
>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>>
>>
>> On Fri, Jul 24, 2020 at 12:11 PM H <agents using meddatainc.com> wrote:
>>
>>> On 07/24/2020 02:50 PM, H wrote:
>>> > On 07/24/2020 02:03 PM, Jeff Newmiller wrote:
>>> >> The set of people interested in helping when you supply a minimal
>>> reproducible example is rather larger than the set of people willing to
>>> read the documentation for you (hint) and guess what aspect of alignment
>>> you are having trouble with.
>>> >>
>>> >> On July 24, 2020 10:46:57 AM PDT, H <agents using meddatainc.com> wrote:
>>> >>> On 07/24/2020 01:14 PM, John Kane wrote:
>>> >>>> <i>Well, I am not looking for help debugging my code but for
>>> >>> information to better understand arranging plots vertically. The code
>>> >>> above aligns them horizontally as expected.</i>
>>> >>>> Sigh, we know the code works but we do not know what the plots are
>>> >>> and we cannot play around with them to see if we can help you if we
>>> >>> have nothing to work with.
>>> >>>> On Fri, 24 Jul 2020 at 12:12, H <agents using meddatainc.com
>>> >>> <mailto:agents using meddatainc.com>> wrote:
>>> >>>> On 07/24/2020 05:29 AM, Erich Subscriptions wrote:
>>> >>>> > Hav a look at the packages cowplot and patchwork
>>> >>>> >
>>> >>>> >> On 24.07.2020, at 02:36, H <agents using meddatainc.com
>>> >>> <mailto:agents using meddatainc.com>> wrote:
>>> >>>> >>
>>> >>>> >> I am trying to arrange two plots vertically, ie plot 2 below
>>> >>> plot 1, where I want the plots to align columnwise but have a height
>>> >>> ratio of eg 3:1.
>>> >>>> >>
>>> >>>> >> My attempts so far after consulting various webpages is that
>>> >>> the following code aligns them columnwise correctly but I have, so far,
>>> >>> failed in setting the relative heights...
>>> >>>> >>
>>> >>>> >> g2<-ggplotGrob(s)
>>> >>>> >> g3<-ggplotGrob(v)
>>> >>>> >> g<-rbind(g2, g3, size = "first")
>>> >>>> >> g$widths<-unit.pmax(g2$widths, g3$widths)
>>> >>>> >>
>>> >>>> >> what would the appropriate statement for the relative heights
>>> >>> to add here be?
>>> >>>> >>
>>> >>>> >> grid.newpage()
>>> >>>> >> grid.draw(g)
>>> >>>> >>
>>> >>>> >> Thank you!
>>> >>>> >>
>>> >>>> >> ______________________________________________
>>> >>>> >> R-help using r-project.org <mailto:R-help using r-project.org> mailing
>>> >>> list -- To UNSUBSCRIBE and more, see
>>> >>>> >> 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.
>>> >>>> So this is not possible without using one of those two packages?
>>> >>> I got the impression I should be able to use grid.arrange to do so but
>>> >>> was not able to get it to work without disturbing the width alignment
>>> >>> above...
>>> >>>> ______________________________________________
>>> >>>> R-help using r-project.org <mailto:R-help using r-project.org> mailing list
>>> >>> -- To UNSUBSCRIBE and more, see
>>> >>>> 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.
>>> >>>> --
>>> >>>> John Kane
>>> >>>> Kingston ON Canada
>>> >>> No need to play around with anything. I am simply looking for
>>> >>> assistance on how to use eg arrangeGrob to not only align two plots
>>> >>> columnwise but also adjust their heights relative to each other rather
>>> >>> than 1:1.
>>> >>>
>>> >>> Can arrangeGrob() be used for that?
>>> >>>
>>> >>>
>>> >>> [[alternative HTML version deleted]]
>>> >>>
>>> >>> ______________________________________________
>>> >>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> >>> 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.
>>> >
>>> > Look at
>>> https://cran.r-project.org/web/packages/egg/vignettes/Ecosystem.html
>>> where there are two mpg charts, one above the other. What would I need to
>>> add to:
>>> >
>>> > |library(gtable) g2 <-ggplotGrob(p2) g3 <-ggplotGrob(p3) g <-rbind(g2,
>>> g3, size = "first") g$widths <-unit.pmax(g2$widths, g3$widths)
>>> grid.newpage() grid.draw(g) |
>>> >
>>> > |to make the second chart 1/2 the size of the top one?|
>>> >
>>> > ||
>>> >
>>> The following code aligns the two plot areas of the two charts perfectly
>>> but they are the same height whereas I want to make the bottom one 1/2 as
>>> tall as the top one:
>>>
>>> g2<-ggplotGrob(s)
>>> g3<-ggplotGrob(v)
>>> g<-rbind(g2, g3, size = "first")
>>> g$widths<-unit.pmax(g2$widths, g3$widths)
>>> grid.newpage()
>>> grid.draw(g)
>>>
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> 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.
>>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
That worked nicely but then the x-axis legend on the top graph needs to be blank. My recollection of trying this a few days ago was that I was left with blank space there...
So, going back to arrangeGrob(), how would one set relative heights while maintaining aligned plot areas?
More information about the R-help
mailing list