Search code examples
rfontsplotly

R plotly layout font weight


I'm using a custom font for a plot that seems to read normally in ggplot but is rendering slightly more bold in plotly. From looking at the R plotly documentation, it seems like I could set a different value for layout.font.weight. Even with a system font, though, this doesn't seem to work:

library(tidyverse)
library(datasets)
data(iris)
library(plotly)


df <- iris %>%
  group_by(Species) %>%
  summarise(avg_petal_length = mean(Petal.Length))


fig_1 <- plot_ly(df,
               x = ~Species, y =~avg_petal_length, type = "bar") %>%
  layout(font = list(weight = 1000,
                     size = 18))

fig_1

Initial output

If I try to decrease the font weight, though, nothing seems to happen.

fig_2 <- plot_ly(df,
                 x = ~Species, y =~avg_petal_length, type = "bar") %>%
  layout(font = list(weight = 100,
                     size = 18))

fig_2

Attempt to change font weight

I'm by no means a plotly expert, so any insight into what's happening would be really helpful.


Solution

  • By nature, once a static ggplot2 plot is made interactive, such as by using ggplotly(), the font weight of the axis labels changes and tend to become bolder.

    However, if you are building the plot from scratch with plotly, then you can play around with the font family in the layout() function as demonstrated below. This seems to be the only way to manipulate font weight in plotly:

    fig_1 <- plot_ly(df,
                     x = ~Species, y =~avg_petal_length, type = "bar") %>%
      layout(font = list(size = 20,
                         family = "Arial Black"))
    
    fig_1
    

    enter image description here

    Using Arial black makes the axis labels thicker. Nevertheless, you can try other Google fonts that are not as bold as the Arial font family according to your need.