Any ggplots side-by-side (or n plots on a grid)
The function grid.arrange()
in the gridExtra
package will combine multiple plots; this is how you put two side by side.
require(gridExtra)
plot1 <- qplot(1)
plot2 <- qplot(1)
grid.arrange(plot1, plot2, ncol=2)
This is useful when the two plots are not based on the same data, for example if you want to plot different variables without using reshape().
This will plot the output as a side effect. To print the side effect to a file, specify a device driver (such as pdf
, png
, etc), e.g.
pdf("foo.pdf")
grid.arrange(plot1, plot2)
dev.off()
or, use arrangeGrob()
in combination with ggsave()
,
ggsave("foo.pdf", arrangeGrob(plot1, plot2))
This is the equivalent of making two distinct plots using par(mfrow = c(1,2))
. This not only saves time arranging data, it is necessary when you want two dissimilar plots.
Appendix: Using Facets
Facets are helpful for making similar plots for different groups. This is pointed out below in many answers below, but I want to highlight this approach with examples equivalent to the above plots.
mydata <- data.frame(myGroup = c('a', 'b'), myX = c(1,1))
qplot(data = mydata,
x = myX,
facets = ~myGroup)
ggplot(data = mydata) +
geom_bar(aes(myX)) +
facet_wrap(~myGroup)
Update
the plot_grid
function in the cowplot
is worth checking out as an alternative to grid.arrange
. See the answer by @claus-wilke below and this vignette for an equivalent approach; but the function allows finer controls on plot location and size, based on this vignette.
Change the last line to
q + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
By default, the axes are aligned at the center of the text, even when rotated. When you rotate +/- 90 degrees, you usually want it to be aligned at the edge instead:
The image above is from this blog post.
Best Answer
Hey folks here's a wee hack that addresses the texture issue in a very basic fashion:
Make the border on one bar darker than the others
EDIT: I've finally found time to give a brief example of this hack that allows at least 3 types of basic pattern in ggplot2. The code:
Produces this:
It isn't super pretty but it is the only solution I can think on.
As can be seen I produce some very basic data. To get the vertical lines I simply create a data frame to contain the variable I wanted to add vertical lines to and redrew the graph borders multiple times reducing the width each time.
A similar thing is done for the horizontal lines but a new data frame is needed for each redraw where I have subtracted a value (in my example '5') from the value associated with the variable of interest. Effectively lowering the height of the bar. This is clunky to achieve and there may be more streamlined approaches but this illustrates how it can be achieved.
The mesh pattern is a combination of both. Firstly draw the vertical lines and then add the horizontal lines setting
fill
asfill='transparent'
to ensure the vertical lines are not drawn over.Until there is a pattern update I hope some of you find this useful.
EDIT 2:
Additionally diagonal patterns may also be added. I added an extra variable to the data frame:
Then I created a new data frame to hold coordinates for the diagonal lines:
From there I added geom_paths to the ggplot above with each one calling different coordinates and drawing the lines over the desired bar:
This results in the following:
It is a bit sloppy as I didn't invest too much time in getting the lines perfectly angled and spaced apart but this should serve as a proof of concept.
Obviously the lines can lean the opposite direction and there is also room for diagonal meshing much like the horizontal and vertical meshing.
I think that's about all I can offer on the pattern front. Hope someone can find a use for it.
EDIT 3: Famous last words. I have come up with another pattern option. This time using
geom_jitter
.Again I added another Variable to the data frame:
And I ordered how I wanted each pattern presented:
Next I created a column to contain the number associated with the intended target bar on the x-axis:
Followed by columns to contain the positions on the y-axis of the 'bubbles':
Finally I added
geom_jitter
s to the ggplot above using the new columns for positioning and re-using 'Points' to vary the size of the 'bubbles':Each time the plot is run the jitter positions the 'bubbles' differently but here is one of the nicer outputs I had:
Sometimes the 'bubbles' will jitter outside borders. If this happens rerun or simply export in larger dimensions. More bubbles can be plotted on each increment on the y-axis which will fill more of the blank space if you so desire.
That makes up to 7 patterns (if you include opposite leaning diagonal lines and diagonal mesh of both) that can be hacked in ggplot.
Please feel free to suggest more if anyone can think on some.
EDIT 4: I've been working on a wrapper function to automate hatching/patterns in ggplot2. I'll post a link once I've expanded the function to allow patterns in facet_grid plots etc. Here's an output with the function input for a simple plot of bars as an example:
I'll add one last edit once I have the function ready to share.
EDIT 5: Here's a link to the function EggHatch that I wrote to make the process of adding patterns to geom_bar plots a little easier.
EDIT 6: I thought I'd share a simple variation of this solution to add some colour to hatched plots.
Using the same df as above running this code:
results in this plot:
And this code, again using dfs from above:
results in this: