How to place legend inside caption area on R

You could just push the legend to the bottom and add a custom title – inlcude a counter to get numbered Plots:

library(ggplot2)
# using the internal iris dataset
iris
# set counter to zero
NR <- 0

# Add counter and build plot
NR <- NR + 1
legend <- paste0("Fig. ", NR, ": This is a Plot about...:")
ggplot2::ggplot(iris, aes(Sepal.Length, Petal.Width, color = Species)) +
  ggplot2::geom_point() +
  ggplot2::labs(color= legend) +
  ggplot2::theme(legend.position = "bottom")


# Add counter and build second plot
NR <- NR + 1
legend <- paste0("Fig. ", NR , ": This is a Plot about...:")
ggplot2::ggplot(iris, aes(Sepal.Width, Petal.Length, color = Species)) +
  ggplot2::geom_point() +
  ggplot2::labs(color= legend) +
  ggplot2::theme(legend.position = "bottom")

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top