Text and Annotations in R

How to add text labels and annotations to plots 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.

Text Mode

library(plotly)

Primates <- c('Potar monkey', 'Gorilla', 'Human', 'Rhesus monkey', 'Chimp')
Bodywt <- c(10.0, 207.0, 62.0, 6.8, 52.2)
Brainwt <- c(115, 406, 1320, 179, 440)
data <- data.frame(Primates, Bodywt, Brainwt)

fig <- plot_ly(data, x = ~Bodywt, y = ~Brainwt, type = 'scatter',
        mode = 'text', text = ~Primates, textposition = 'middle right',
        textfont = list(color = '#000000', size = 16))
fig <- fig %>% layout(title = 'Primates Brain and Body Weight',
         xaxis = list(title = 'Body Weight (kg)',
                      zeroline = TRUE,
                      range = c(0, 250)),
         yaxis = list(title = 'Brain Weight (g)',
                      range = c(0,1400)))

fig

See more options on the textposition argument here.

Styling Text

library(plotly)

data <- mtcars[which(mtcars$am == 1 & mtcars$gear == 4),]

t <- list(
  family = "sans serif",
  size = 14,
  color = toRGB("grey50"))

fig <- plot_ly(data, x = ~wt, y = ~mpg, text = rownames(data))
fig <- fig %>% add_markers()
fig <- fig %>% add_text(textfont = t, textposition = "top right")
fig <- fig %>% layout(xaxis = list(range = c(1.6, 3.2)),
         showlegend = FALSE)

fig

Controlling text fontsize with uniformtext

For the pie, bar, sunburst and treemap traces, it is possible to force all the text labels to have the same size thanks to the uniformtext layout parameter. The minsize attribute sets the font size, and the mode attribute sets what happens for labels which cannot fit with the desired fontsize: either hide them or show them with overflow.

library(plotly)

df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv", stringsAsFactors = FALSE)
df <- df[which(df$year==2007 & df$continent=='Europe' & df$pop > 2.e6),]

fig <- plot_ly(df, type='bar', x = ~country, y = ~pop, text = ~lifeExp, name="",
               hovertemplate = paste('%{x}', '<br>lifeExp: %{text:.2s}<br>'),
               texttemplate = '%{y:.2s}', textposition = 'outside')

fig <- fig %>% layout(uniformtext=list(minsize=8, mode='hide'))
fig
library(plotly)

df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv", stringsAsFactors = FALSE)
df <- df[which(df$year==2007 & df$continent=='Asia'),]

fig <- plot_ly(df, type='pie', labels = ~country, values = ~pop, textposition = 'inside')
fig <- fig %>% layout(uniformtext=list(minsize=12, mode='hide'))
fig

Adding Informations to Default Hover Text

library(plotly)

fig <- plot_ly(iris, x = ~Petal.Length, y = ~Petal.Width, type = 'scatter', mode = 'markers',
        text = ~paste('Species: ', Species))

fig

Custom Hover Text

library(plotly)

fig <- plot_ly(iris, x = ~Petal.Length, y = ~Petal.Width, type = 'scatter', mode = 'markers',
        hoverinfo = 'text',
        text = ~paste('</br> Species: ', Species,
                      '</br> Petal Length: ', Petal.Length,
                      '</br> Petal Width: ', Petal.Width))

fig

Single Annotation

library(plotly)

m <- mtcars[which.max(mtcars$mpg), ]

a <- list(
  x = m$wt,
  y = m$mpg,
  text = rownames(m),
  xref = "x",
  yref = "y",
  showarrow = TRUE,
  arrowhead = 7,
  ax = 20,
  ay = -40
)

fig <- plot_ly(mtcars, x = ~wt, y = ~mpg)
fig <- fig %>% add_markers()
fig <- fig %>% layout(annotations = a)

fig

Multiple Annotations

library(plotly)

data <- mtcars[which(mtcars$am == 1 & mtcars$gear == 4),]

fig <- plot_ly(data, x = ~wt, y = ~mpg, type = 'scatter', mode = 'markers',
        marker = list(size = 10))
fig <- fig %>% add_annotations(x = data$wt,
                  y = data$mpg,
                  text = rownames(data),
                  xref = "x",
                  yref = "y",
                  showarrow = TRUE,
                  arrowhead = 4,
                  arrowsize = .5,
                  ax = 20,
                  ay = -40)

fig

Subplot Annotations

library(plotly)

m <- economics[which.max(economics$unemploy), ]
n <- economics[which.max(economics$uempmed), ]

# annotations
a <- list(
  x = m$date,
  y = m$unemploy,
  text = "annotation a",
  xref = "x",
  yref = "y",
  showarrow = TRUE,
  arrowhead = 7,
  ax = 20,
  ay = -40
)

b <- list(
  x = n$date,
  y = n$uempmed,
  text = "annotation b",
  xref = "x2",
  yref = "y2",
  showarrow = TRUE,
  arrowhead = 7,
  ax = 20,
  ay = -40
)

# figure labels
f <- list(
  family = "Courier New, monospace",
  size = 18,
  color = "#7f7f7f ")
x <- list(
  title = "x Axis",
  titlefont = f)
y <- list(
  title = "y Axis",
  titlefont = f)

fig1 <- plot_ly(economics, x = ~date, y = ~unemploy)
fig1 <- fig1 %>% add_lines(name = ~"unemploy")
fig1 <- fig1 %>% layout(annotations = a, xaxis = x, yaxis = y)
fig2 <- plot_ly(economics, x = ~date, y = ~uempmed)
fig2 <- fig2 %>% add_lines(name = ~"uempmed")
fig2 <- fig2 %>% layout(annotations = b, xaxis = x, yaxis = y)
fig <- subplot(fig1, fig2, titleX = TRUE, titleY = TRUE)
fig2 <- fig2 %>% layout(showlegend = FALSE)

fig

3D Annotations

library(plotly)

fig <- plot_ly()
fig <- fig %>% add_trace(
    x = c("2017-01-01", "2017-02-10", "2017-03-20"), 
    y = c("A", "B", "C"), 
    z = c(1, 1000, 100000), 
    name = "z", 
    type = "scatter3d"
  )
fig <- fig %>% layout(
    scene = list(
      aspectratio = list(
        x = 1,
        y = 1,
        z = 1
      ),
      camera = list(
        center = list(
          x = 0,
          y = 0,
          z = 0
        ),
        eye = list(
          x = 1.96903462608,
          y = -1.09022831971,
          z = 0.405345349304
        ),
        up = list(
          x = 0,
          y = 0,
          z = 1
        )
      ),
      dragmode = "turntable",
      xaxis = list(
        title = "",
        type = "date"
      ),
      yaxis = list(
        title = "",
        type = "category"
      ),
      zaxis = list(
        title = "",
        type = "log"
      ),
      annotations = list(list(
        showarrow = F,
        x = "2017-01-01",
        y = "A",
        z = 0,
        text = "Point 1",
        xanchor = "left",
        xshift = 10,
        opacity = 0.7
      ), list(
        x = "2017-02-10",
        y = "B",
        z = 4,
        text = "Point 2",
        textangle = 0,
        ax = 0,
        ay = -75,
        font = list(
          color = "black",
          size = 12
        ),
        arrowcolor = "black",
        arrowsize = 3,
        arrowwidth = 1,
        arrowhead = 1
      ), list(
        x = "2017-03-20",
        y = "C",
        z = 5,
        ax = 50,
        ay = 0,
        text = "Point 3",
        arrowhead = 1,
        xanchor = "left",
        yanchor = "bottom"
      )
    )),
    xaxis = list(title = "x"),
    yaxis = list(title = "y")
  )

fig

Styling Annotations

library(plotly)

data <- mtcars[which(mtcars$am == 1 & mtcars$gear == 4),]

fig <- plot_ly(data, x = ~wt, y = ~mpg, type = 'scatter', mode = 'markers',
        marker = list(size = 10))
fig <- fig %>% add_annotations(x = data$wt,
                  y = data$mpg,
                  text = rownames(data),
                  xref = "x",
                  yref = "y",
                  showarrow = TRUE,
                  arrowhead = 4,
                  arrowsize = .5,
                  ax = 20,
                  ay = -40,
                  # Styling annotations' text:
                  font = list(color = '#264E86',
                              family = 'sans serif',
                              size = 14))

fig

Set Annotation Text Anchors

library(plotly)

fig <- plot_ly()
fig <- fig %>% add_markers(
    x = 1,
    y = 1,
    showlegend = F
  )
fig <- fig %>% add_markers(
    x = 1,
    y = 2,
    showlegend = F
  )
fig <- fig %>% add_markers(
    x = 1,
    y = 3,
    showlegend = F
  )
fig <- fig %>% add_annotations(
    x=1,
    y=1,
    xref = "x",
    yref = "y",
    text = "Right Anchor",
    xanchor = 'right',
    showarrow = F
  )
fig <- fig %>% add_annotations(
    x=1,
    y=2,
    xref = "x",
    yref = "y",
    text = "Center Anchor",
    xanchor = 'center',
    showarrow = F
  )
fig <- fig %>% add_annotations(
    x=1,
    y=3,
    xref = "x",
    yref = "y",
    text = "Left Anchor",
    xanchor = 'left',
    showarrow = F
  )

fig

Customize Displayed Text with a Text Template

To show an arbitrary text in your chart you can use texttemplate, which is a template string used for rendering the information, and will override textinfo. This template string can include variables in %{variable} format, numbers in d3-format's syntax, and date in d3-time-fomrat's syntax. texttemplate customizes the text that appears on your plot vs. hovertemplate that customizes the tooltip text.

library(plotly)

fig <- plot_ly(
  type='pie',
  values=c(40000000, 20000000, 30000000, 10000000),
  labels=c("Wages", "Operating expenses", "Cost of sales", "Insurance"),
  texttemplate="%{label}: %{value:$,s} <br>(%{percent})",
  textposition="inside")
fig

Customize Text Template

The following example uses textfont to customize the added text.

library(plotly)

fig <- plot_ly(
  type='scatterternary',
  a = c(3, 2, 5),
  b = c(2, 5, 2),
  c = c(5, 2, 2),
  mode = "markers+text",
  text = c("A", "B", "C"),
  texttemplate = "%{text}<br>(%{a:.2f}, %{b:.2f}, %{c:.2f})",
  textposition = "bottom center",
  textfont = list(family= "Times", size= c(18, 21, 20), color= c("IndianRed", "MediumPurple", "DarkOrange"))
)
fig

Set Date in Text Template

The following example shows how to show date by setting axis.type in funnel charts. As you can see textinfo and texttemplate have the same functionality when you want to determine 'just' the trace information on the graph.

fig <- plot_ly()

fig <- fig %>% add_trace(
  type='funnel',
  name = 'Montreal',
  orientation = "h",
  y = c("2018-01-01", "2018-07-01", "2019-01-01", "2020-01-01"),
  x = c(100, 60, 40, 20),
  textposition = "inside",
  texttemplate = "%{y| %a. %_d %b %Y}")

fig <- fig %>% add_trace(
  type='funnel',
  name = 'Vancouver',
  orientation = "h",
  y = c("2018-01-01", "2018-07-01", "2019-01-01", "2020-01-01"),
  x = c(90, 70, 50, 10),
  textposition = "inside",
  textinfo = "label")

fig <- fig %>% layout(yaxis = list(type= 'date'))
fig

Set Annotation Coordinate References

library(plotly)

fig <- plot_ly()
fig <- fig %>% add_markers(
    x = 0.5,
    y = 1,
    showlegend = F
  )
fig <- fig %>% add_annotations(
    x= 0.5,
    y= 1,
    xref = "paper",
    yref = "paper",
    text = "<b>paper reference = [0.5, 1]</b>",
    showarrow = F
  )
fig <- fig %>% add_annotations(
    x= 0.5,
    y= 1,
    xref = "x",
    yref = "y",
    text = "x + y reference = [0.5, 1]",
    showarrow = T,
    ax = 20,
    ay = -40
  )
fig <- fig %>% layout(
    xaxis = list(zeroline = F),
    yaxis = list(zeroline = F)
  )

fig

Reference

See https://plotly.com/r/reference/#layout-annotations for more information and chart attribute options!

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)