getting tick labels to fit in grid.arrange

35 views
Skip to first unread message

Alexander Shenkin

unread,
Mar 25, 2013, 8:46:47 AM3/25/13
to ggp...@googlegroups.com
Hi Folks,

I've set up the plot below (and here: http://i.imgur.com/vScUR31.png) using the following code:

    require(gridExtra)
    plot_grobs = ggplot_gtable(ggplot_build(mort_by_2))$grobs
    grid.arrange(mort_by_2_nolab, mort_by_3_nolab,
                 mort_by_4_nolab, mort_by_5_nolab, plot_grobs[[5]], ncol=1,
                 left = "Proportion of trees dead")

As you can see, the tick labels on the bottom are going to the edges of the entire plot, and hence they don't line up properly.  Any idea how I might constrain them to the plot area so the fit correctly?

Thanks,
Allie


Brandon Hurr

unread,
Mar 25, 2013, 9:00:30 AM3/25/13
to Alexander Shenkin, ggplot2
It may require manual tweaking of how much space each plot takes up, but why not leave the labels on the 5th census plot and give it more space? 

Brandon


--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
 
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

eidbdcgg.png

Alexander Shenkin

unread,
Mar 25, 2013, 9:09:03 AM3/25/13
to Brandon Hurr, ggplot2
Thanks Brandon. Leaving the labels on the bottom-most plot ends up
squeezing the plot area upwards (see http://i.imgur.com/HBDq1bV.png).
Not sure what you mean by "manual tweaking". Photoshop?

On 3/25/2013 8:00 AM, Brandon Hurr wrote:
> It may require manual tweaking of how much space each plot takes up, but
> why not leave the labels on the 5th census plot and give it more space?
>
> Brandon
>
>
> On Mon, Mar 25, 2013 at 12:46 PM, Alexander Shenkin <ashe...@ufl.edu>wrote:
>
>> Hi Folks,
>>
>> I've set up the plot below (and here: http://i.imgur.com/vScUR31.png)
>> using the following code:
>>
>> require(gridExtra)
>> plot_grobs = ggplot_gtable(ggplot_build(mort_by_2))$grobs
>> grid.arrange(mort_by_2_nolab, mort_by_3_nolab,
>> mort_by_4_nolab, mort_by_5_nolab, plot_grobs[[5]],
>> ncol=1,
>> left = "Proportion of trees dead")
>>
>> As you can see, the tick labels on the bottom are going to the edges of
>> the entire plot, and hence they don't line up properly. Any idea how I
>> might constrain them to the plot area so the fit correctly?
>>
>> Thanks,
>> Allie
>>
>>

Brandon Hurr

unread,
Mar 25, 2013, 9:21:41 AM3/25/13
to Alexander Shenkin, ggplot2
Photoshop is a bit harsh. 

I was thinking more like telling grid.arrange that you have 4 plots of heights=c(1/5, 1/5, 1/5, 2/5)

See the below stackOverflow response from Justin about the options. 

If you still get stuck, send a reproducible example so we can play with the real deal. 

B

Alexander Shenkin

unread,
Mar 25, 2013, 12:17:45 PM3/25/13
to Brandon Hurr, ggplot2
Hi Brandon,

Tweaking with the heights isn't ideal, since if my labels change for
example, i'll have to re-tweak the heights all over again. Might be a
good last resort.

Here's some reproducible code that leads to the plot seen here:
http://i.imgur.com/okbwX55.png

Any thoughts about how to get the tick labels aligned?

thanks.

groups = c("grp1","grp2","grp3")
my_df = data.frame(group = sample(groups,100,replace=T), surv1 =
sample(c(1,0),100,replace=T), surv2 = sample(c(1,0),100,replace=T))
my_df_summ =
ddply(my_df, "group", summarize, surv_rate1 = mean(surv1), num_surv1
= sum(surv1),
surv_rate2 = mean(surv2), num_surv2 =
sum(surv2),
n = length(surv1))
require(binom)
attach(my_df_summ)
limits1 = binom.confint(num_surv1, n, method="exact")
limits2 = binom.confint(num_surv2, n, method="exact")
detach(my_df_summ)

ggsurv1 = ggplot(my_df_summ, aes(x=group, y=surv_rate1)) +
geom_bar(stat="identity") +
geom_errorbar(aes(ymax = limits1$upper, ymin = limits1$lower),
position = "dodge", width = 0.25, col="darkgrey")
ggsurv2 = ggplot(my_df_summ, aes(x=group, y=surv_rate2)) +
geom_bar(stat="identity") +
geom_errorbar(aes(ymax = limits2$upper, ymin = limits2$lower),
position = "dodge", width = 0.25, col="darkgrey")

plot_grobs = ggplot_gtable(ggplot_build(ggsurv1 + ylab("")))$grobs

require(gridExtra)
grid.arrange(ggsurv1 + scale_x_discrete(labels="") + xlab("") + ylab(""),
ggsurv2+ scale_x_discrete(labels="") + xlab("") + ylab(""),
plot_grobs[[5]], ncol=1,
left = "Proportion alive")
Reply all
Reply to author
Forward
0 new messages