Search code examples
rggplot2plotlydata-visualization

R: How to control font size of legend map in plotly/ggplotly?


How can I control the font size of the legend map in plotly/ggplotly?

enter image description here


Solution

  • You can change the font size of the legend using legend.text in your theme like this:

    library(dplyr)
    library(ggplot2)
    library(plotly)
    p <- iris %>%
      ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
      geom_point() +
      theme(legend.text=element_text(size=3))
    
    ggplotly(p)
    

    p <- iris %>%
      ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
      geom_point() +
      theme(legend.text=element_text(size=15))
    
    ggplotly(p)
    

    Created on 2022-07-15 by the reprex package (v2.0.1)