Setting the Font, Title, Legend Entries, and Axis Titles in R

How to set the global font, title, legend-entries, and axis-titles in R.


New to Plotly?

Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

Setting the Font, Title, Legend Entries, and Axis Titles in R

How to set the global font, title, legend-entries, and axis-titles in for plots in R.

Automatic Labelling with Plotly

When using Plotly, your axes is automatically labelled, and it's easy to override the automation for a customized figure using the labels keyword argument. The title of your figure is up to you though!

Here's a figure with automatic labels and then the same figure with overridden labels. Note the fact that when overriding labels, the axes, legend title and hover labels reflect the specified labels automatically.

library(plotly)

data("iris")

fig1 <-  plot_ly(data = iris ,x =  ~Sepal.Length, y = ~Sepal.Width, color = ~Species, type = 'scatter', mode = 'markers')%>%
        layout(title = 'Automatic Labels Based on Data Frame Column Names', plot_bgcolor = "#e5ecf6")
fig1
#Manually specifying labels

fig2 <-  plot_ly(data = iris ,x =  ~Sepal.Length, y = ~Sepal.Width, color = ~Species, type = 'scatter', mode = 'markers')%>%
  layout(title = 'Manually Specified Labels', plot_bgcolor = "#e5ecf6", xaxis = list(title = 'Sepal Length (cm)'), 
         yaxis = list(title = 'Sepal Width (cm)'), legend = list(title=list(text='<b> Species of Iris </b>')))
fig2

Global and Local Font Specification

You can set the figure-wide font with the layout.font.family attribute, which will apply to all titles and tick labels, but this can be overridden for specific plot items like individual axes and legend titles etc. In the following figure, we set the figure-wide font to Courier New in blue, and then override this for certain parts of the figure.

library(plotly)
data(iris)

t <- list(
  family = "Courier New",
  size = 14,
  color = "blue")
t1 <- list(
  family = "Times New Roman",
  color = "red"
)
t2 <- list(
  family = "Courier New",
  size = 14,
  color = "green")
t3 <- list(family = 'Arial')

fig <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Sepal.Width, color = ~Species, 
               type = 'scatter', mode = 'markers')%>% 
  layout(title= list(text = "Playing with Fonts",font = t1), font=t, 
         legend=list(title=list(text='Species',font = t2)), 
         xaxis = list(title = list(text ='Sepal.Length', font = t3)),
         plot_bgcolor='#e5ecf6')
fig

Fonts and Labels in Dash

Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash for R at https://dashr.plot.ly/installation.

Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument.

library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)
library(devtools)
library(plotly)
library(dashDaq)
data("iris")


app <- Dash$new()

app$layout(
  htmlDiv(
    list(
      dccGraph(id = 'graph'),
      daqColorPicker(id = 'font',
                     label = 'Font Color',
                     value = list(hex = "#119DFF")),
      daqColorPicker(id = 'title',
                     label = 'Title Color',
                     value = list(hex = "#2A0203"))

    )
  )
)

app$callback(
  output(id = 'graph', property='figure'),
  params=list(input(id='font', property='value'), 
              input(id='title', property='value')),
  function(font_color, title_color) {
    t <- list(
      family = "Courier New",
      size = 14,
      color = strsplit(toString(font_color), split = ",")[[1]][1]
      )
    t1 <- list(
      family = "Times New Roman",
      color = strsplit(toString(title_color), split = ",")[[1]][1]
    )
    t2 <- list(
      family = "Courier New",
      size = 14,
      color = strsplit(toString(font_color), split = ",")[[1]][1])
    t3 <- list(family = 'Arial')

    fig <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Sepal.Width, color = ~Species, 
                   type = 'scatter', mode = 'markers')%>% 
      layout(title= list(text = "Playing with Fonts",font = t1), font=t, 
             legend=list(title=list(text='Species',font = t2)), 
             xaxis = list(title = list(text ='Sepal.Length', font = t3)))

    return(fig)
  })

After executing this code, give app$run_server() in the console to start the dash.

Manual Labelling in Plotly

Explicitly Labeling traces and axes in Plotly.

library(plotly)
t <- list(
  family = "Courier New, monospace",
  size = 15,
  color = "RebeccaPurple")
x1 = c(0, 1, 2, 3, 4, 5, 6, 7, 8)
y1 = c(0, 1, 2, 3, 4, 5, 6, 7, 8)

x2 = c(0, 1, 2, 3, 4, 5, 6, 7, 8)
y2 = c(1, 0, 3, 2, 5, 4, 7, 6, 8)

df = data.frame(x1, y1, x2, y2)

fig <-  plot_ly()%>%
  add_trace(df, x = ~x1, y = ~y1, type = 'scatter', mode = 'lines+markers', name = 'Name of Trace 1')%>%
  add_trace(df, x = ~x2, y = ~y2, type = 'scatter', mode = 'lines+markers', name = 'Name of Trace 2')%>%
  layout(title = 'Plot Title', xaxis = list(title = 'X Axis Title'), font=t, plot_bgcolor = "#e5ecf6",
         yaxis = list(title = 'Y Axis Title'), legend = list(title=list(text='Legend Title')))

fig

The configuration of the legend is discussed in detail in the Legends page.

Align Plot Title

The following example shows how to align the plot title in layout.title. x sets the x position with respect to xref from "0" (left) to "1" (right), and y sets the y position with respect to yref from "0" (bottom) to "1" (top). Moreover, you can define xanchor to left,right, or center for setting the title's horizontal alignment with respect to its x position, and/or yanchor to top, bottom, or middle for setting the title's vertical alignment with respect to its y position.

library(plotly)

fig <- plot_ly(x= c('Mon', 'Tue', 'Wed'), y= c(3,1,4), type= 'scatter', mode= 'lines+markers')%>% 
  layout(title = list(text='Plot Title', y = 0.95, x = 0.5, xanchor = 'center', yanchor =  'top'), plot_bgcolor = "#e5ecf6")
fig

Reference

See https://plotly.com/r/reference/layout/ for more information!

What About Dash?

Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash for R at https://dashr.plot.ly/installation.

Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument of the Graph component from the built-in dashCoreComponents package like this:

library(plotly)

fig <- plot_ly() 
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... ) 

library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)

app <- Dash$new()
app$layout(
    htmlDiv(
        list(
            dccGraph(figure=fig) 
        )
     )
)

app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)