Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multiple ggplot legends: How do you adjust one of them?

I would like to access only of the legend below, carb and setting the font size to 20. How can I do that. The code below changes both legends.

library(tidyverse)

ggplot(mtcars, aes(x = mpg, y = disp, color = carb, shape = as.factor(am))) +
  geom_point() +
  theme(legend.title = element_text(size = 20))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can set individual theme elements for a legend inside the function guides(). In your case, the legend for carb is a colorbar mapped to the color aesthetic, so we can do

ggplot(mtcars, aes(x = mpg, y = disp, color = carb, shape = as.factor(am))) +
  geom_point() +
  guides(color = guide_colorbar(
           theme = theme(legend.title = element_text(size = 20))
    )
  )

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading