Is it possible to set the margin in ggplot2 to a fixed size? I create many plots, and I want them to look the same. Especially I want them to have the same left margin. But P<-ggplot()+geom_bar(aes(c("short label1","short label2"),runif(2)))+coord_flip() P<-creates a plot with another margin as ggplot()+geom_bar(aes(c("very very very very long label1","short label2"),runif(2)))+coord_flip() In the standard plot system margins can be set with par(mar=c(...)), but this doesn't work in ggplot2 I thought I could do it with plot.margin, but p+theme(plot.margin= unit(c(0, 0, 0,3), "cm")) doesn't work,it adds extra, external margins. Does anyone know a solution? Thanks Frans ------------------- dr F.H.G. (Frans) Marcelissen DigiPsy ( <http://www.digipsy.nl/> www.DigiPsy.nl) Pomperschans 26 5595 AV Leende tel: 040 7630487 (let op: nieuw nummer, oude nummer blijft actief) skype adres: frans.marcelissen email: <mailto:frans.marcelissen@digipsy.nl> frans.marcelissen@digipsy.nl [[alternative HTML version deleted]]
Well, the margin is being set large enough to accommodate the labels. So if you want narrower margins just shorten the labels: library(stringr) P + scale_x_discrete(labels = function(x) str_wrap(x, width=12)) HTH, Ista On Fri, Dec 21, 2012 at 5:35 PM, Frans Marcelissen <frans.marcelissen at digipsy.nl> wrote:> Is it possible to set the margin in ggplot2 to a fixed size? I create many > plots, and I want them to look the same. > > Especially I want them to have the same left margin. > > But > > P<-ggplot()+geom_bar(aes(c("short label1","short > label2"),runif(2)))+coord_flip() > > P<-creates a plot with another margin as > > ggplot()+geom_bar(aes(c("very very very very long label1","short > label2"),runif(2)))+coord_flip() > > > > In the standard plot system margins can be set with par(mar=c(...)), but > this doesn't work in ggplot2 > > I thought I could do it with plot.margin, but > > p+theme(plot.margin= unit(c(0, 0, 0,3), "cm")) > > doesn't work,it adds extra, external margins. > > Does anyone know a solution? > > Thanks > > Frans > > ------------------- > dr F.H.G. (Frans) Marcelissen > DigiPsy ( <http://www.digipsy.nl/> www.DigiPsy.nl) > Pomperschans 26 > 5595 AV Leende > tel: 040 7630487 (let op: nieuw nummer, oude nummer blijft actief) > skype adres: frans.marcelissen > email: <mailto:frans.marcelissen at digipsy.nl> frans.marcelissen at digipsy.nl > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Hi Ista, Your suggestion is not the solution I need. My problem is that in this way each plot gets another margin. I try to create a presentation with about 10 plots.The presentation does'nt look well if each plot has another marging. Is there way to set the margin manually? Frans ? -----Oorspronkelijk bericht----- Van: Ista Zahn [mailto:istazahn at gmail.com] Verzonden: zaterdag 22 december 2012 0:47 Aan: Frans Marcelissen CC: R-help at r-project.org Onderwerp: Re: [R] ggplot2: setting martin ? Well, the margin is being set large enough to accommodate? the labels. So if you want narrower margins just shorten the labels: ? library(stringr) P + scale_x_discrete(labels = function(x) str_wrap(x, width=12)) ? ? HTH, Ista On Fri, Dec 21, 2012 at 5:35 PM, Frans Marcelissen <frans.marcelissen at digipsy.nl> wrote:> Is itpossible to set the margin in ggplot2 to a fixed size? I create> many plots,and I want them to look the same.>?> EspeciallyI want them to have the same left margin.>?> But>?>P<-ggplot()+geom_bar(aes(c("short label1","short>label2"),runif(2)))+coord_flip()>?>P<-creates a plot with another margin as>?>ggplot()+geom_bar(aes(c("very very very very long label1","short>label2"),runif(2)))+coord_flip()>?>?>?> In thestandard plot system margins can be set with par(mar=c(...)),> but thisdoesn't work in ggplot2>?> I thought Icould do it with plot.margin, but>?>p+theme(plot.margin= unit(c(0, 0, 0,3), "cm"))>?> doesn'twork,it adds extra, external margins.>?> Does anyoneknow a solution?>?> Thanks>?> Frans>? >?>?>???????? [[alternative HTML version deleted]]>?>______________________________________________> R-help at r-project.org mailing list> https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE doread the posting guide> http://www.R-project.org/posting-guide.html> and providecommented, minimal, self-contained, reproducible code.