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

How to Set the Font, Title, Legend Entries, and Axis Titles in ggplot2 with Plotly.


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.

Automatic Labelling with ggplot2

When using ggplot2, your axes and legend are 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)
library(ggplot2)

p <- ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width)) +
      geom_point(aes(color=Species, shape=Species)) +
      labs(title = "Iris sepal width vs length")

ggplotly(p)

Manually specified labels

library(plotly)
library(ggplot2)

p <- ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width)) +
      geom_point(aes(color=Species, shape=Species)) +
      labs(title = "Iris sepal width vs length", 
            x = "Sepal Length", 
            y = "Sepal Width")

ggplotly(p)

Change color, size, style of text

To change these, use theme() and appropriate arugment, in this case plot.title, legend.title, legend.text, axis.title, and the desired changes you wish to make to the specified element.

library(plotly)
library(ggplot2)

p <- ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width)) +
      geom_point(aes(color=Species, shape=Species)) +
      labs(title = "Iris sepal width vs length", 
            x = "Sepal Length", 
            y = "Sepal Width") +
      theme(plot.title = element_text(size = 18, face = "bold", color = "red"),
        legend.title=element_text(size=10, face = "italic", color = "blue"), 
        legend.text=element_text(size=6, color="darkgreen"),
        axis.title = element_text(face = "bold", color="orange"))

ggplotly(p)

Align title

library(plotly)
library(ggplot2)

p <- ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width)) +
      geom_point(aes(color=Species, shape=Species)) +
      labs(title = "Iris sepal width vs length", 
            x = "Sepal Length", 
            y = "Sepal Width") +
      theme(plot.title = element_text(hjust = 0.5))

ggplotly(p)

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)