Show Sidebar Hide Sidebar

Marker Styling in R

How to style markers in R.

New to Plotly?

Plotly's R library is free and open source!
Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Version 4 of Plotly's R package is now available!
Check out this post for more information on breaking changes and new features available in this version.

library(plotly)
packageVersion('plotly')
## [1] '4.7.1.9000'

Add Marker Border

In order to make markers distinct, you can add a border to the markers. This can be achieved by adding the line dict to the marker dict. For example, marker:{..., line: {...}}.

library(plotly)

x <- runif(500, min=3, max=6)
y <- runif(500, min=3, max=6)

p <- plot_ly(type = 'scatter', mode = 'markers') %>%
  add_trace(
    x = x,
    y = y,
    marker = list(
      color = 'rgb(17, 157, 255)',
      size = 20,
      line = list(
        color = 'rgb(231, 99, 250)',
        width = 2
      )
    ),
    showlegend = F
  ) %>%
  add_trace(
    x = c(2),
    y = c(4.5),
    marker = list(
      color = 'rgb(17, 157, 255)',
      size = 120,
      line = list(
        color = 'rgb(231, 99, 250)',
        width = 12
      )
    ),
    showlegend = F
  )

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="style-add-border")
chart_link

Fully Opaque

Fully opaque, the default setting, is useful for non-overlapping markers. When many points overlap it can be hard to observe density.

library(plotly)

x <- runif(500, min=3, max=6)
y <- runif(500, min=3, max=6)

p <- plot_ly(type = 'scatter', mode = 'markers') %>%
  add_trace(
    x = x,
    y = y,
    marker = list(
      color = 'rgb(17, 157, 255)',
      size = 20,
      line = list(
        color = 'rgb(231, 99, 250)',
        width = 2
      )
    ),
    showlegend = F
  ) %>%
  add_trace(
    x = c(2,2),
    y = c(4.25,4.75),
    marker = list(
      color = 'rgb(17, 157, 255)',
      size = 120,
      line = list(
        color = 'rgb(231, 99, 250)',
        width = 12
      )
    ),
    showlegend = F
  )

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="style-full-opacity")
chart_link

Opacity

Setting opacity outside the marker will set the opacity of the trace. Thus, it will allow greater visbility of additional traces but like fully opaque it is hard to distinguish density.

library(plotly)

x <- runif(500, min=3, max=6)
y <- runif(500, min=3, max=4.5)
x2 <- runif(500, min=3, max=6)
y2 <- runif(500, min=4.5, max=6)

p <- plot_ly(type = 'scatter', mode = 'markers') %>%
  add_trace(
    x = x,
    y = y,
    opacity = 0.5,
    marker = list(
      color = 'rgb(17, 157, 255)',
      size = 20,
      line = list(
        color = 'rgb(231, 99, 250)',
        width = 2
      )
    ),
    name = 'Opacity 0.5'
  ) %>%
  add_trace(
    x = x2,
    y = y2,
    marker = list(
      color = 'rgb(17, 157, 255)',
      size = 20,
      line = list(
        color = 'rgb(231, 99, 250)',
        width = 2
      )
    ),
    name = 'Opacity 1.0'
  ) %>%
  add_trace(
    x = c(2,2),
    y = c(4.25,4.75),
    opacity = 0.5,
    marker = list(
      color = 'rgb(17, 157, 255)',
      size = 120,
      line = list(
        color = 'rgb(231, 99, 250)',
        width = 12
      )
    ),
    showlegend = F
  )

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="style-opacity")
chart_link

Marker Opacity

To maximise visibility of density, it is recommended to set the opacity inside the marker marker:{opacity:0.5}. If mulitple traces exist with high density, consider using marker opacity in conjunction with trace opacity.

library(plotly)

x <- runif(500, min=3, max=6)
y <- runif(500, min=3, max=6)

p <- plot_ly(type = 'scatter', mode = 'markers') %>%
  add_trace(
    x = x,
    y = y,
    marker = list(
      color = 'rgb(17, 157, 255)',
      size = 20,
      opacity = 0.5,
      line = list(
        color = 'rgb(231, 99, 250)',
        width = 2
      )
    ),
    showlegend = F
  ) %>%
  add_trace(
    x = c(2,2),
    y = c(4.25,4.75),
    marker = list(
      color = 'rgb(17, 157, 255)',
      size = 120,
      opacity = 0.5,
      line = list(
        color = 'rgb(231, 99, 250)',
        width = 12
      )
    ),
    showlegend = F
  )

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="style-marker-opacity")
chart_link

Color Opacity

To maximise visibility of each point, set the color opacity by using alpha: marker:{color: 'rgba(0,0,0,0.5)'}. Here, the marker line will remain opaque.

library(plotly)

x <- runif(500, min=3, max=6)
y <- runif(500, min=3, max=6)

p <- plot_ly(type = 'scatter', mode = 'markers') %>%
  add_trace(
    x = x,
    y = y,
    marker = list(
      color = 'rgba(17, 157, 255,0.5)',
      size = 20,
      line = list(
        color = 'rgb(231, 99, 250)',
        width = 2
      )
    ),
    showlegend = F
  ) %>%
  add_trace(
    x = c(2,2),
    y = c(4.25,4.75),
    marker = list(
      color = 'rgba(17, 157, 255,0.5)',
      size = 120,
      line = list(
        color = 'rgb(231, 99, 250)',
        width = 12
      )
    ),
    showlegend = F
  )

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="style-color-opacity")
chart_link

Reference

See https://plot.ly/r/reference/ for more information and chart attribute options!

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.