I have made a plot like the one described by the code below resulting in the posted image. I can not figure out how to set the entire background to the same "grey80"-color I have used when defining the subplots. Ie. I want to color the white areas between the plots and on the sides of the legend in the same color.
Is this possible to achieve, perhaps with some fancy gridgrob-magic?
This question is similar to change the background color of grid.arrange output but I would like a solution without using the png() function, if possible
library(ggplot2)
library(gridExtra)
library(ggthemes)
library(grid)
p1 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width,
colour = Species)) +
ggtitle('Sepal') +
geom_point() + theme_bw() +
theme(rect = element_rect(fill = 'grey80'))
p2 <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width,
colour = Species)) +
ggtitle('Petal') +
geom_point() + theme_bw() +
theme(rect = element_rect(fill = 'grey80'))
grid_arrange_shared_legend <- function(...) {
plots <- list(...)
g <- ggplotGrob(plots[[1]] + theme(legend.position = "bottom"))$grobs
legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
lheight <- sum(legend$height)
grid.arrange(
do.call(arrangeGrob, lapply(plots, function(x)
x + theme(legend.position="none"))),
legend,
ncol = 1,
heights = unit.c(unit(1, "npc") - lheight, lheight))
}
grid_arrange_shared_legend(p1,p2)
upgrade comment
You can do this by adding a grey background to the graphics window and
then adding the plots on top. As your legend function uses grid.arrange this generates a newpage; so either add newpage=FALSE or change to arrangeGrob to your function.
Your example
library(ggplot2)
library(gridExtra)
library(ggthemes)
library(grid)
p1 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species)) +
ggtitle('Sepal') +
geom_point() + theme_bw() +
# by adding colour=grey removes the white border of the plot and
# so removes the lines between the plots
# add panel.background = element_rect(fill = "grey80")
# if you want the plot panel grey aswell
theme(plot.background=element_rect(fill="grey80", colour="grey80"),
rect = element_rect(fill = 'grey80'))
p2 <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width, colour = Species)) +
ggtitle('Petal') +
geom_point() + theme_bw() +
theme(plot.background=element_rect(fill="grey80", colour="grey80"),
rect = element_rect(fill = 'grey80'))
Tweal your function
# Change grid.arrange to arrangeGrob
grid_arrange_shared_legend <- function(...) {
plots <- list(...)
g <- ggplotGrob(plots[[1]] + theme(legend.position = "bottom"))$grobs
legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
lheight <- sum(legend$height)
arrangeGrob( # change here
do.call(arrangeGrob, lapply(plots, function(x)
x + theme(legend.position="none"))),
legend,
ncol = 1,
heights = unit.c(unit(1, "npc") - lheight, lheight))
}
Plot
grid.draw(grobTree(rectGrob(gp=gpar(fill="grey80", lwd=0)),
grid_arrange_shared_legend(p1,p2)))
Which gives
I think you could take advantage of the ggdraw() function from the cowplot package as showed here.
In your case, you would just need to add plot.background = element_rect(fill="grey80", color = NA) in the theme() of each plot p1 and p2, stitch them together with your function grid_arrange_shared_legend() and then call ggdraw() on its output:
g <- grid_arrange_shared_legend(p1,p2)
g2 <- cowplot::ggdraw(g) +
# same plot.background should be in the theme of p1 and p2 as mentioned above
theme(plot.background = element_rect(fill="grey80", color = NA))
plot(g2)
Related
A similar question was asked here, however I cant adapt the answer to my issue.
I am trying to correctly align two legends when using cowplot. For example, if I create some data and a cowplot with two legends like so:
library(cowplot)
library(ggplot2)
# create some data
dat <- NULL
for(i in 1:20){
x <- LETTERS[1:5]
y <- paste0("var", seq(1,5))
dat[[i]] <- expand.grid(X=x, Y=y)
dat[[i]]$Z <- runif(25, 0, 1)
}
# plotting function
plotFun <- function(data){
ggplot(data, aes(X, Y, fill= Z)) +
geom_tile() +
theme(aspect.ratio = 1,
legend.justification = c(0,1),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank()) +
xlab("") + ylab("")
}
# set up to plot on a grid
allPlots <- lapply(dat, plotFun)
allPlotsAlter <- lapply(allPlots, function(x) x + theme(legend.position = "none"))
n <- length(allPlotsAlter)
nRow <- floor(sqrt(n))
plotGrid <- gridExtra::arrangeGrob(grobs=allPlotsAlter, nrow=nRow)
# create a different type of legend
newPlot <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, fill = Species)) +
geom_bar(stat = 'identity') + theme(legend.justification = c(0,1))
# get both legends and combine
legend <- cowplot::get_legend(allPlots[[1]])
legend1 <- cowplot::get_legend(newPlot)
combineLegend <- cowplot::plot_grid(
legend,
legend1,
nrow = 2)
# now make plot
cowplot::plot_grid(plotGrid,
combineLegend,
rel_widths = c(0.9, 0.11),
ncol = 2)
That creates this type of plot:
As you can see, the two legends have quite a bit of vertical space between them and they are not centred with the plot.
Is there a way to align the two legends so they look something like this:
I'm not sure if it is possible using cowplot... or is there a way to maybe use ggplot's annotate to place the legends?
I would probably go for patchwork, as Stefan suggests, but within cowplot you probably need to adjust the legend margins:
theme_margin <- theme(legend.box.margin = margin(100, 10, 100, 10))
legend <- cowplot::get_legend(allPlots[[1]] + theme_margin)
legend1 <- cowplot::get_legend(newPlot + theme_margin)
combineLegend <- cowplot::plot_grid(
legend,
legend1,
nrow = 2)
# now make plot
cowplot::plot_grid(plotGrid,
combineLegend,
rel_widths = c(0.9, 0.11),
ncol = 2)
If switching to another package is an option for you I would suggest to use patchwork to glue your plots together. One feature offered by patchwork is that using plot_spacer you could easily add some empty panels above and below your legends to "move" them to the center and thereby getting rid of the empty space. Depending on your final result or the height of your final plot you probably have to play a bit around with the heights and/or widths arguments:
library(cowplot)
library(ggplot2)
library(patchwork)
set.seed(123)
# create some data
dat <- NULL
for (i in 1:20) {
x <- LETTERS[1:5]
y <- paste0("var", seq(1, 5))
dat[[i]] <- expand.grid(X = x, Y = y)
dat[[i]]$Z <- runif(25, 0, 1)
}
# plotting function
plotFun <- function(data) {
ggplot(data, aes(X, Y, fill = Z)) +
geom_tile() +
theme(
aspect.ratio = 1,
legend.justification = c(0, 1),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank()
) +
labs(x = NULL, y = NULL)
}
# set up to plot on a grid
allPlots <- lapply(dat, plotFun)
allPlotsAlter <- lapply(allPlots, function(x) x + theme(legend.position = "none"))
n <- length(allPlotsAlter)
nRow <- floor(sqrt(n))
plotGrid <- wrap_plots(grobs = allPlotsAlter, nrow = nRow)
# create a different type of legend
newPlot <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, fill = Species)) +
geom_bar(stat = "identity") +
theme(legend.justification = c(0, 1))
# get both legends and combine
legend <- cowplot::get_legend(allPlots[[1]])
legend1 <- cowplot::get_legend(newPlot)
combineLegend <- plot_spacer() + legend + legend1 + plot_spacer() + plot_layout(ncol = 1, heights = c(.5, 1, 1, .5))
wrap_elements(plotGrid) + combineLegend + plot_layout(widths = c(4, 1))
I have three different subplotts, each with their own legend. I want to combine each of these 3 legends into one common legend at the bottom of the plot. I have found many similar questions combining the legends of different sub plots into one common legend when all the subplots had the same legend. Yet, not when the legends are different. Attempts to change the code were not succesful.
grid_arrange_shared_legend <- function(...) {
plots <- list(...)
g <- ggplotGrob(plots[[1]] + theme(legend.position = "bottom"))$grobs
legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
lheight <- sum(legend$height)
grid.arrange(
do.call(arrangeGrob, lapply(plots, function(x)
x + theme(legend.position="none"))),
legend,
ncol = 1,
heights = unit.c(unit(1, "npc") - lheight, lheight))
}
data = read.table("fermentation_run.csv", header=TRUE, sep=",", fileEncoding="UTF-8-BOM")
p1 <- ggplot(data, aes(x = time)) +
geom_line(aes(y = cdw*5, colour = "CDW"), size=1) +
geom_line(aes(y = glucose, colour = "glucose"), size=1) +
geom_step(aes(y = substrate, colour = "substrate"), size=1) +
theme_classic() + ylab("Concentration (g/l)") +
xlab("Time (h)") +
scale_colour_manual(values = c("grey", "red", "black"))
theme(legend.position="bottom", legend.title=element_blank())
p2 <- ggplot(data, aes(x=time)) +
geom_line(aes(y = alkyl, colour = "alkyl SS"), size=1) +
geom_line(aes(y = oleyl, colour = "oleyl alcohol"), size=1) +
theme_classic() +
xlab("Time (h)") +
ylab("Concentration (g/l)") +
scale_colour_manual(values = c("green", "blue"))
theme(legend.position="bottom", legend.title=element_blank())
p3 <- ggplot(data, aes(x=time)) +
geom_step(aes(y = aeration, colour="aeration"), size=1) +
geom_line(aes(y = do/2, colour="dissolved oxygen"), size=1) +
theme_classic() +
xlab("Time (h)") +
ylab("Aeration (lpm)") +
scale_y_continuous(sec.axis = sec_axis(~.*2, name = "Dissolved oxygen (%)")) +
theme(legend.position="bottom", legend.title=element_blank())
grid_arrange_shared_legend(p1, p2,p3)
This returns only the legend of the first plot and not of the three plots combined.
I think the key is to add all the legends in your first plot. To achieve this, you could add some fake rows in your data and label them according to your legends for all plots. Let's assume those legends are "a", "b", "c", "d", "e", and "f" in the following:
library(tidyverse)
# insert several rows with values outside your plot range
data <- add_row(mtcars,am=c(2, 3, 4, 5), mpg = 35, disp = 900)
data1<-data %>%
mutate (
by1 = factor(am, levels = c(0, 1, 2, 3, 4, 5),
labels = c("a", "b","c","d", "e","f")))
p1 <- ggplot(data1, aes(x = mpg, y=disp, col=by1)) +
geom_point() +
ylim(50,500)
You will get all the legends you need, and grid_arrange_shared_legend(p1, p2,p3) will pick up this. As you can see only "a" and "b" are for the first plot, and the rest are for other plots.
I don't have your data so I'll illustrate it with some basic datasets. The method isn't perfect with respect to some whitespace around the legends, but maybe someone in the comments knows a solution.
The answer I'm proposing is getting dirty with gtables and patchwork and internal functions thereof.
library(ggplot2)
library(grid)
library(patchwork) #https://github.com/thomasp85/patchwork
# Make plots as usual
g1 <- ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
geom_point(aes(colour = Species))
g2 <- ggplot(mtcars, aes(mpg, disp)) +
geom_point(aes(colour = as.factor(cyl)))
# specify a legend position and a orientation for plots
position <- "bottom"
orientation <- "vertical"
# Add as many plots as you want to this list
plots <- list(g1, g2)
# Grab legends from plots in list
legends <- lapply(plots, function(p) {
p <- ggplotGrob(p + theme(legend.position = position))$grobs
p[[which(sapply(p, function(x) x$name) == "guide-box")]]
})
# Combine the legends
legend <- switch(position,
"bottom" = do.call(gtable:::cbind.gtable, legends),
"right" = do.call(gtable:::rbind.gtable, legends))
# Now make versions of the plots without the legend
stripped <- lapply(plots, function(p) p + theme(legend.position = "none"))
# Combine all the plots
stripped <- switch(orientation,
"horizontal" = do.call(patchwork:::ggplot_add.ggplot, stripped),
"vertical" = do.call(patchwork:::`/.ggplot`, stripped))
# Combine plots with legend
out <- switch(position,
"bottom" = stripped / legend,
"right" = stripped + legend)
out
Created on 2019-08-17 by the reprex package (v0.3.0)
If the whitespace really is a problem, you could supply a plot layout, but this would have to be a manual judgement to make:
out + plot_layout(heights = c(1,1,0.2))
I have been using the plot_grid command from cowplot to arrange my plots. I use the labeling feature, and my plots all look the same in that regard. However, when I 'hv' align some plots that have very different y-axis limits, such as the one below, it appears the height of the plot with shortest range of y is used.
If I just 'v' align the plot it looks better in some respects, but it is hard to resize the plot and have the labels looking good. I'd prefer the plot height not consider the x-axis labels, etc, like above.
Using gtables, I can get the desired width/height (below), but these leaves me without the consistent labels across all the figures in a document. Can I use the 'hv' alignment with cowplot and specify which plot height to use?
library(ggplot2)
library(dplyr)
library(scales)
library(grid)
library(cowplot)
data(iris)
iris <- iris %>% mutate(Petal.Width2 = ifelse(Species == "setosa", Petal.Width * 75, Petal.Width))
p1 <- ggplot(data=iris, aes(x = factor(Species), y=Sepal.Width)) +
geom_bar(stat="identity") +
labs(x = NULL, y = "Plot One") +
scale_y_continuous(labels = percent) +
theme(axis.text.x = element_blank(),
axis.title.y = element_text(vjust=1), plot.margin=unit(c(2,2,0,2),"mm"))
p2 <- ggplot(data=iris, aes(x = factor(Species), y=Petal.Width2)) + geom_bar(stat="identity") +
labs(x = NULL, y = "Plot Two") +
scale_y_continuous(labels = percent) +
theme(axis.text.x = element_blank(),
axis.title.y = element_text(vjust=1), plot.margin=unit(c(0,2,0,2),"mm"))
p3 <- ggplot(data=iris, aes(x = factor(Species), y=Petal.Length*0+.01)) + geom_bar(stat="identity") +
labs(x = "SPECIES", y = "The Third plot") +
scale_y_continuous(labels = percent) +
theme( axis.title.y = element_text(vjust=1, color="blue"), plot.margin=unit(c(0,2,0,2),"mm"),
axis.text.x = element_text(angle = 90, hjust=1, vjust=1,face ="italic", size=10))
plot_grid(p1,p2,p3,ncol=1, align="v", labels=c("A", "B", "C"))
# https://stackoverflow.com/a/27408589/1670053
plots <- list(p1, p2, p3)
grobs = lapply(plots, ggplotGrob)
g = do.call(rbind, c(grobs, size="first"))
g$widths = do.call(unit.pmax, lapply(grobs, "[[", "widths"))
grid.newpage()
grid.draw(g)
it's easy as to add labels,
plots <- list(p1, p2, p3)
grobs = lapply(plots, ggplotGrob)
library(gridExtra)
g = do.call(rbind, grobs) # uses gridExtra::rbind.gtable
panels <- g$layout[g$layout$name=="panel",]
g <- gtable::gtable_add_grob(g, lapply(LETTERS[1:nrow(panels)],
textGrob, vjust=1, y=1,
gp=gpar(fontface=2)),
t=panels$t, l=2)
grid.newpage()
grid.draw(g)
I am attempting to use grobs and gtable to arrange 4 (ggplot2) plots into a 2x2 grid. I don't know how to set widths, and also a non- 1xn, or nx1 arrangement.
Using this code:
data(iris)
a <- ggplot(iris, aes(x=Species, y=Petal.Width)) + geom_boxplot(color="black") + ylab(expression(Foo~Bar~(g~cm^{-3})))
b <- ggplot(iris, aes(x=Species, y=Petal.Length*100)) + geom_boxplot(color="black") + ylab("foobar (mm)")
c <- ggplot(iris, aes(x=Species, y=Sepal.Width)) + geom_boxplot(color="black") + ylab("foobar (%)")
d <- ggplot(iris, aes(x=Species, y=log10(Sepal.Length))) + geom_boxplot(color="black") + ylab("foobar (cm)")
plots <- list(a,b,c,d)
grobs = lapply(plots, ggplotGrob)
g = do.call(rbind, c(grobs, size="first"))
g$widths = do.call(unit.pmax, lapply(grobs, "[[", "widths"))
grid.newpage()
grid.draw(g)
I can create the following 1x4 arrangement.
If I use grid.arrange for two columns, on the 4 plots, the plots are of different widths.
How can I bind plots into a gtable for a 4 x 4 arrangement?
# I thought maybe I could cbind, then rbind, but this does not work
plots1 <- list(a,b)
plots2 <- list(c,d)
grobs1 = lapply(plots1, ggplotGrob)
grobs2 = lapply(plots2, ggplotGrob)
g1 = do.call(cbind, c(grobs1, size="first"))
g2 = do.call(cbind, c(grobs2, size="first"))
# g3 = do.call(rbind, c(g1,g2, size="first")) #this does not work
I think you already had the answer.
Your last line returns an error, but a small edit results in a combined plot where widths within columns are the same:
g3 = do.call(rbind, c(list(g1,g2), size="first")) #combine g1 and g2 into a list
A sidenote for aesthetics/reference:
If your x-axis is the same, you can drop it from the top two plots.
library(ggplot2); library(gridExtra); library(grid)
# Tweak the margins to use up empty space. Margins: Top, Right, Bottom, Left
# For reference: a1= top left, b1= top right
# c1= bottom left, d1= bottom right
a1 <- a + theme(axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x= element_blank(),
plot.margin= unit(c(1, 1, -0.5, 0.5), "lines") )
b1 <- b + theme(axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x= element_blank(),
plot.margin= unit(c(1, 1, -0.5, 0.5), "lines") )
c1 <- c + theme(plot.margin= unit(c(0, 1, 0.5, 0.5), "lines") )
d1 <- d + theme(plot.margin= unit(c(0, 1, 0.5, 0.5), "lines") )
grobz <- lapply(list(a1, b1, c1, d1), ggplotGrob)
grobz.plot <- arrangeGrob( grobs = list(rbind(grobz[[1]], grobz[[3]], size = "last"),
rbind(grobz[[2]], grobz[[4]], size = "last")),
ncol = 2)
grid.draw(grobz.plot)
These StackOverflow questions are helpful in aligning plots:
Using rbind in gtable to set plot width (Baptiste) [link]
Relative panel heights in gtable (Baptiste) [link]
Plot widths and legends [link]
Pretty similar to the above, but using gtable functions*
library(ggplot2)
pl <- list(ggplot() + xlab("x"),
ggplot() + ylab("y"),
ggplot() + ylab("y"),
ggplot() + ggtitle("title") + xlab("x"))
library(grid)
library(gridExtra)
gl <- lapply(pl, ggplotGrob)
# gt <- cbind(rbind(gl[[1]], gl[[3]]),
# rbind(gl[[2]], gl[[4]]))
# alternative to remove x-axes of top row of plots
gt <- cbind(rbind(gl[[1]][1:3,], gl[[3]]),
rbind(gl[[2]][1:3,], gl[[4]]))
grid.newpage()
grid.draw(gt)
*: actually, since gtable doesn't allow the use of pmax when comparing units, this is using a drop-in replacement from the dev version of gridExtra.
would this work for you
library(cowplot)
library(ggplot2)
data(iris)
a <- ggplot(iris, aes(x=Species, y=Petal.Width)) + geom_boxplot(color="black") + ylab(expression(Foo~Bar~(g~cm^{-3}))) + theme_grey()
b <- ggplot(iris, aes(x=Species, y=Petal.Length*100)) + geom_boxplot(color="black") + ylab("foobar (mm)") + theme_grey()
c <- ggplot(iris, aes(x=Species, y=Sepal.Width)) + geom_boxplot(color="black") + ylab("foobar (%)") + theme_grey()
d <- ggplot(iris, aes(x=Species, y=log10(Sepal.Length))) + geom_boxplot(color="black") + ylab("foobar (cm)") + theme_grey()
plot_grid(a,b, c, d, ncol=2,align="v")
I would like plot a line below a ggplot2 graph with text above it, something like this:
where the starting and ending point of the gene on the x axis can be specified.
My attempt so far:
require(ggplot2)
require(grid)
require(gridExtra)
data = data.frame(y = -log10(runif(100)), x = 1:100)
p = ggplot(data=data, aes(x, y)) + geom_point()
p = p + theme(plot.margin=unit(c(1, 1, 5, 1), "lines"))
t1 = textGrob("Gene1")
p1 = p + annotation_custom(grob=t1, xmin=0, ymin=0, xmax = 3, ymax=-.1)
print(p1)
which gives:
If I try to move the text down by adjusting ymax, then it disappears.
In my answer, I have changed a couple things:
1 - I changed the name of your data to "df", as "data" can cause confusion between objects and arguments.
2 - I removed the extra panel space around the main data plot, so that the annotation wasn't so far away.
require(ggplot2)
require(grid)
require(gridExtra)
# make the data
df <- data.frame(y = -log10(runif(100)), x = 1:100)
p <- ggplot(data=df, aes(x, y)) + geom_point()
# remove this line of code:
# p <- p + theme(plot.margin=unit(c(1, 1, 5, 1), "lines"))
# set up the plot theme for the annotation
blank_axes_and_thin_margin <- theme(axis.text = element_text(color="white"),
axis.title = element_text(color="white"),
axis.ticks = element_blank(),
panel.grid = element_blank(),
panel.border = element_blank(),
plot.margin=unit(c(0, 2, 0,2),"mm"))
# define the position of the arrow (you would change this part)
arrow_start <- min(df$x)
arrow_end <- mean(c(min(df$x), max(df$x)))
arrow_height <- 1
# here's the rectangle with the arrow
t2 <- ggplot(df, aes(x,y))+
theme_bw()+
geom_rect(aes(xmin=min(x), xmax = max(x)),
ymin=0, ymax=4,fill="gray50")+
coord_cartesian(ylim=c(0,4))+
annotate(geom="text", label="Gene1",
x=20, y=2, size=6, color="black")+
geom_segment(x=arrow_start, xend=arrow_end,
y=arrow_height, yend=arrow_height,
color="black", arrow=arrow(ends="both"))+
blank_axes_and_thin_margin
t2
# arrange the graphic objects here
# I use arrangeGrob because it allows you to use ggsave(), unlike grid.arrange
plot_both <- arrangeGrob(p, t2, nrow=2, heights=unit(c(0.75,0.25), "null"))
plot_both
# ta-da !
you can turn clipping off,
g <- ggplotGrob(p1)
g$layout$clip[g$layout$name == "panel"] <- "off"
grid.newpage()
grid.draw(g)