grid.layout in ggplot - r

I'm using the following code to create three sets of plots in the amazing package ggplot2:
w<-rnorm(100)
x<-rnorm(100)
y<-rnorm(100)
z<-rnorm(100)
g<-rep(factor(LETTERS[1:4]), 25)
d<-data.frame(g,w,x,y,z)
library(ggplot2)
pw<-ggplot(d, aes(w, y))
px<-ggplot(d, aes(x, y))
pz<-ggplot(d, aes(z, y))
pw+geom_point()+facet_grid(.~g, scales='fixed')+coord_equal()+stat_smooth(method='lm')
px+geom_point()+facet_grid(.~g, scales='fixed')+coord_equal()+stat_smooth(method='lm')
pz+geom_point()+facet_grid(.~g, scales='fixed')+coord_equal()+stat_smooth(method='lm')
I would make a PDF file that has each of these three sets of plots printed on the same page. My understanding is thatsplit.screen(c(3,1))andpar(mfrow=c(3,1))won't work with ggplot2 graphics, but thatgrid.layout()from the grid package would work so I tried:
pdf(file="test.pdf")
pushViewport(viewport(layout=grid.layout(3,1)))
print(pw+geom_point()+facet_grid(.~g, scales='fixed')+coord_equal()+stat_smooth(method='lm'))
print(px+geom_point()+facet_grid(.~g, scales='fixed')+coord_equal()+stat_smooth(method='lm'))
print(pz+geom_point()+facet_grid(.~g, scales='fixed')+coord_equal()+stat_smooth(method='lm'))
dev.off()
but this ends up being a four page PDF file with the first page being blank and each set of figures following one per page and the x-axis label way down at the bottom. Is there a way to make a PDF file with all the sets of figures on the same page (and without a blank page leading!)?

You'll probably have a better time using grid.arrange(), from the gridExtra package:
p1 <- pw + geom_point() + facet_grid(.~g, scales='fixed') + coord_equal() +
stat_smooth(method='lm')
p2 <- px + geom_point() + facet_grid(.~g, scales='fixed') + coord_equal() +
stat_smooth(method='lm')
p3 <- pz + geom_point() + facet_grid(.~g, scales='fixed') + coord_equal() +
stat_smooth(method='lm')
grid.arrange(p1, p2, p3, ncol=1)

You can also use the multiplot() function, which could be customized to suite your needs: http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/

If you use markdown, use fig.height in each code chunk for each plot:
```{r pw, fig.height = 2.66, echo = F}
pw
```
```{r px, fig.height = 2.66, echo = F}
px
```
```{r pz, fig.height = 2.66, echo = F}
pz
```

Related

Force empty plot to show up with `patchwork` with pdf_output R Markdown

I would like to display a single plot that takes up half of my page (to appear like it is taking up one column), using the patchwork library and the plot_spacer() function. The thing is, I can't get the "empty plot" to take up half the space! No matter what I do, the first plot seems to take up most of the page. I'm looking for the equivalent of p1 + p2 below, except I'd want p2 to be white space.
Reproducible example and output
---
title: " "
author: " "
date: " "
output: pdf_document
geometry: paperheight=8.5in,paperwidth=5.5in,left=0.4in, right=0.4in, top=0.25in, bottom=0.25in
header-includes:
- \pagenumbering{gobble}
---
{r, fig.height = 1.5}
library(ggplot2)
library(patchwork)
p1 <- ggplot(iris, aes(Sepal.Width, Sepal.Length)) + geom_point()
p2 <- ggplot() + geom_blank()
p3 <- p2 + theme_void()
p1 + plot_spacer()
p1 + p1
p1 + p2
p1 + p3
Also note that the plots appear to work in my R session but not when output to pdf:
Finally, I also tried another suggestion to use plot_layout:
design <- "
1#
"
p1 + plot_layout(design = design)
This also seems to work in the session but not in pdf_output.
If I understand you correctly, you should set out.height="50%" in your code chunk. This means that the plots will take 50% of the page. To create a white space plot, I used a theme with white background. You can see this in the code below:
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\newpage
```{r, out.height="50%"}
library(ggplot2)
library(patchwork)
p1 <- ggplot(iris, aes(Sepal.Width, Sepal.Length)) + geom_point()
p2 <- ggplot() + geom_blank() + theme(panel.background = element_rect(fill = 'white', colour = 'white'))
p3 <- p2 + theme_void()
p1 + plot_spacer()
p1 + p1
p1 + p2
p1 + p3
```
In the pdf document you will see that the plots will plot 50% of the page. The output of p1 + plot_spacer() looks like this:
Output of p1 + p1 and p1 + p2:
And finally the output of p1 + p3:
As you can see from the images, the plots are half of the pages in a one column.

Side-By-Side ggplots within rMarkdown using grid.arrange()

I want to display two plots (ggplot) side-by-side in an html report. Most resources suggest grid.arrange() from the package gridExtra is the simplest way to accomplish this. However, grid.arrange() is changing the proportions of my plots (squishing them along the x-axis). I want to maintain axis proportions. I've played with widths= and heights= options but nothing has worked.
If this isn't easily resolved with grid.arrange(), are there other alternatives for displaying images side-by-side?
P.S. I did see altering height of rows produced by grid.arrange when nrow=1, but the solution did not work for me.
Thanks!
{r Images, echo=FALSE}
p1 <- ggplot(pcaData, aes(PC1, PC2, color=By_Experiment, shape=Date)) +
geom_point(size=3) +
xlab(paste0("PC1: ",percentVar[1],"% Variance")) +
ylab(paste0("PC2: ",percentVar[2],"% Variance")) +
labs(title="PCA of Gene Counts (rLog Transformed)") +
theme(axis.line = element_line(color = "black"))
p2 <- ggplot(pcaData, aes(PC1, PC2, color=By_Experiment, shape=Calcium_TP)) +
geom_point(size=3) +
xlab(paste0("PC1: ",percentVar[1],"% Variance")) +
ylab(paste0("PC2: ",percentVar[2],"% Variance")) +
labs(title="PCA of Gene Counts (rLog Transformed)") +
theme(axis.line = element_line(color = "black"))
p1
p2
grid.arrange(p1, p2, ncol=2)
You can control the dimensions of the plots produced in an HTML report by including fig.width and fig.height in the beginning of the code chunk. Try to include {r, fig.width=12, fig.height=4} and see if the plots looks less squashed.

Adding ggplot code chunk to RMarkdown breaks HTML tabset

Newish R programmer here and new to SO...
I have an RMarkdown doc where I've created tabs.
For some reason, when I try to knit, the chunks with ggplot seem to break the tabset code, because when I omit those chunks, the doc appears as it should.
When the ggplot code include = FALSE, the tabs are included, but the below code appears to break the tabs. Any ideas for workarounds? Is this a bug, or my novice-ness? Thanks in advance...
## Options {.tabset .tabset-pills}
```{r Barplot, include=TRUE,echo=FALSE}
barplot_group_count <- ggplot(data=group_count, aes(x=Group, y=n)) +
geom_bar(stat = "identity") +
labs(title = "Count of Groups According to PSA Attributes") +
coord_flip()
barplot_group_count
```
I found that if I wrapped the call to ggplotly(), the graphs are displayed.
barplot_group_count <- ggplot(data=group_count, aes(x=Group, y=n)) +
geom_bar(stat = "identity") +
labs(title = "Count of Groups According to PSA Attributes") +
theme(plot.margin=unit(c(2, 1.5, 1.5, 1.5),"cm"))+
coord_flip()
ggplotly(barplot_group_count)

x-axis is pressed together

I'd like to create a markdown document out of my R script. I managed it to display different barplots, piecharts, a leaflet map and so on.
But it doesn't work with ggplot.
{r, eval=TRUE, echo=TRUE, warning=FALSE, message=FALSE}
p <- qplot() +
theme_classic() +
ggtitle("Title") +
coord_fixed(ratio = 1) +
xlab("wrong") +
ylab("super") +
geom_polygon(data=fortify(data1),
aes(x=long, y=lat, group=group), alpha=0.1, fill=NA, col="black", size=0.1) +
geom_point(data = as.data.frame(data2),
aes(x = coords.x1, y = coords.x2, color = pal1), shape=4, alpha=1)
p + theme(legend.position="right")
The plot looks totally fine if i export it with ggsave, but in the markdown the x axis is pressed together.
Without a reproducible example, it is difficult to work out your problems. However, you could try to change a few lines (each at a time) to see if it makes a difference. Particularly, I would remove the line coord_fixed(ratio = 1) + to see what happens. You could also try different ratio numbers as well to get what you want.
If this does not fix your problem, please provide a reproducible example data.

Add a footnote citation outside of plot area in R?

I'd like to add a footnote citation to my 3-panel facet grid plot produced in R. It's a footnote to credit the data source. I'd ideally like to have it below and external to all three axes---preferably in the lower left.
I'm using ggplot2 and also ggsave(). This means I can't use grid.text()-based solutions, because that only draws on the x11() window, and can't be added to the ggplot object.
Using instead png() ...code... dev.off() does not appear to be an option because I need ggsave's resizing parameters, and find this command produces better, clearer prints (that are also much faster, because I'm not printing to the screen).
Here's my basic code:
p1 <- ggplot(data, aes(date, value))
facet_grid(variable ~ .) + geom_point(aes(y =value), size=1) +
theme_bw() +
opts(title=mytitle)
print(p1)
ggsave("FILE.png",width=mywidth, height=myheight, p1, dpi=90)
I've tried:
p1 <- ggplot(data, aes(date, value))
facet_grid(variable ~ .) + geom_point(aes(y =value), size=1) +
theme_bw() +
opts(title=mytitle)
print(p1)
grid.text(unit(0.1,"npc"),0.025,label = "Data courtesy of Me")
grid.gedit("GRID.text", gp=gpar(fontsize=7))
ggsave("FILE.png",width=mywidth, height=myheight, p1, dpi=90)
This appropriately puts the footnote in the lower left corner on the x11() display, external to the plots, but unfortunately, since it isn't applied to the p1 object, it isn't saved by the ggsave command.
I've also tried:
p1 <- ggplot(data, aes(date, value))
facet_grid(variable ~ .) + geom_point(aes(y =value), size=1) +
theme_bw() +
opts(title=mytitle) +
annotate("text", label = "Footnote", x = 0, y = 10, size = 5, colour = "black") +
print(p1)
ggsave("FILE.png",width=mywidth, height=myheight, p1, dpi=90)
This successfully prints using ggsave, however it has the following problems:
It is repeated 3 times, in each of the 3 facets, rather than 1 time.
It is contained within the plots, rather than external to them.
Text is difficult to place---seems to be using plot units (my x-axis is date, so 0 puts it around 1970).
The text size doesn't seem to change despite my size parameter.
A couple of related links from when I explored this...
ggplot2 footnote
(doesn't work with ggsave)
How to label the barplot in ggplot with the labels in another test result?
(is inside the plot, not external/below plot)
Different font faces and sizes within label text entries in ggplot2
(doesn't work with ggsave)
problem saving pdf file in R with ggplot2
ggplot2 now has this ability natively with no need for additional packages. ... + labs(caption = "footnote", ...)
library(ggplot2)
ggplot(diamonds, aes(carat, price, color = clarity)) +
geom_point() +
labs(title = "Diamonds are forever...",
subtitle = "Carat weight by Price",
caption = "H. Wickham. ggplot2: Elegant Graphics for Data Analysis Springer-Verlag New York, 2009.")
library(gridExtra)
library(grid)
library(ggplot2)
g <- grid.arrange(qplot(1:10, 1:10, colour=1:10) + labs(caption="ggplot2 caption"),
bottom = textGrob("grid caption", x = 1,
hjust = 1, gp = gpar(fontface = 3L, fontsize = 9)))
ggsave("plot.pdf", g)
Edit: note that this solution is somewhat complementary to the recent caption argument added to ggplot2, since the textGrob can here be aligned with respect to the whole figure, not just the plot panel.
Adding to the answer of Brandon Bertelsen: if you want to have the caption in the left corner, add
theme(plot.caption = element_text(hjust = 0))

Resources