Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align shared legends to the center of the plot grid (with cowplot)

Tags:

r

ggplot2

cowplot

The reproducible example can be found in this tutorial for the package cowplot.

https://cran.r-project.org/web/packages/cowplot/vignettes/shared_legends.html

Copying example code:

library(ggplot2)
library(cowplot)
#down-sampled diamonds data set
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]

# Make three plots.
# We set left and right margins to 0 to remove unnecessary spacing in the
# final plot arrangement.
p1 <- qplot(carat, price, data=dsamp, colour=clarity) +
  theme(plot.margin = unit(c(6,0,6,0), "pt"))
p2 <- qplot(depth, price, data=dsamp, colour=clarity) +
  theme(plot.margin = unit(c(6,0,6,0), "pt")) + ylab("")
p3 <- qplot(color, price, data=dsamp, colour=clarity) +
  theme(plot.margin = unit(c(6,0,6,0), "pt")) + ylab("")

# arrange the three plots in a single row
prow <- plot_grid( p1 + theme(legend.position="none"),
           p2 + theme(legend.position="none"),
           p3 + theme(legend.position="none"),
           align = 'vh',
           labels = c("A", "B", "C"),
           hjust = -1,
           nrow = 1
           )
legend_b <- get_legend(p1 + theme(legend.position="bottom"))

# add the legend underneath the row we made earlier. Give it 10% of the height
# of one plot (via rel_heights).
p <- plot_grid( prow, legend_b, ncol = 1, rel_heights = c(1, .2))
p

This example shows a plot in which the legend is drawn aligned to the bottom left of the grid. However, it used to be differently, as the legend was then drawn aligned to the bottom center of the plot. Here is an example generated by my personal code some months ago. https://s1.postimg.org/8pf2en1zen/Untitled.png (Upload tool currently not working for me)

Rerunning my old code after an unknown amount of changes in either packages delivers a legend aligned to the bottom left (as also shown in the tutorial, third plot from above): https://s1.postimg.org/3agjw7n9gf/Untitled2.png

The question is how to adjust the code to draw the legend aligned to bottom center.

like image 547
nouse Avatar asked Oct 09 '17 11:10

nouse


People also ask

How to create a Cowplot grid with shared legend in R?

To create a cowplot grid with shared legend there is no built-in method but the functionality can be achieved by following these steps: Step 1: Create plots to be put in the grid without legend using: Step 2: Now combine both plots using the plot_grid () function and store that in a variable:

How to combine two plots into one plot in Python Cowplot?

We will use plot_grid () function with no legend for each plot we have and store the result as a variable. Next, we will create a new variable with legend from one of the plots such that the legend is at the bottom of our new combined plot. We can combine the two new plot objects with plot_grid () function in cowplot.

How do I make compound plots with a shared legend?

This vignette demonstrates how to make compound plots with a shared legend. We begin with a row of three plots, without legend. Now we add the legend back in manually. We can place the legend to the side of the plots.

How to add a legend to the bottom of a plot?

Give it one-third of # the width of one plot (via rel_widths). plot_grid ( prow, legend, rel_widths = c ( 3, .4 )) Or we can place the legend at the bottom.


1 Answers

you can set legend_b this way:

legend_b <- get_legend(p1 + theme(legend.position=c(0.3,0.8),legend.direction = "horizontal"))

a better way:

legend_b <- get_legend(p1 + theme(legend.direction = "horizontal",legend.justification="center" ,legend.box.just = "bottom"))
like image 193
Zeinab Ghaffarnasab Avatar answered Sep 16 '22 13:09

Zeinab Ghaffarnasab