Show Sidebar Hide Sidebar

geom_jitter in ggplot2

How to make plots in ggplot2 using geom_jitter.

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.5.6.9000'

Scatterplot

library(plotly)

df <- data.frame(x = sample(LETTERS[1:10], size = 1000, replace = T),
                 y = sample(1:100, size = 1000, replace = T),
                 group = sample(letters[1:5], size = 1000, replace = T))

p <- ggplot(df, aes(x, y, color = group)) +
  geom_point() + 
  geom_jitter(width = 1, height = 1) + 
  ggtitle("geom_jitter: scatterplots")

p <- ggplotly(p)

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

Boxplot

library(plotly)

p <- ggplot(mtcars, aes(x = factor(gear), y = mpg, color = cyl)) +
  geom_boxplot() + 
  geom_jitter(size = 2) +
  ggtitle("geom_jitter: boxplot")

p <- ggplotly(p)

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

Add Text

library(plotly)

p <- ggplot(mtcars, aes(x = factor(gear), y = mpg, color = cyl)) +
  geom_text(aes(label = mpg)) + 
  geom_jitter(size = 2) +
  ggtitle("geom_jitter: text")

p <- ggplotly(p)

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

Add Colors

library(plotly)

p <- ggplot(mtcars, aes(x = factor(gear), y = mpg)) +
  geom_point(aes(color = cyl)) +
  geom_jitter(size = 2, color = "red") +
  ggtitle("geom_jitter: color")

p <- ggplotly(p)

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

Add Error Bars

library(plotly)
library(dplyr)

df <- mtcars %>% 
  group_by(cyl) %>% 
  summarise(min = min(mpg),
            max = max(mpg))

p <- ggplot(mtcars, aes(x = factor(cyl), y = mpg)) + 
  geom_jitter() + 
  geom_errorbar(data = df, aes(x = factor(cyl), y = 1, ymin = min, ymax = max))

p <- ggplotly(p)

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = plotly_POST(p, filename="geom_jitter/error-bars")
chart_link
Still need help?
Contact Us

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