Legends in ggplot2

How to work with Legends in ggplot2 with Plotly.


Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Sign up for early access now.

Default box plot

library(plotly)
library(ggplot2)

ToothGrowth$dose <- as.factor(ToothGrowth$dose)

p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
  geom_boxplot()

ggplotly(p)

Change the legend position

The position of the legend can be changed using the function theme() as follow:

library(plotly)
library(ggplot2)

ToothGrowth$dose <- as.factor(ToothGrowth$dose)

p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
  geom_boxplot()

p <- p + theme(legend.position="bottom")

ggplotly(p)

Note that, the argument legend.position can be also a numeric vector c(x,y). In this case it is possible to position the legend inside the plotting area. x and y are the coordinates of the legend box. Their values should be between 0 and 1. c(0,0) corresponds to the bottom left and c(1,1) corresponds to the top right position.

library(plotly)
library(ggplot2)

ToothGrowth$dose <- as.factor(ToothGrowth$dose)

p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
  geom_boxplot()

p <- p + theme(legend.position = c(0.8, 0.2))

ggplotly(p)

Change the legend title and text font styles

library(plotly)
library(ggplot2)

ToothGrowth$dose <- as.factor(ToothGrowth$dose)

p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
  geom_boxplot()

p <- p + theme(legend.title = element_text(colour="red", size=10,
                                      face="italic"))
p <- p + theme(legend.text = element_text(colour="blue", size=10,
                                     face="bold"))

ggplotly(p)

Change the background color of the legend box

library(plotly)
library(ggplot2)

ToothGrowth$dose <- as.factor(ToothGrowth$dose)

p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
  geom_boxplot()

p <- p + theme(legend.background = element_rect(fill="lightblue",
                                  size=0.5, linetype="solid",
                                  colour ="red"))

ggplotly(p)

Change the order of legend items

library(plotly)
library(ggplot2)

ToothGrowth$dose <- as.factor(ToothGrowth$dose)

p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
  geom_boxplot()

p <- p + scale_x_discrete(limits=c("2", "0.5", "1"))

ggplotly(p)

Remove the plot legend

library(plotly)
library(ggplot2)

ToothGrowth$dose <- as.factor(ToothGrowth$dose)

p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
  geom_boxplot()

p <- p + theme(legend.title = element_blank())
p <- p + theme(legend.position='none')

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)