
facet_wrap in ggplot2
How to make subplots with facet_wrap in ggplot2 and 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.5.6.9000'
Basic Columns
library(reshape2)
library(plotly)
p <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + geom_point(shape=1)
# Divide by day, going horizontally and wrapping with 2 columns
p <- p + facet_wrap( ~ day, ncol=2)
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="facetwrap/basic")
chart_link
Inspired by Cookbook for R
Add Unique Curves
library(plotly)
## read in data set (tolerance data from the ALDA book)
tolerance <- read.table("http://www.ats.ucla.edu/stat/r/examples/alda/data/tolerance1_pp.txt",
sep = ",", header = TRUE)
## change id and male to factor variables
tolerance <- within(tolerance, {
id <- factor(id)
male <- factor(male, levels = 0:1, labels = c("female", "male"))
})
p <- ggplot(data = tolerance, aes(x = time, y = tolerance)) + geom_point() +
stat_smooth(method = "lm", se = FALSE) + facet_wrap(~id)
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="facetwrap/curves")
chart_link
Inspired by The IDRE at UCLA
Add Stat_Smooth
library(plotly)
p <- ggplot(mpg, aes(displ, hwy))+
geom_point()+
stat_smooth()+
facet_wrap(~year)
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="facetwrap/stat_smooth")
chart_link
Inspired by R Study Group
Labels
library(plotly)
set.seed(123)
df <- diamonds[sample(1:nrow(diamonds), size = 1000), ]
# Create labels
labs <- c("Best","Second best","Third best","Average", "Average","Third Worst","Second Worst","Worst")
levels(df$clarity) <- rev(labs)
p <- ggplot(df, aes(carat, price)) +
geom_point() +
facet_wrap(~ clarity)
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="facetwrap/labels")
chart_link
Inspired by Stack Overflow
Titles
library(plotly)
set.seed(123)
df <- diamonds[sample(1:nrow(diamonds), size = 1000), ]
# Create labels
labs <- c("Best","Second best","Third best","Average", "Average","Third Worst","Second Worst","Worst")
levels(df$clarity) <- rev(labs)
p <- ggplot(df, aes(carat, price)) +
geom_point() +
facet_wrap(~ clarity) +
ggtitle("Diamonds dataset facetted by clarity")
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="facetwrap/titles")
chart_link
Inspired by ggplot2 Documentation
Ordered Facets
library(plotly)
set.seed(123)
df <- diamonds[sample(1:nrow(diamonds), size = 1000), ]
# Reorer levels
levels(df$clarity) <- c("VS2", "VS1", "VVS2", "I1", "SI2", "IF", "VVS1", "SI1")
p <- ggplot(df, aes(carat, price)) +
geom_point() +
facet_wrap(~ clarity) +
ggtitle("Diamonds dataset facetted by clarity")
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="facetwrap/ordered")
chart_link
Inspired by Stack Overflow