Each ggplot expands to the full space available, therefore the size of
the plotting region depends on the size of the text labels (and, if
any, legends). This is why you observe a misalignment between the plot
panels.
My suggestion would be to join all your datasets into one and create a
dummy facetting variable. You could then use facet_grid or facet_wrap
to get the alignment you want.
Another possibility would be to use align.plots() instead of
grid.arrange(), but note that this is really a hack.
library(ggplot2)
# create plots with unequal y axis titles
ps <- replicate(5, qplot(1:10, rnorm(10),
ylab= paste(sample(letters,
sample(2:10,1)),collapse="",sep="")) +
opts(axis.title.y=theme_text(angle=0)),simplify=FALSE)
library(gridExtra)
do.call(grid.arrange, ps)
library(ggExtra)
grid.newpage()
do.call(align.plots, ps)
In your case, since you have several columns, you'd want to use a more
complicated version of align.plots that was posted on the list some
time ago (i forget by whom).
HTH,
baptiste
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
library(ggplot2)
# create a list with all the data.frames
ld <- llply(title.names, function(d) data.frame(x=1:10, y=rnorm(10)))
# give them a name
names(ld) <- title.names <- paste("plot", 1:4)
# combine them
m <- melt(ld, id = "x")
str(m)
ggplot(m) + facet_wrap(~L1,scales="free") +
geom_path(aes(x,value)) +
theme_bw() + ylab("")+
opts(strip.background=theme_blank())
HTH,
baptiste
library(ggplot2)
ld <-replicate(4, data.frame(x=1:10, y=rnorm(10)), simplify=FALSE)
names(ld) <- paste("plot", 1:4)
m <- melt(ld, id = "x")
str(m)
ggplot(m) + facet_wrap(~L1,scales="free") +
geom_path(aes(x,value)) +
theme_bw() + ylab("")+
opts(strip.background=theme_blank())
More options: http://groups.google.com/group/ggplot2
More options: http://groups.google.com/group/ggplot2
More options: http://groups.google.com/group/ggplot2
Totally agreed. A big part of becoming fluent in R is being able to
choose a way of storing your data that makes it easy to do what you
want with it. I think I'm slowly converging on a consistent philosophy
of data that helps you figure this out, and also provides the tools
you need to work with the data.
Hadley
--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/
I'd also recommend the reshape paper
(www.jstatsoft.org/v21/i12/paper), particularly the earlier part
before it gets to casting. I'm thinking my next book will be more
about storing and working with data in R.
Other than rendering the data with a fine ggplot2 plot, there are few
things as gratifying as adding a view to an existing set of tables and
bringing the data into R with a very simple query against that view --
the query encapsulated into a nicely re-usable function, of course.
That being said, melt can be far more convenient to writing a union
query, so the RDBMS would be complementary to the packages of manipulators.