geom_jitter in ggplot2
How to make a graph using geom_jitter.
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.
Basic Jitter Plot
You can use the "text=" option to control what pops when you hover over each point. (Note: you might get an error message when running this function; ggplot does not recognize it but the plotly function does.)
library(plotly)
district_density <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/district_density.csv",
stringsAsFactors = FALSE)
district_density$cluster <- factor(district_density$cluster, levels=c("Pure urban", "Urban-suburban mix", "Dense suburban", "Sparse suburban", "Rural-suburban mix", "Pure rural"))
district_density$region <- factor(district_density$region, levels=c("West", "South", "Midwest", "Northeast"))
p <- ggplot(district_density,aes(x=cluster, y=dem_margin, colour=region)) +
geom_jitter(aes(text=paste("district: ", cd_code)), width=0.25, alpha=0.5, ) +
geom_hline(yintercept=0) +
theme(axis.text.x = element_text(angle = -30, hjust = 0.1)) +
labs(title = "Democratic performance in the 2018 House elections, by region and density",
x = "Density Index from CityLab",
y = "Democratic Margin of Victory/Defeat")
fig <- ggplotly(p)
fig
Add Boxplot
library(plotly)
p <- ggplot(district_density,aes(x=cluster, y=dem_margin)) +
geom_boxplot(fill=NA, alpha=0.5) +
geom_jitter(aes(colour=region, text=paste("district: ", cd_code)), width=0.25, alpha=0.5) +
geom_hline(yintercept=0) +
theme(axis.text.x = element_text(angle = -30, hjust = 0.1)) +
labs(title = "Democratic performance in the 2018 House elections, by region and density",
x = "Density Index from CityLab",
y = "Democratic Margin of Victory/Defeat")
fig <- ggplotly(p)
fig
Facetting
library(plotly)
p <- ggplot(district_density,aes(x=region, y=dem_margin, colour=region)) +
geom_jitter(aes(text=paste("district: ", cd_code)), width=0.25, alpha=0.5, ) +
geom_hline(yintercept=0) +
facet_grid(.~cluster) +
theme(axis.text.x = element_text(angle = -30, hjust = 0.1)) +
labs(title = "Democratic performance in the 2018 House elections, by region and density",
x = "Density Index from CityLab",
y = "Democratic Margin of Victory/Defeat")
fig <- ggplotly(p)
fig
Adding Text
Tabulating the number of observations in each category, and adding these numbers to the graph.
library(plotly)
library(dplyr)
density_sum <- district_density %>%
group_by(cluster, region) %>%
dplyr::summarise(count = n())
p <- ggplot(district_density,aes(x=region, y=dem_margin, colour=region)) +
geom_jitter(aes(text=paste("district: ", cd_code)), width=0.25, alpha=0.5, ) +
geom_hline(yintercept=0) +
facet_grid(.~cluster) +
theme(axis.text.x = element_text(angle = -30, hjust = 0.1)) +
geom_text(data = density_sum, aes(label = count,
x = region, y = -90)) +
labs(title = "Democratic performance in the 2018 House elections, by region and density",
x = "Density Index from CityLab",
y = "Democratic Margin of Victory/Defeat")
fig <- ggplotly(p)
fig
Customized Appearance
Centre the title, add colours to the facet label, rotate the labels, change the font, and remove the unnecessary legend.
library(plotly)
p <- ggplot(district_density,aes(x=region, y=dem_margin, colour=region)) +
geom_jitter(aes(text=paste("district: ", cd_code)), width=0.25, alpha=0.5, ) +
geom_hline(yintercept=0) +
facet_grid(.~cluster) +
geom_text(data = density_sum, aes(label = count,
x = region, y = -90)) +
theme(axis.text.x = element_text(angle = -30, hjust = 0.1),
plot.title = element_text(hjust = 0.5),
strip.background = element_rect(fill="lightblue")) +
labs(title = "Democratic performance in the 2018 House elections, by region and density",
x = "Density Index from CityLab",
y = "Democratic Margin of Victory/Defeat") +
theme(text = element_text(family = 'Fira Sans'),
legend.position = "none")
fig <- ggplotly(p)
fig
Position Jitterdodge
Up to this point, we've subdivided points by making one category the x-axis, and facetting by the other. Another way is to make one category the x-axis, then use "position = dodge" so that the points are distinct rather than overlapping. Since we want points to be jittered and dodged, we can use geom_point with position_jitterdodge().
Make sure to specify the "group" variable: this graph specifies three potential grouping variables (cluster, region, cd_code), and position_jitterdodge can't tell which two to use unless specified. Further, you can use the ggplotly() function to specify what shows up on the tooltip.
library(plotly)
p <- ggplot(district_density,aes(x=cluster, y=dem_margin, colour=region,
district=cd_code, group=paste(cluster, region))) +
geom_point(position=position_jitterdodge(), alpha=0.5) +
geom_hline(yintercept=0) +
theme(axis.text.x = element_text(angle = -30, hjust = 0.1),
plot.title = element_text(hjust = 0.5)) +
labs(title = "Democratic performance in the 2018 House elections, by region and density",
x = "Density Index from CityLab",
y = "Democratic Margin of Victory/Defeat") +
theme(text = element_text(family = 'Fira Sans'))
fig <- ggplotly(fig, tooltip=c("district","y"))
fig
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)