Learn R Programming

smplot2 (version 0.2.6)

sm_add_legend: Adding a common legend on a combined figure

Description

Adding a common legend on a combined figure

Usage

sm_add_legend(
  combined_plot,
  x,
  y,
  sampleplot,
  legend,
  direction = "vertical",
  border = TRUE,
  legend_spacing = 0.5,
  border_color = "black",
  font_size = 12
)

Value

It prints a legend on a a combined plot. It can be used to create a common legend for subplots.

Arguments

combined_plot

Combined figure, an output from sm_put_together().

x

Location of the legend along the x-axis of the combined figure. The middle origin is at 0.5.

y

Location of the legend along the y-axis of the combined figure. The middle origin is at 0.5.

sampleplot

A variable containing one sample ggplot2 from which the legend can be derived.

legend

Pre-specified layer of legend created with sm_common_legend().

direction

Direction of the legend: 'horizontal' or 'vertical'.

border

If set TRUE, border around the legend will be created. If set FALSE, the border will be removed.

legend_spacing

Spacing within the legend.

border_color

Color of the legend border

font_size

Text size of the legend

Examples

Run this code
library(ggplot2)
library(smplot2)

ggplot(data = mtcars, mapping = aes(
  x = drat, y = mpg,
  fill = as.factor(cyl)
)) +
  geom_point(
    shape = 21, color = "white",
    size = 3
  ) +
  sm_classic(legends = FALSE) -> p1

ggplot(data = mtcars, mapping = aes(
  x = drat, y = mpg,
  fill = as.factor(cyl)
)) +
  geom_point(
    shape = 21, color = "white",
    size = 3
  ) +
  sm_hvgrid(legends = FALSE) -> p2

combined_fig <- sm_put_together(list(p1, p2), ncol = 2, nrow = 1)
sm_add_legend(combined_fig, x = 0.1, y = 0.1, sampleplot = p1)

Run the code above in your browser using DataLab