Black Lives Matter. Please consider donating to Black Girls Code today.

facet_wrap in ggplot2

How to make subplots with facet_wrap in ggplot2 and R.


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 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)

fig <- ggplotly(p)

fig

Inspired by Cookbook for R

Add Unique Curves

library(plotly)

## read in data set (tolerance data from the ALDA book)
tolerance <- read.table("https://stats.idre.ucla.edu/wp-content/uploads/2016/02/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)

fig <- ggplotly(p)

fig

Inspired by The IDRE at UCLA

Add Stat_Smooth

library(plotly)

p <- ggplot(mpg, aes(displ, hwy))+
  geom_point()+
  stat_smooth()+
  facet_wrap(~year)

fig <- ggplotly(p)

fig

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)

fig <- ggplotly(p)

fig

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")

fig <- ggplotly(p)

fig

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")

fig <- ggplotly(p)

fig

Inspired by Stack Overflow

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)