Violin Plots in ggplot2
How to make Violin Plots in ggplot2 with Plotly.
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.
Default violin plot
library(plotly)
library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p <- p + geom_violin()
plotly::ggplotly(p)
Flip plot orientation
library(plotly)
library(ggplot2)
p <-
ggplot(mtcars, aes(mpg, factor(cyl))) +
geom_violin()
plotly::ggplotly(p)
With geom_violin()
, the y-axis must always be the continuous variable, and the x-axis the categorical variable. To create horizontal violin graphs, keep the x- and y-variables as is and add coord_flip()
.
library(plotly)
library(ggplot2)
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, fill=cluster)) +
geom_violin(colour=NA) +
geom_hline(yintercept=0, alpha=0.5) +
labs(title = "Democratic performance in the 2018 House elections, by region and density",
x = "Density Index\nfrom CityLab",
y = "Margin of Victory/Defeat") +
coord_flip()
ggplotly(p)
Add data points with jitter
library(plotly)
library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p <- p + geom_violin() + geom_jitter(height = 0, width = 0.1)
plotly::ggplotly(p)
Scaling maximum width
library(plotly)
library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p <- p + geom_violin(scale = "count")
plotly::ggplotly(p)
library(plotly)
library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p <- p + geom_violin(scale = "width")
plotly::ggplotly(p)
Disabling default trim
library(plotly)
library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p <- p + geom_violin(trim = FALSE)
plotly::ggplotly(p)
Closer density fit
library(plotly)
library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p <- p + geom_violin(adjust = .5)
plotly::ggplotly(p)
Adding fill
library(plotly)
library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p <- p + geom_violin(aes(fill = cyl))
plotly::ggplotly(p)
library(plotly)
library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p <- p + geom_violin(aes(fill = factor(cyl)))
plotly::ggplotly(p)
library(plotly)
library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p <- p + geom_violin(aes(fill = factor(vs)))
plotly::ggplotly(p)
library(plotly)
library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p <- p + geom_violin(aes(fill = factor(am)))
plotly::ggplotly(p)
Changing border colour
library(plotly)
library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p <- p + geom_violin(fill = "grey80", colour = "#3366FF")
plotly::ggplotly(p)
Enabling quartiles
library(plotly)
library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p <- p + geom_violin(draw_quantiles = c(0.25, 0.5, 0.75))
plotly::ggplotly(p)
Add facetting
Including facetting by region.
Add colour to the facet titles, centre-align the title, rotate the y-axis title, change the font, and get rid of the unnecessary legend. Note that coord_flip()
flips the axes for the variables and the titles, but does not flip theme()
elements.
Rotated the x-axis text 45 degrees, and used facet_grid
to create a 4x1 facet (compared to facet_wrap
, which defaults to 2x2).
library(plotly)
library(ggplot2)
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, fill=cluster)) +
geom_violin(colour=NA) +
geom_hline(yintercept=0, alpha=0.5) +
facet_grid(.~region) +
labs(title = "Democratic performance in the 2018 House elections, by region and density",
x = "Density Index\nfrom CityLab",
y = "Margin of Victory/Defeat") +
theme(axis.text.x = element_text(angle = -45),
plot.title = element_text(hjust = 0.5),
strip.background = element_rect(fill="lightblue"),
text = element_text(family = 'Fira Sans'),
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)