Arranging ggplot2 objects with ggplotGrob()

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
22 messages Options
12
H-2
Reply | Threaded
Open this post in threaded view
|

Arranging ggplot2 objects with ggplotGrob()

H-2
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!

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Arranging ggplot2 objects with ggplotGrob()

John Kane-3
We reallly need to see more code (a minimum working example and some data.

For some suggestions on how to do this see

 http://adv-r.had.co.nz/Reproducibility.html

http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

On Fri, 24 Jul 2020 at 01:16, H <[hidden email]> 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!
>
> ______________________________________________
> [hidden email] 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

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
H-2
Reply | Threaded
Open this post in threaded view
|

Re: Arranging ggplot2 objects with ggplotGrob()

H-2
On 07/24/2020 10:16 AM, John Kane wrote:

> We reallly need to see more code (a minimum working example and some data.
>
> For some suggestions on how to do this see
>
>  http://adv-r.had.co.nz/Reproducibility.html
>
> http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
>
> On Fri, 24 Jul 2020 at 01:16, H <[hidden email] <mailto:[hidden email]>> 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!
>
>     ______________________________________________
>     [hidden email] <mailto:[hidden email]> 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

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.


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
H-2
Reply | Threaded
Open this post in threaded view
|

Re: Arranging ggplot2 objects with ggplotGrob()

H-2
In reply to this post by H-2
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 <[hidden email]> 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!
>>
>> ______________________________________________
>> [hidden email] 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...

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Arranging ggplot2 objects with ggplotGrob()

John Kane-3
<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 <[hidden email]> 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 <[hidden email]> 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!
> >>
> >> ______________________________________________
> >> [hidden email] 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...
>
> ______________________________________________
> [hidden email] 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

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
H-2
Reply | Threaded
Open this post in threaded view
|

Re: Arranging ggplot2 objects with ggplotGrob()

H-2
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 <[hidden email] <mailto:[hidden email]>> 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 <[hidden email] <mailto:[hidden email]>> 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!
>     >>
>     >> ______________________________________________
>     >> [hidden email] <mailto:[hidden email]> 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...
>
>     ______________________________________________
>     [hidden email] <mailto:[hidden email]> 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Arranging ggplot2 objects with ggplotGrob()

Jeff Newmiller
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 <[hidden email]> 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 <[hidden email]
><mailto:[hidden email]>> 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 <[hidden email]
><mailto:[hidden email]>> 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!
>>     >>
>>     >> ______________________________________________
>>     >> [hidden email] <mailto:[hidden email]> 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...
>>
>>     ______________________________________________
>>     [hidden email] <mailto:[hidden email]> 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]]
>
>______________________________________________
>[hidden email] 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.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] 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.
H-2
Reply | Threaded
Open this post in threaded view
|

Re: Arranging ggplot2 objects with ggplotGrob()

H-2
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 <[hidden email]> 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 <[hidden email]
>> <mailto:[hidden email]>> 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 <[hidden email]
>> <mailto:[hidden email]>> 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!
>>>     >>
>>>     >> ______________________________________________
>>>     >> [hidden email] <mailto:[hidden email]> 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...
>>>     ______________________________________________
>>>     [hidden email] <mailto:[hidden email]> 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]]
>>
>> ______________________________________________
>> [hidden email] 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?|

||


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Arranging ggplot2 objects with ggplotGrob()

R help mailing list-2
What about cowplot?
https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html

    On Friday, July 24, 2020, 11:51:17 AM PDT, H <[hidden email]> 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 <[hidden email]> 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 <[hidden email]
>> <mailto:[hidden email]>> 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 <[hidden email]
>> <mailto:[hidden email]>> 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!
>>>    >>
>>>    >> ______________________________________________
>>>    >> [hidden email] <mailto:[hidden email]> 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...
>>>    ______________________________________________
>>>    [hidden email] <mailto:[hidden email]> 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]]
>>
>> ______________________________________________
>> [hidden email] 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?|

||


    [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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]]

______________________________________________
[hidden email] 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.
H-2
Reply | Threaded
Open this post in threaded view
|

Re: Arranging ggplot2 objects with ggplotGrob()

H-2
In reply to this post by H-2
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 <[hidden email]> 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 <[hidden email]
>>> <mailto:[hidden email]>> 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 <[hidden email]
>>> <mailto:[hidden email]>> 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!
>>>>     >>
>>>>     >> ______________________________________________
>>>>     >> [hidden email] <mailto:[hidden email]> 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...
>>>>     ______________________________________________
>>>>     [hidden email] <mailto:[hidden email]> 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]]
>>>
>>> ______________________________________________
>>> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Arranging ggplot2 objects with ggplotGrob()

Bert Gunter-2
?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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]
> >>> <mailto:[hidden email]>> 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 <[hidden email]
> >>> <mailto:[hidden email]>> 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!
> >>>>     >>
> >>>>     >> ______________________________________________
> >>>>     >> [hidden email] <mailto:[hidden email]> 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...
> >>>>     ______________________________________________
> >>>>     [hidden email] <mailto:[hidden email]> 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]]
> >>>
> >>> ______________________________________________
> >>> [hidden email] 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Arranging ggplot2 objects with ggplotGrob()

Rui Barradas
In reply to this post by R help mailing list-2
Hello,

I've just tried it.

library(ggplot2)
#library(grid)
library(cowplot)

s <- ggplot(data = subset(iris, Species == 'virginica'),
aes(Sepal.Length, Sepal.Width)) + geom_point()
v <- ggplot(data = subset(iris, Species == 'setosa'), aes(Sepal.Length,
Sepal.Width)) + geom_point()

#g2 <- ggplotGrob(s)
#g3 <- ggplotGrob(v)
#g <- rbind(g2, g3, size = "first")

cowplot::plot_grid(s, v, align = "h", nrow = 2, rel_heights = 2:1)


Hope this helps,

Rui Barradas

Às 19:57 de 24/07/2020, Felipe Carrillo via R-help escreveu:

> What about cowplot?
> https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html
>
>      On Friday, July 24, 2020, 11:51:17 AM PDT, H <[hidden email]> 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 <[hidden email]> 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 <[hidden email]
>>> <mailto:[hidden email]>> 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 <[hidden email]
>>> <mailto:[hidden email]>> 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!
>>>>      >>
>>>>      >> ______________________________________________
>>>>      >> [hidden email] <mailto:[hidden email]> 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...
>>>>      ______________________________________________
>>>>      [hidden email] <mailto:[hidden email]> 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]]
>>>
>>> ______________________________________________
>>> [hidden email] 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?|
>
> ||
>
>
>      [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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]]
>
> ______________________________________________
> [hidden email] 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.


--
Este e-mail foi verificado em termos de vírus pelo software antivírus Avast.
https://www.avast.com/antivirus

______________________________________________
[hidden email] 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.
H-2
Reply | Threaded
Open this post in threaded view
|

Re: Arranging ggplot2 objects with ggplotGrob()

H-2
On 07/24/2020 05:56 PM, Rui Barradas wrote:

> Hello,
>
> I've just tried it.
>
> library(ggplot2)
> #library(grid)
> library(cowplot)
>
> s <- ggplot(data = subset(iris, Species == 'virginica'), aes(Sepal.Length, Sepal.Width)) + geom_point()
> v <- ggplot(data = subset(iris, Species == 'setosa'), aes(Sepal.Length, Sepal.Width)) + geom_point()
>
> #g2 <- ggplotGrob(s)
> #g3 <- ggplotGrob(v)
> #g <- rbind(g2, g3, size = "first")
>
> cowplot::plot_grid(s, v, align = "h", nrow = 2, rel_heights = 2:1)
>
>
> Hope this helps,
>
> Rui Barradas
>
> Às 19:57 de 24/07/2020, Felipe Carrillo via R-help escreveu:
>> What about cowplot?
>> https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html
>>
>>      On Friday, July 24, 2020, 11:51:17 AM PDT, H <[hidden email]> 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 <[hidden email]> 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 <[hidden email]
>>>> <mailto:[hidden email]>> 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 <[hidden email]
>>>> <mailto:[hidden email]>> 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!
>>>>>      >>
>>>>>      >> ______________________________________________
>>>>>      >> [hidden email] <mailto:[hidden email]> 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...
>>>>>      ______________________________________________
>>>>>      [hidden email] <mailto:[hidden email]> 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]]
>>>>
>>>> ______________________________________________
>>>> [hidden email] 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?|
>>
>> ||
>>
>>
>>      [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] 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]]
>>
>> ______________________________________________
>> [hidden email] 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.
>
>
Thank you for your example. However, this does not align the plot areas horizontally. It happens to do so in this example because the y-axis labels are the same length etc but when this is not the case, it fails.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Arranging ggplot2 objects with ggplotGrob()

Rui Barradas
Hello,

Inline.

Às 16:54 de 25/07/2020, H escreveu:

> On 07/24/2020 05:56 PM, Rui Barradas wrote:
>> Hello,
>>
>> I've just tried it.
>>
>> library(ggplot2)
>> #library(grid)
>> library(cowplot)
>>
>> s <- ggplot(data = subset(iris, Species == 'virginica'), aes(Sepal.Length, Sepal.Width)) + geom_point()
>> v <- ggplot(data = subset(iris, Species == 'setosa'), aes(Sepal.Length, Sepal.Width)) + geom_point()
>>
>> #g2 <- ggplotGrob(s)
>> #g3 <- ggplotGrob(v)
>> #g <- rbind(g2, g3, size = "first")
>>
>> cowplot::plot_grid(s, v, align = "h", nrow = 2, rel_heights = 2:1)
>>
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>> Às 19:57 de 24/07/2020, Felipe Carrillo via R-help escreveu:
>>> What about cowplot?
>>> https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html
>>>
>>>       On Friday, July 24, 2020, 11:51:17 AM PDT, H <[hidden email]> 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 <[hidden email]> 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 <[hidden email]
>>>>> <mailto:[hidden email]>> 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 <[hidden email]
>>>>> <mailto:[hidden email]>> 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!
>>>>>>       >>
>>>>>>       >> ______________________________________________
>>>>>>       >> [hidden email] <mailto:[hidden email]> 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...
>>>>>>       ______________________________________________
>>>>>>       [hidden email] <mailto:[hidden email]> 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]]
>>>>>
>>>>> ______________________________________________
>>>>> [hidden email] 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?|
>>>
>>> ||
>>>
>>>
>>>       [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> [hidden email] 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]]
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>
> Thank you for your example. However, this does not align the plot areas horizontally. It happens to do so in this example because the y-axis labels are the same length etc but when this is not the case, it fails.

Maybe it fails but can you give an example?
The question, as far as I understand it, is about the plots' relative
heights, not about horizontal alignment. And (all) others have asked you
to give a reproducible example, please don't ask us to guess a use case
axis labels lengths (!), which is impossible to do.

Anyway, the function cowplot::plot_grid has an argument `align`. One of
the possible values is "hv" for both horizontal and vertical alignment.

Hope this helps,

Rui Barradas

>
> ______________________________________________
> [hidden email] 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.


--
Este e-mail foi verificado em termos de vírus pelo software antivírus Avast.
https://www.avast.com/antivirus

______________________________________________
[hidden email] 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.
H-2
Reply | Threaded
Open this post in threaded view
|

Re: Arranging ggplot2 objects with ggplotGrob()

H-2
On 07/25/2020 12:36 PM, Rui Barradas wrote:

> Hello,
>
> Inline.
>
> Às 16:54 de 25/07/2020, H escreveu:
>> On 07/24/2020 05:56 PM, Rui Barradas wrote:
>>> Hello,
>>>
>>> I've just tried it.
>>>
>>> library(ggplot2)
>>> #library(grid)
>>> library(cowplot)
>>>
>>> s <- ggplot(data = subset(iris, Species == 'virginica'), aes(Sepal.Length, Sepal.Width)) + geom_point()
>>> v <- ggplot(data = subset(iris, Species == 'setosa'), aes(Sepal.Length, Sepal.Width)) + geom_point()
>>>
>>> #g2 <- ggplotGrob(s)
>>> #g3 <- ggplotGrob(v)
>>> #g <- rbind(g2, g3, size = "first")
>>>
>>> cowplot::plot_grid(s, v, align = "h", nrow = 2, rel_heights = 2:1)
>>>
>>>
>>> Hope this helps,
>>>
>>> Rui Barradas
>>>
>>> Às 19:57 de 24/07/2020, Felipe Carrillo via R-help escreveu:
>>>> What about cowplot?
>>>> https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html
>>>>
>>>>       On Friday, July 24, 2020, 11:51:17 AM PDT, H <[hidden email]> 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 <[hidden email]> 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 <[hidden email]
>>>>>> <mailto:[hidden email]>> 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 <[hidden email]
>>>>>> <mailto:[hidden email]>> 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!
>>>>>>>       >>
>>>>>>>       >> ______________________________________________
>>>>>>>       >> [hidden email] <mailto:[hidden email]> 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...
>>>>>>>       ______________________________________________
>>>>>>>       [hidden email] <mailto:[hidden email]> 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]]
>>>>>>
>>>>>> ______________________________________________
>>>>>> [hidden email] 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?|
>>>>
>>>> ||
>>>>
>>>>
>>>>       [[alternative HTML version deleted]]
>>>>
>>>> ______________________________________________
>>>> [hidden email] 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]]
>>>>
>>>> ______________________________________________
>>>> [hidden email] 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.
>>>
>> Thank you for your example. However, this does not align the plot areas horizontally. It happens to do so in this example because the y-axis labels are the same length etc but when this is not the case, it fails.
>
> Maybe it fails but can you give an example?
> The question, as far as I understand it, is about the plots' relative heights, not about horizontal alignment. And (all) others have asked you to give a reproducible example, please don't ask us to guess a use case axis labels lengths (!), which is impossible to do.
>
> Anyway, the function cowplot::plot_grid has an argument `align`. One of the possible values is "hv" for both horizontal and vertical alignment.
>
> Hope this helps,
>
> Rui Barradas
>
>>
>> ______________________________________________
>> [hidden email] 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.
>
>
The easiest demonstration would be to replace:

v <- ggplot(data = subset(iris, Species == 'setosa'), aes(Sepal.Length, Sepal.Width)) + geom_point()

with:

v <- ggplot(data = subset(iris, Species == 'setosa'), aes(Sepal.Length, Sepal.Width * 1000)) + geom_point()

I am looking to not only being able to set relative heights of rows (as your code correctly does) but also have the plot areas aligned.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Arranging ggplot2 objects with ggplotGrob()

Rui Barradas
Hello,

OK, now it's reproducible, thanks.
align = "hv" works like I had suggested.

The full code is now

library(ggplot2)
library(cowplot)

s <- ggplot(data = subset(iris, Species == 'virginica'),
aes(Sepal.Length, Sepal.Width)) + geom_point()
v <- ggplot(data = subset(iris, Species == 'setosa'), aes(Sepal.Length,
Sepal.Width * 1000)) + geom_point()

cowplot::plot_grid(s, v, align = "hv", nrow = 2, rel_heights = 2:1)


Also, I have been failing to comply to the posting guide and never
posted the output of

sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

Matrix products: default

locale:
[1] LC_COLLATE=Portuguese_Portugal.1252 LC_CTYPE=Portuguese_Portugal.1252
[3] LC_MONETARY=Portuguese_Portugal.1252 LC_NUMERIC=C
[5] LC_TIME=Portuguese_Portugal.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods base

other attached packages:
[1] cowplot_1.0.0 ggplot2_3.3.2 tidyr_1.1.0   dplyr_1.0.0 sos_2.0-0
[6] brew_1.0-6

loaded via a namespace (and not attached):
  [1] Rcpp_1.0.4.6     magrittr_1.5     tidyselect_1.1.0 munsell_0.5.0
  [5] colorspace_1.4-1 R6_2.4.1         rlang_0.4.6 fansi_0.4.1
  [9] tools_4.0.2      grid_4.0.2       gtable_0.3.0 utf8_1.1.4
[13] cli_2.0.2        withr_2.2.0      ellipsis_0.3.1 digest_0.6.25
[17] assertthat_0.2.1 tibble_3.0.1     lifecycle_0.2.0 crayon_1.3.4
[21] farver_2.0.3     purrr_0.3.4      vctrs_0.3.1 glue_1.4.1
[25] labeling_0.3     compiler_4.0.2   pillar_1.4.4 generics_0.0.2
[29] scales_1.1.1     pkgconfig_2.0.3


Hope this helps,

Rui Barradas

Às 19:13 de 25/07/2020, H escreveu:

> On 07/25/2020 12:36 PM, Rui Barradas wrote:
>> Hello,
>>
>> Inline.
>>
>> Às 16:54 de 25/07/2020, H escreveu:
>>> On 07/24/2020 05:56 PM, Rui Barradas wrote:
>>>> Hello,
>>>>
>>>> I've just tried it.
>>>>
>>>> library(ggplot2)
>>>> #library(grid)
>>>> library(cowplot)
>>>>
>>>> s <- ggplot(data = subset(iris, Species == 'virginica'), aes(Sepal.Length, Sepal.Width)) + geom_point()
>>>> v <- ggplot(data = subset(iris, Species == 'setosa'), aes(Sepal.Length, Sepal.Width)) + geom_point()
>>>>
>>>> #g2 <- ggplotGrob(s)
>>>> #g3 <- ggplotGrob(v)
>>>> #g <- rbind(g2, g3, size = "first")
>>>>
>>>> cowplot::plot_grid(s, v, align = "h", nrow = 2, rel_heights = 2:1)
>>>>
>>>>
>>>> Hope this helps,
>>>>
>>>> Rui Barradas
>>>>
>>>> Às 19:57 de 24/07/2020, Felipe Carrillo via R-help escreveu:
>>>>> What about cowplot?
>>>>> https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html
>>>>>
>>>>>        On Friday, July 24, 2020, 11:51:17 AM PDT, H <[hidden email]> 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 <[hidden email]> 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 <[hidden email]
>>>>>>> <mailto:[hidden email]>> 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 <[hidden email]
>>>>>>> <mailto:[hidden email]>> 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!
>>>>>>>>        >>
>>>>>>>>        >> ______________________________________________
>>>>>>>>        >> [hidden email] <mailto:[hidden email]> 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...
>>>>>>>>        ______________________________________________
>>>>>>>>        [hidden email] <mailto:[hidden email]> 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]]
>>>>>>>
>>>>>>> ______________________________________________
>>>>>>> [hidden email] 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?|
>>>>>
>>>>> ||
>>>>>
>>>>>
>>>>>        [[alternative HTML version deleted]]
>>>>>
>>>>> ______________________________________________
>>>>> [hidden email] 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]]
>>>>>
>>>>> ______________________________________________
>>>>> [hidden email] 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.
>>> Thank you for your example. However, this does not align the plot areas horizontally. It happens to do so in this example because the y-axis labels are the same length etc but when this is not the case, it fails.
>> Maybe it fails but can you give an example?
>> The question, as far as I understand it, is about the plots' relative heights, not about horizontal alignment. And (all) others have asked you to give a reproducible example, please don't ask us to guess a use case axis labels lengths (!), which is impossible to do.
>>
>> Anyway, the function cowplot::plot_grid has an argument `align`. One of the possible values is "hv" for both horizontal and vertical alignment.
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>>> ______________________________________________
>>> [hidden email] 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.
>>
> The easiest demonstration would be to replace:
>
> v <- ggplot(data = subset(iris, Species == 'setosa'), aes(Sepal.Length, Sepal.Width)) + geom_point()
>
> with:
>
> v <- ggplot(data = subset(iris, Species == 'setosa'), aes(Sepal.Length, Sepal.Width * 1000)) + geom_point()
>
> I am looking to not only being able to set relative heights of rows (as your code correctly does) but also have the plot areas aligned.
>
> ______________________________________________
> [hidden email] 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.


--
Este e-mail foi verificado em termos de vírus pelo software antivírus Avast.
https://www.avast.com/antivirus

______________________________________________
[hidden email] 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.
H-2
Reply | Threaded
Open this post in threaded view
|

Re: Arranging ggplot2 objects with ggplotGrob()

H-2
On 07/25/2020 03:01 PM, Rui Barradas wrote:

> Hello,
>
> OK, now it's reproducible, thanks.
> align = "hv" works like I had suggested.
>
> The full code is now
>
> library(ggplot2)
> library(cowplot)
>
> s <- ggplot(data = subset(iris, Species == 'virginica'), aes(Sepal.Length, Sepal.Width)) + geom_point()
> v <- ggplot(data = subset(iris, Species == 'setosa'), aes(Sepal.Length, Sepal.Width * 1000)) + geom_point()
>
> cowplot::plot_grid(s, v, align = "hv", nrow = 2, rel_heights = 2:1)
>
>
> Also, I have been failing to comply to the posting guide and never posted the output of
>
> sessionInfo()
> R version 4.0.2 (2020-06-22)
> Platform: x86_64-w64-mingw32/x64 (64-bit)
> Running under: Windows 10 x64 (build 18362)
>
> Matrix products: default
>
> locale:
> [1] LC_COLLATE=Portuguese_Portugal.1252 LC_CTYPE=Portuguese_Portugal.1252
> [3] LC_MONETARY=Portuguese_Portugal.1252 LC_NUMERIC=C
> [5] LC_TIME=Portuguese_Portugal.1252
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods base
>
> other attached packages:
> [1] cowplot_1.0.0 ggplot2_3.3.2 tidyr_1.1.0   dplyr_1.0.0 sos_2.0-0
> [6] brew_1.0-6
>
> loaded via a namespace (and not attached):
>  [1] Rcpp_1.0.4.6     magrittr_1.5     tidyselect_1.1.0 munsell_0.5.0
>  [5] colorspace_1.4-1 R6_2.4.1         rlang_0.4.6 fansi_0.4.1
>  [9] tools_4.0.2      grid_4.0.2       gtable_0.3.0 utf8_1.1.4
> [13] cli_2.0.2        withr_2.2.0      ellipsis_0.3.1 digest_0.6.25
> [17] assertthat_0.2.1 tibble_3.0.1     lifecycle_0.2.0 crayon_1.3.4
> [21] farver_2.0.3     purrr_0.3.4      vctrs_0.3.1 glue_1.4.1
> [25] labeling_0.3     compiler_4.0.2   pillar_1.4.4 generics_0.0.2
> [29] scales_1.1.1     pkgconfig_2.0.3
>
>
> Hope this helps,
>
> Rui Barradas
>
> Às 19:13 de 25/07/2020, H escreveu:
>> On 07/25/2020 12:36 PM, Rui Barradas wrote:
>>> Hello,
>>>
>>> Inline.
>>>
>>> Às 16:54 de 25/07/2020, H escreveu:
>>>> On 07/24/2020 05:56 PM, Rui Barradas wrote:
>>>>> Hello,
>>>>>
>>>>> I've just tried it.
>>>>>
>>>>> library(ggplot2)
>>>>> #library(grid)
>>>>> library(cowplot)
>>>>>
>>>>> s <- ggplot(data = subset(iris, Species == 'virginica'), aes(Sepal.Length, Sepal.Width)) + geom_point()
>>>>> v <- ggplot(data = subset(iris, Species == 'setosa'), aes(Sepal.Length, Sepal.Width)) + geom_point()
>>>>>
>>>>> #g2 <- ggplotGrob(s)
>>>>> #g3 <- ggplotGrob(v)
>>>>> #g <- rbind(g2, g3, size = "first")
>>>>>
>>>>> cowplot::plot_grid(s, v, align = "h", nrow = 2, rel_heights = 2:1)
>>>>>
>>>>>
>>>>> Hope this helps,
>>>>>
>>>>> Rui Barradas
>>>>>
>>>>> Às 19:57 de 24/07/2020, Felipe Carrillo via R-help escreveu:
>>>>>> What about cowplot?
>>>>>> https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html
>>>>>>
>>>>>>        On Friday, July 24, 2020, 11:51:17 AM PDT, H <[hidden email]> 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 <[hidden email]> 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 <[hidden email]
>>>>>>>> <mailto:[hidden email]>> 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 <[hidden email]
>>>>>>>> <mailto:[hidden email]>> 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!
>>>>>>>>>        >>
>>>>>>>>>        >> ______________________________________________
>>>>>>>>>        >> [hidden email] <mailto:[hidden email]> 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...
>>>>>>>>>        ______________________________________________
>>>>>>>>>        [hidden email] <mailto:[hidden email]> 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]]
>>>>>>>>
>>>>>>>> ______________________________________________
>>>>>>>> [hidden email] 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?|
>>>>>>
>>>>>> ||
>>>>>>
>>>>>>
>>>>>>        [[alternative HTML version deleted]]
>>>>>>
>>>>>> ______________________________________________
>>>>>> [hidden email] 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]]
>>>>>>
>>>>>> ______________________________________________
>>>>>> [hidden email] 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.
>>>> Thank you for your example. However, this does not align the plot areas horizontally. It happens to do so in this example because the y-axis labels are the same length etc but when this is not the case, it fails.
>>> Maybe it fails but can you give an example?
>>> The question, as far as I understand it, is about the plots' relative heights, not about horizontal alignment. And (all) others have asked you to give a reproducible example, please don't ask us to guess a use case axis labels lengths (!), which is impossible to do.
>>>
>>> Anyway, the function cowplot::plot_grid has an argument `align`. One of the possible values is "hv" for both horizontal and vertical alignment.
>>>
>>> Hope this helps,
>>>
>>> Rui Barradas
>>>
>>>> ______________________________________________
>>>> [hidden email] 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.
>>>
>> The easiest demonstration would be to replace:
>>
>> v <- ggplot(data = subset(iris, Species == 'setosa'), aes(Sepal.Length, Sepal.Width)) + geom_point()
>>
>> with:
>>
>> v <- ggplot(data = subset(iris, Species == 'setosa'), aes(Sepal.Length, Sepal.Width * 1000)) + geom_point()
>>
>> I am looking to not only being able to set relative heights of rows (as your code correctly does) but also have the plot areas aligned.
>>
>> ______________________________________________
>> [hidden email] 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.
>
>
Just tried it and it works in this example, however, with my two plots where the top plot does not have an x-axis legend the code above still reserves room for the legend which now shows up as a large empty space...

This is harder than I thought. I wonder if going back to the grobs would offer a better solution? The problem is that diving into the underlying code and understanding which variables to change is hard, there are /lots/ of variables...

______________________________________________
[hidden email] 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.
H-2
Reply | Threaded
Open this post in threaded view
|

Re: Arranging ggplot2 objects with ggplotGrob()

H-2
On 07/25/2020 03:05 PM, H wrote:

> On 07/25/2020 03:01 PM, Rui Barradas wrote:
>> Hello,
>>
>> OK, now it's reproducible, thanks.
>> align = "hv" works like I had suggested.
>>
>> The full code is now
>>
>> library(ggplot2)
>> library(cowplot)
>>
>> s <- ggplot(data = subset(iris, Species == 'virginica'), aes(Sepal.Length, Sepal.Width)) + geom_point()
>> v <- ggplot(data = subset(iris, Species == 'setosa'), aes(Sepal.Length, Sepal.Width * 1000)) + geom_point()
>>
>> cowplot::plot_grid(s, v, align = "hv", nrow = 2, rel_heights = 2:1)
>>
>>
>> Also, I have been failing to comply to the posting guide and never posted the output of
>>
>> sessionInfo()
>> R version 4.0.2 (2020-06-22)
>> Platform: x86_64-w64-mingw32/x64 (64-bit)
>> Running under: Windows 10 x64 (build 18362)
>>
>> Matrix products: default
>>
>> locale:
>> [1] LC_COLLATE=Portuguese_Portugal.1252 LC_CTYPE=Portuguese_Portugal.1252
>> [3] LC_MONETARY=Portuguese_Portugal.1252 LC_NUMERIC=C
>> [5] LC_TIME=Portuguese_Portugal.1252
>>
>> attached base packages:
>> [1] stats     graphics  grDevices utils     datasets  methods base
>>
>> other attached packages:
>> [1] cowplot_1.0.0 ggplot2_3.3.2 tidyr_1.1.0   dplyr_1.0.0 sos_2.0-0
>> [6] brew_1.0-6
>>
>> loaded via a namespace (and not attached):
>>  [1] Rcpp_1.0.4.6     magrittr_1.5     tidyselect_1.1.0 munsell_0.5.0
>>  [5] colorspace_1.4-1 R6_2.4.1         rlang_0.4.6 fansi_0.4.1
>>  [9] tools_4.0.2      grid_4.0.2       gtable_0.3.0 utf8_1.1.4
>> [13] cli_2.0.2        withr_2.2.0      ellipsis_0.3.1 digest_0.6.25
>> [17] assertthat_0.2.1 tibble_3.0.1     lifecycle_0.2.0 crayon_1.3.4
>> [21] farver_2.0.3     purrr_0.3.4      vctrs_0.3.1 glue_1.4.1
>> [25] labeling_0.3     compiler_4.0.2   pillar_1.4.4 generics_0.0.2
>> [29] scales_1.1.1     pkgconfig_2.0.3
>>
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>> Às 19:13 de 25/07/2020, H escreveu:
>>> On 07/25/2020 12:36 PM, Rui Barradas wrote:
>>>> Hello,
>>>>
>>>> Inline.
>>>>
>>>> Às 16:54 de 25/07/2020, H escreveu:
>>>>> On 07/24/2020 05:56 PM, Rui Barradas wrote:
>>>>>> Hello,
>>>>>>
>>>>>> I've just tried it.
>>>>>>
>>>>>> library(ggplot2)
>>>>>> #library(grid)
>>>>>> library(cowplot)
>>>>>>
>>>>>> s <- ggplot(data = subset(iris, Species == 'virginica'), aes(Sepal.Length, Sepal.Width)) + geom_point()
>>>>>> v <- ggplot(data = subset(iris, Species == 'setosa'), aes(Sepal.Length, Sepal.Width)) + geom_point()
>>>>>>
>>>>>> #g2 <- ggplotGrob(s)
>>>>>> #g3 <- ggplotGrob(v)
>>>>>> #g <- rbind(g2, g3, size = "first")
>>>>>>
>>>>>> cowplot::plot_grid(s, v, align = "h", nrow = 2, rel_heights = 2:1)
>>>>>>
>>>>>>
>>>>>> Hope this helps,
>>>>>>
>>>>>> Rui Barradas
>>>>>>
>>>>>> Às 19:57 de 24/07/2020, Felipe Carrillo via R-help escreveu:
>>>>>>> What about cowplot?
>>>>>>> https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html
>>>>>>>
>>>>>>>        On Friday, July 24, 2020, 11:51:17 AM PDT, H <[hidden email]> 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 <[hidden email]> 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 <[hidden email]
>>>>>>>>> <mailto:[hidden email]>> 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 <[hidden email]
>>>>>>>>> <mailto:[hidden email]>> 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!
>>>>>>>>>>        >>
>>>>>>>>>>        >> ______________________________________________
>>>>>>>>>>        >> [hidden email] <mailto:[hidden email]> 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...
>>>>>>>>>>        ______________________________________________
>>>>>>>>>>        [hidden email] <mailto:[hidden email]> 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]]
>>>>>>>>>
>>>>>>>>> ______________________________________________
>>>>>>>>> [hidden email] 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?|
>>>>>>>
>>>>>>> ||
>>>>>>>
>>>>>>>
>>>>>>>        [[alternative HTML version deleted]]
>>>>>>>
>>>>>>> ______________________________________________
>>>>>>> [hidden email] 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]]
>>>>>>>
>>>>>>> ______________________________________________
>>>>>>> [hidden email] 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.
>>>>> Thank you for your example. However, this does not align the plot areas horizontally. It happens to do so in this example because the y-axis labels are the same length etc but when this is not the case, it fails.
>>>> Maybe it fails but can you give an example?
>>>> The question, as far as I understand it, is about the plots' relative heights, not about horizontal alignment. And (all) others have asked you to give a reproducible example, please don't ask us to guess a use case axis labels lengths (!), which is impossible to do.
>>>>
>>>> Anyway, the function cowplot::plot_grid has an argument `align`. One of the possible values is "hv" for both horizontal and vertical alignment.
>>>>
>>>> Hope this helps,
>>>>
>>>> Rui Barradas
>>>>
>>>>> ______________________________________________
>>>>> [hidden email] 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.
>>> The easiest demonstration would be to replace:
>>>
>>> v <- ggplot(data = subset(iris, Species == 'setosa'), aes(Sepal.Length, Sepal.Width)) + geom_point()
>>>
>>> with:
>>>
>>> v <- ggplot(data = subset(iris, Species == 'setosa'), aes(Sepal.Length, Sepal.Width * 1000)) + geom_point()
>>>
>>> I am looking to not only being able to set relative heights of rows (as your code correctly does) but also have the plot areas aligned.
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>
> Just tried it and it works in this example, however, with my two plots where the top plot does not have an x-axis legend the code above still reserves room for the legend which now shows up as a large empty space...
>
> This is harder than I thought. I wonder if going back to the grobs would offer a better solution? The problem is that diving into the underlying code and understanding which variables to change is hard, there are /lots/ of variables...
>
Returning to this problem. Given the plots below where the plot areas are now nicely aligned, is it possible to change the heights to eg relative heights being 2:1?

s<-ggplot(data = subset(iris, Species == 'virginica'), aes(Sepal.Length, Sepal.Width)) + geom_point()
v<-ggplot(data = subset(iris, Species == 'setosa'), aes(Sepal.Length, Sepal.Width * 1000)) + geom_point()
g2<-ggplotGrob(s)
g3<-ggplotGrob(v)
g<-rbind(g2, g3, size = "first")
g$widths<-unit.pmax(g2$widths, g3$widths)
grid.newpage()
grid.draw(

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Arranging ggplot2 objects with ggplotGrob()

R help mailing list-2
In reply to this post by Bert Gunter-2
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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]
>> >>> <mailto:[hidden email]>> 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 <[hidden email]
>> >>> <mailto:[hidden email]>> 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!
>> >>>>     >>
>> >>>>     >> ______________________________________________
>> >>>>     >> [hidden email] <mailto:[hidden email]> 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...
>> >>>>     ______________________________________________
>> >>>>     [hidden email] <mailto:[hidden email]> 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]]
>> >>>
>> >>> ______________________________________________
>> >>> [hidden email] 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]]
>>
>> ______________________________________________
>> [hidden email] 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
H-2
Reply | Threaded
Open this post in threaded view
|

Re: Arranging ggplot2 objects with ggplotGrob()

H-2
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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]
>>> >>> <mailto:[hidden email]>> 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 <[hidden email]
>>> >>> <mailto:[hidden email]>> 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!
>>> >>>>     >>
>>> >>>>     >> ______________________________________________
>>> >>>>     >> [hidden email] <mailto:[hidden email]> 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...
>>> >>>>     ______________________________________________
>>> >>>>     [hidden email] <mailto:[hidden email]> 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]]
>>> >>>
>>> >>> ______________________________________________
>>> >>> [hidden email] 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]]
>>>
>>> ______________________________________________
>>> [hidden email] 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]]
>>
>> ______________________________________________
>> [hidden email] 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?

______________________________________________
[hidden email] 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.
12