plot size not "maintained" by grid.arrange

701 views
Skip to first unread message

Ellen

unread,
Oct 31, 2012, 12:24:40 PM10/31/12
to ggp...@googlegroups.com
Dear all,

I have a problem using the grid.arrange function in the gridExtra package to put multiple plots in one window. I want to plot seven scatterplots (dataset 1) and one barchart (dataset 2), and the latter seems to be compressed laterally (such that you cannot even see the bars!) when I save the image as a pdf. In attach you can find the dput output with the two datasets. Below you can find the code I used to create the plots.

plot.H.poc<-ggplot(div.env,aes(res.POC.SED,res.H.SED))+geom_point()
plot.H.poc<-plot.H.poc+geom_smooth(method="lm",formula=y~poly(x,2),se=F,colour="black")+opts(panel.grid.major=theme_blank(),panel.grid.minor=theme_blank())+labs(x="POC",y="H'")

plot.H.SED<-ggplot(div.env,aes(res.SED.POC,res.H.POC))+geom_point()
plot.H.SED<-plot.H.SED+geom_smooth(method="lm",se=F,colour="black")+opts(panel.grid.major=theme_blank(),panel.grid.minor=theme_blank())+labs(x="SED",y="H'")

plot.J.SVI<-ggplot(div.env,aes(SVI,J))+geom_point()+geom_smooth(se=F,method="lm",colour="black")+labs(y="J'",x="SVI")+opts(panel.grid.major=theme_blank(),panel.grid.minor=theme_blank())

plot.EG18.poc<-ggplot(div.env,aes(res.POC.SVI,res.EG18.SVI))+geom_point()
plot.EG18.poc<-plot.EG18.poc+geom_smooth(method="lm",se=F,colour="black")+opts(panel.grid.major=theme_blank(),panel.grid.minor=theme_blank())+labs(x="POC",y="EG(18)")

plot.EG18.SVI<-ggplot(div.env,aes(res.SVI.POC,res.EG18.POC))+geom_point()
plot.EG18.SVI<-plot.EG18.SVI+geom_smooth(method="lm",se=F,colour="black")+opts(panel.grid.major=theme_blank(),panel.grid.minor=theme_blank())+labs(x="SVI",y="EG(18)")

plot.TD.SED<-ggplot(div.env,aes(SED,TD))+geom_point()+geom_smooth(method="lm",se=F,colour="black")+opts(panel.grid.major=theme_blank(),panel.grid.minor=theme_blank())


plot.MI.SVI<-ggplot(div.env,aes(SVI,MI))+geom_point()+geom_smooth(method="lm",se=F,colour="black")+opts(panel.grid.major=theme_blank(),panel.grid.minor=theme_blank())

varpart.plot<-ggplot(varpart,aes(div,fraction))+geom_bar(colour="black",aes(fill=as.factor(var_expl)))+labs(x="",y="Fraction of variation explained (%)")+opts(panel.grid.major=theme_blank(),panel.rid.minor=theme_blank())+scale_fill_manual(values=c("#F5F5F5","#C0C0C0","#696969","white"),guide=guide_legend(" "))+coord_cartesian(ylim=c(0,100))+scale_x_discrete(labels=c("EG(18)","H'","J'","MI","TD"))

library(gridExtra)
pdf("div_env.pdf")
grid.arrange(plot.H.poc,plot.H.SED,plot.J.SVI,plot.EG18.poc,plot.EG18.SVI,plot.TD.SED,plot.MI.SVI,varpart.plot,clip=F)
dev.off()



Does anybody know how to solve this problem?

Thanks!
Ellen

Ellen

unread,
Oct 31, 2012, 12:30:33 PM10/31/12
to ggp...@googlegroups.com
and now with attach :)
datasets.txt

Brandon Hurr

unread,
Nov 1, 2012, 4:23:17 AM11/1/12
to Ellen, ggplot2
Ellen, 

Your plots are calling for variables that do not exist in your provided datasets. They look almost like you want the residuals from a lm() between two variables, but your dataset only has the two variables so I cannot reproduce. I edited your script and put it into the attached. theme_ellen() seemed appropriate given that you were repeating the same changes to the grid lines in every plot. Also, it got rid of those annoying warnings. 

Can you supply values or supply the code that you use to compute your residuals? 

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

ellen.pape.r

Ellen Pape

unread,
Nov 1, 2012, 5:37:38 AM11/1/12
to Brandon Hurr, Ellen, ggplot2
Hello Brandon,

You're right, I completely forgot that some plots were in fact partial regression plots. Pardon my carelessness! Now, I've added the residual objects to the R-script you sent. I tried to run the code, but I got the following error:

Error in theme_ellen() : could not find function "%+replace%"

Do you know why this is?

Thanks for your help!
Ellen




Ellen Pape (PhD student)

Ghent University
Marine Biology Research Group
Krijgslaan 281, S8
9000 Ghent (Belgium)

Email: Ellen...@Ugent.be
Tel: ++ 32 (0)9 264 85 23
Fax: ++ 32 (0)9 264 85 98

ellen.pape.r

Brandon Hurr

unread,
Nov 1, 2012, 5:52:56 AM11/1/12
to Ellen Pape, Ellen, ggplot2
What version of ggplot2 are you running? I have no issues with %replace%. 
sessionInfo()
Mine is below. 

The reason why you're plot is compressed is because it is cramming the plot and the legend into the area you supplied via gridExtra. The best thing to do is to get rid of the legend and your plot will then take up the space. Thanks to Baptiste's good documentation and examples I found this: 


Which allowed me to edit the script. Should work and look snazzy now! 

B

> sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
 [1] plyr_1.7.1      drc_2.3-0       plotrix_3.4-5   nlme_3.1-104    magic_1.5-2     abind_1.4-0     gtools_2.7.0   
 [8] alr3_2.0.5      car_2.0-14      nnet_7.3-4      MASS_7.3-21     lattice_0.20-10 scales_0.2.2    gridExtra_0.9.1
[15] ggplot2_0.9.2.1

loaded via a namespace (and not attached):
 [1] colorspace_1.1-1   dichromat_1.2-4    digest_0.5.2       gtable_0.1.1       labeling_0.1       memoise_0.1       
 [7] munsell_0.4        proto_0.3-9.2      RColorBrewer_1.0-5 reshape2_1.2.1     stringr_0.6.1      tools_2.15.1  
ellen.pape.r

Ellen Pape

unread,
Nov 1, 2012, 6:55:27 AM11/1/12
to Brandon Hurr, Ellen, ggplot2
Hi,

Well, I updated ggplot2, but it still does not recognize the replace-function. And, I've got the same version of R as you...Strange...

> sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=Dutch_Belgium.1252  LC_CTYPE=Dutch_Belgium.1252    LC_MONETARY=Dutch_Belgium.1252
[4] LC_NUMERIC=C                   LC_TIME=Dutch_Belgium.1252    

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

other attached packages:
[1] gtools_2.7.0    ggplot2_0.9.2.1 gridExtra_0.9.1 scales_0.2.2   

loaded via a namespace (and not attached):
 [1] colorspace_1.1-1   dichromat_1.2-4    digest_0.5.2       labeling_0.1       MASS_7.3-19        memoise_0.1       
 [7] munsell_0.4        plyr_1.7.1         proto_0.3-9.2      RColorBrewer_1.0-5 reshape2_1.2.1     stringr_0.6.1     
[13] tools_2.15.1      

>



Cheers

Ellen

Ellen Pape (PhD student)

Ghent University
Marine Biology Research Group
Krijgslaan 281, S8
9000 Ghent (Belgium)

Email: Ellen...@Ugent.be
Tel: ++ 32 (0)9 264 85 23
Fax: ++ 32 (0)9 264 85 98

Brandon Hurr

unread,
Nov 1, 2012, 6:58:53 AM11/1/12
to Ellen Pape, Ellen, ggplot2
Ellen, 

I'm not sure why that's not working, it was part of 0.9.2. See the NEWS file here... 

http://cran.r-project.org/web/packages/ggplot2/NEWS

Try flushing your workspace, and restarting R. 

If not that, then maybe it has something to do with your locale setting? (I really have no idea). 

Brandon

Ellen Pape

unread,
Nov 1, 2012, 6:59:09 AM11/1/12
to Brandon Hurr, Ellen, ggplot2
okay, I restarted everything, and now it does work! sorry! and thanks!




Ellen Pape (PhD student)

Ghent University
Marine Biology Research Group
Krijgslaan 281, S8
9000 Ghent (Belgium)

Email: Ellen...@Ugent.be
Tel: ++ 32 (0)9 264 85 23
Fax: ++ 32 (0)9 264 85 98

On 1/11/2012 10:52, Brandon Hurr wrote:

Brandon Hurr

unread,
Nov 1, 2012, 7:00:21 AM11/1/12
to Ellen Pape, Ellen, ggplot2
Does that do what you want it to? I'm not sure what other options you have to be honest.

Ellen Pape

unread,
Nov 1, 2012, 7:05:10 AM11/1/12
to Brandon Hurr, Ellen, ggplot2
Hi,

Yes, the modifications you made to the script work out fine!

Thanks for the help!

Ellen

Ellen Pape (PhD student)

Ghent University
Marine Biology Research Group
Krijgslaan 281, S8
9000 Ghent (Belgium)

Email: Ellen...@Ugent.be
Tel: ++ 32 (0)9 264 85 23
Fax: ++ 32 (0)9 264 85 98

Data Analytics Corp.

unread,
Nov 1, 2012, 10:53:06 AM11/1/12
to ggp...@googlegroups.com
Hi,

I want to create a simple pie chart -- nothing fancy, just a simple
pie. But I can't get ggplot2 to do this. Help!!! Here's the code I'm
using:

x <- data.frame(y = c(0.08, 0.92))
library("ggplot2")
p <- ggplot(data = x, aes(x = factor(1), fill = y ) )
p <- p + geom_bar() + coord_polar(theta="y")
p

I keep getting this error message. What's wrong?

Error in unit(tic_pos.c, "mm") : 'x' and 'units' must have
length > 0

Any help is appreciated.

Thanks,

Walt

________________________

Walter R. Paczkowski, Ph.D.
Data Analytics Corp.
44 Hamilton Lane
Plainsboro, NJ 08536
________________________
(V) 609-936-8999
(F) 609-936-3733
wa...@dataanalyticscorp.com
www.dataanalyticscorp.com
_____________________________________________________



Dennis Murphy

unread,
Nov 1, 2012, 4:18:29 PM11/1/12
to wa...@dataanalyticscorp.com, ggp...@googlegroups.com
Hi:

This 'kind of'' works:

x <- data.frame(y = c(0.08, 0.92))
q <- ggplot(x, aes(x = factor(1), fill = factor(y))) +
        geom_bar(aes(y = y), stat = "identity")
q + coord_polar(theta = "y")

I don't know what's up with the open circle in the center of the chart, but maybe someone else knows how to fix it.

Dennis

--
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

Winston Chang

unread,
Nov 1, 2012, 4:31:41 PM11/1/12
to Dennis Murphy, wa...@dataanalyticscorp.com, ggplot2
I think it's because the bar width defaults to 0.9 instead of 1.

library(ggplot2)
dat <- data.frame(y = c(0.08, 0.92), g = c("A", "B"))

ggplot(dat, aes(x = factor(1), y = y, fill = g)) +
  geom_bar(stat="identity", width=1) +
  coord_polar(theta = "y")



Data Analytics Corp.

unread,
Nov 1, 2012, 5:26:49 PM11/1/12
to ggp...@googlegroups.com
I finally figured it out after much effort. Winston's fix was like mine
which is comforting. But the white dot is an issue. Can this be
removed? By the way, I know all the arguments about pies, but the
reality is that they are used everywhere in the business world. Can a
simple geom be developed just for them? One without the outer circle
and degree notation? Just a simple pie chart.

Thanks,

Walt

On 11/1/2012 4:31 PM, Winston Chang wrote:
> I think it's because the bar width defaults to 0.9 instead of 1.
>
> library(ggplot2)
> dat <- data.frame(y = c(0.08, 0.92), g = c("A", "B"))
>
> ggplot(dat, aes(x = factor(1), y = y, fill = g)) +
> geom_bar(stat="identity", width=1) +
> coord_polar(theta = "y")
>
>
>
> On Thu, Nov 1, 2012 at 3:18 PM, Dennis Murphy <djm...@gmail.com
> <mailto:djm...@gmail.com>> wrote:
>
> Hi:
>
> This 'kind of'' works:
>
> x <- data.frame(y = c(0.08, 0.92))
> q <- ggplot(x, aes(x = factor(1), fill = factor(y))) +
> geom_bar(aes(y = y), stat = "identity")
> q + coord_polar(theta = "y")
>
> I don't know what's up with the open circle in the center of the
> chart, but maybe someone else knows how to fix it.
>
> Dennis
>
> On Thu, Nov 1, 2012 at 7:53 AM, Data Analytics Corp.
> <wa...@dataanalyticscorp.com <mailto:wa...@dataanalyticscorp.com>>
> wrote:
>
> Hi,
>
> I want to create a simple pie chart -- nothing fancy, just a
> simple pie. But I can't get ggplot2 to do this. Help!!!
> Here's the code I'm using:
>
> x <- data.frame(y = c(0.08, 0.92))
> library("ggplot2")
> p <- ggplot(data = x, aes(x = factor(1), fill = y ) )
> p <- p + geom_bar() + coord_polar(theta="y")
> p
>
> I keep getting this error message. What's wrong?
>
> Error in unit(tic_pos.c, "mm") : 'x' and 'units' must
> have length > 0
>
> Any help is appreciated.
>
> Thanks,
>
> Walt
>
> ________________________
>
> Walter R. Paczkowski, Ph.D.
> Data Analytics Corp.
> 44 Hamilton Lane
> Plainsboro, NJ 08536
> ________________________
> (V) 609-936-8999 <tel:609-936-8999>
> (F) 609-936-3733 <tel:609-936-3733>
> wa...@dataanalyticscorp.com <mailto:wa...@dataanalyticscorp.com>
> www.dataanalyticscorp.com <http://www.dataanalyticscorp.com>
> _____________________________________________________
>
>
>
> --
> 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
> <mailto:ggp...@googlegroups.com>
> To unsubscribe: email ggplot2+u...@googlegroups.com
> <mailto:ggplot2%2Bunsu...@googlegroups.com>
> More options: http://groups.google.com/group/ggplot2
>
>
> --
> 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
> <mailto:ggp...@googlegroups.com>
> To unsubscribe: email ggplot2+u...@googlegroups.com
> <mailto:ggplot2%2Bunsu...@googlegroups.com>

Winston Chang

unread,
Nov 1, 2012, 5:48:02 PM11/1/12
to walt, ggplot2
Hm, maybe it's because I'm using the latest development version of ggplot2, but mine doesn't have the hole in the middle when I use width=1.

Now that I think of it, with ggplot2 0.9.2.1, you might be able to make it work by using scale_x_discrete(expand=c(0, 0)):

library(ggplot2)
dat <- data.frame(y = c(0.08, 0.92), g = c("A", "B"))

ggplot(dat, aes(x = factor(1), y = y, fill = g)) +
  geom_bar(stat="identity", width=1) +
  scale_x_discrete(expand = c(0, 0)) +
  coord_polar(theta = "y")

-Winston


On Thu, Nov 1, 2012 at 4:26 PM, Data Analytics Corp. <wa...@dataanalyticscorp.com> wrote:
I finally figured it out after much effort.  Winston's fix was like mine which is comforting.  But the white dot is an issue.  Can this be removed?  By the way, I know all the arguments about pies, but the reality is that they are used everywhere in the business world.  Can a simple geom be developed just for them?  One without the outer circle and degree notation?  Just a simple pie chart.

Thanks,

Walt


On 11/1/2012 4:31 PM, Winston Chang wrote:
I think it's because the bar width defaults to 0.9 instead of 1.

library(ggplot2)
dat <- data.frame(y = c(0.08, 0.92), g = c("A", "B"))

ggplot(dat, aes(x = factor(1), y = y, fill = g)) +
  geom_bar(stat="identity", width=1) +
  coord_polar(theta = "y")



On Thu, Nov 1, 2012 at 3:18 PM, Dennis Murphy <djm...@gmail.com <mailto:djm...@gmail.com>> wrote:

    Hi:

    This 'kind of'' works:

    x <- data.frame(y = c(0.08, 0.92))
    q <- ggplot(x, aes(x = factor(1), fill = factor(y))) +
            geom_bar(aes(y = y), stat = "identity")
    q + coord_polar(theta = "y")

    I don't know what's up with the open circle in the center of the
    chart, but maybe someone else knows how to fix it.

    Dennis

    On Thu, Nov 1, 2012 at 7:53 AM, Data Analytics Corp.

    wrote:

        Hi,

        I want to create a simple pie chart -- nothing fancy, just a
        simple pie.  But I can't get ggplot2 to do this.  Help!!!
         Here's the code I'm using:

                x <- data.frame(y = c(0.08, 0.92))
                library("ggplot2")
                p <- ggplot(data = x, aes(x = factor(1), fill = y ) )
                p <- p + geom_bar() + coord_polar(theta="y")
                p

        I keep getting this error message.  What's wrong?

                Error in unit(tic_pos.c, "mm") : 'x' and 'units' must
        have length > 0

        Any help is appreciated.

        Thanks,

        Walt

        ________________________

        Walter R. Paczkowski, Ph.D.
        Data Analytics Corp.
        44 Hamilton Lane
        Plainsboro, NJ 08536
        ________________________
        (V) 609-936-8999 <tel:609-936-8999>
        (F) 609-936-3733 <tel:609-936-3733>

        www.dataanalyticscorp.com <http://www.dataanalyticscorp.com>

        _____________________________________________________



        --         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
        <mailto:ggplot2@googlegroups.com>
        To unsubscribe: email ggplot2+unsubscribe@googlegroups.com
        <mailto:ggplot2%2Bunsubscribe@googlegroups.com>

        More options: http://groups.google.com/group/ggplot2


    --     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

    More options: http://groups.google.com/group/ggplot2


--
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

--
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
Reply all
Reply to author
Forward
0 new messages