Facet Plots in ggplot2

How to make Facet Plots in ggplot2 with Plotly.


Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Sign up for early access now.

Basic

library(reshape2)
library(plotly)

p <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + geom_point(shape=1)

# Divide by levels of "sex", in the vertical direction
p <- p + facet_grid(sex ~ .)

ggplotly(p)

Horizontal Grid

library(reshape2)
library(plotly)

p <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + geom_point(shape=1)

# Divide by levels of "sex", in the horizontal direction
p <- p + facet_grid(. ~ sex)

ggplotly(p)

Free Scale

library(reshape2)
library(plotly)

p <- ggplot(tips, aes(x=total_bill)) + geom_histogram(binwidth=2,colour="white")

# Histogram of total_bill, divided by sex and smoker
p <- p + facet_grid(sex ~ smoker)

ggplotly(p)

Free Y Axis

library(reshape2)
library(plotly)

p <- ggplot(tips, aes(x=total_bill)) + geom_histogram(binwidth=2,colour="white")

# Same as above, with scales="free_y"
p <- p + facet_grid(sex ~ smoker, scales="free_y")

ggplotly(p)

Varied Range

library(reshape2)
library(plotly)

p <- ggplot(tips, aes(x=total_bill)) + geom_histogram(binwidth=2,colour="white")

# With panels that have the same scaling, but different range (and therefore different physical sizes)
p <- p + facet_grid(sex ~ smoker, scales="free", space="free")

ggplotly(p)

Time Series Data

library(plotly)
require(scales)
require(gridExtra)

mymelt <- structure(list(mydate = structure(c(15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15371, 15371, 15371, 15371, 15371, 15371, 15371, 15371, 15371, 15371, 15371, 15371, 15400, 15400, 15400, 15400, 15400, 15400, 15400, 15400, 15400, 15400, 15400, 15400, 15431, 15431, 15431, 15431, 15431, 15431, 15431, 15431, 15431, 15431, 15431, 15431, 15461, 15461, 15461, 15461, 15461, 15461, 15461, 15461, 15461, 15461, 15461, 15461, 15492, 15492, 15492, 15492, 15492, 15492, 15492, 15492, 15492, 15492, 15492, 15492, 15522, 15522, 15522, 15522, 15522, 15522, 15522, 15522, 15522, 15522, 15522, 15522, 15553, 15553, 15553, 15553, 15553, 15553, 15553, 15553, 15553, 15553, 15553, 15553), class = "Date"), variable = c("b", "bc", "f", "in", "it", "l", "of", "o", "pr", "s", "total", "tr", "b", "bc", "f", "in", "it", "l", "of", "o", "pr", "s", "total", "tr", "b", "bc", "f", "in", "it", "l", "of", "o", "pr", "s", "total", "tr", "b", "bc", "f", "in", "it", "l", "of", "o", "pr", "s", "total", "tr", "b", "bc", "f", "in", "it", "l", "of", "o", "pr", "s", "total", "tr", "b", "bc", "f", "in", "it", "l", "of", "o", "pr", "s", "total", "tr", "b", "bc", "f", "in", "it", "l", "of", "o", "pr", "s", "total", "tr", "b", "bc", "f", "in", "it", "l", "of", "o", "pr", "s", "total", "tr"), value = c(-23, 6.90000000000001, 459.799999999999, -403.6, -56.1, -95, -13.8, 32.6, 121.5, -15.7, 26.2000000000007, 12.5, -25.1, 238.3, 1047.2, -803.2, -151.5, -260.5, -59.6, -93.8, 461.5, -37.7, 26.7999999999993, -288.8, -46.4, 249, 1289.8, -783.2, -188.1, -414.9, -77.7, -61, 928.4, -36.8, 17.4000000000015, -841.7, -46.5, 276.2, 1384.8, -541.1, -71.8999999999999, -433.3, -61.3, -28.3, 494.699999999999, -23.4, -14.5999999999985, -964.5, -46.1, 376.2, 1020.1, -119.4, 56.8000000000001, -447.7, -9.50000000000001, 14.2, -9.20000000000164, 2.5, -42.7999999999993, -880.6, -52.9, 345.5, 892.599999999999, -241.8, 144.3, -428.2, -3.30000000000001, 91.9, -294.800000000002, -5.19999999999999, -42.1999999999971, -490.1, -64.5, 379.7, 679.299999999999, -143.1, 185.9, -419.8, -4.30000000000001, 182.4, -421.900000000002, 1.80000000000001, -59.8999999999978, -435.2, -80.2, 422.2, 645.499999999998, -391.4, 76.6000000000001, -387.4, -1.70000000000001, 211.2, -131.500000000002, -10.6, -40.8999999999978, -393.6), fill = c("#A4D3EE80", "#A478AB80", "#01AEF080", "#8DC73F80", "#F8931D80", "#FFAAAA80", "#8C8C8C", "#D38D5F80", "#23238E80", "#77B9B780", "#C8373780", "#EEDD8280", "#A4D3EE80", "#A478AB80", "#01AEF080", "#8DC73F80", "#F8931D80", "#FFAAAA80", "#8C8C8C", "#D38D5F80", "#23238E80", "#77B9B780", "#C8373780", "#EEDD8280", "#A4D3EE80", "#A478AB80", "#01AEF080", "#8DC73F80", "#F8931D80", "#FFAAAA80", "#8C8C8C", "#D38D5F80", "#23238E80", "#77B9B780", "#C8373780", "#EEDD8280", "#A4D3EE80", "#A478AB80", "#01AEF080", "#8DC73F80", "#F8931D80", "#FFAAAA80", "#8C8C8C", "#D38D5F80", "#23238E80", "#77B9B780", "#C8373780", "#EEDD8280", "#A4D3EE80", "#A478AB80", "#01AEF080", "#8DC73F80", "#F8931D80", "#FFAAAA80", "#8C8C8C", "#D38D5F80", "#23238E80", "#77B9B780", "#C8373780", "#EEDD8280", "#A4D3EE80", "#A478AB80", "#01AEF080", "#8DC73F80", "#F8931D80", "#FFAAAA80", "#8C8C8C", "#D38D5F80", "#23238E80", "#77B9B780", "#C8373780", "#EEDD8280", "#A4D3EE80", "#A478AB80", "#01AEF080", "#8DC73F80", "#F8931D80", "#FFAAAA80", "#8C8C8C", "#D38D5F80", "#23238E80", "#77B9B780", "#C8373780", "#EEDD8280", "#A4D3EE80", "#A478AB80", "#01AEF080", "#8DC73F80", "#F8931D80", "#FFAAAA80", "#8C8C8C", "#D38D5F80", "#23238E80", "#77B9B780", "#C8373780", "#EEDD8280")), .Names = c("mydate", "variable", "value", "fill"), row.names = c(NA, 96L), class = "data.frame")

myvals <- mymelt[mymelt$mydate == mymelt$mydate[nrow(mymelt)],] ## last date in mymelt should always be same as plotenddate as we subset earlier
mymelt <- within(mymelt, variable <- factor(variable, as.character(myvals[order(myvals$value, decreasing = T),]$variable), ordered = TRUE))

p <- ggplot(mymelt, aes(x = mydate, y = value)) +
    geom_line(lwd=0.3) +
    facet_grid(. ~ variable) +
    theme(axis.text.x = element_text(size = 5, angle = 90),
          axis.text.y = element_text(size = 8),
          axis.title.x = element_text(vjust = 0),
          axis.ticks = element_blank(),
          panel.grid.minor = element_blank())

ggplotly(p)

Geom Line

library(plotly)
library(plyr)

date <- rep(as.Date(1:365,origin='2011-1-1'),7)
location <- factor(rep(1:7,365))
product <- rep(letters[1:7], each=365)
value <- c(sample(1:10, size=365, replace=T),sample(1:3, size=365, replace=T),
           sample(10:100, size=365, replace=T), sample(1:50, size=365, replace=T),
           sample(1:20, size=365, replace=T),sample(50:100, size=365, replace=T),
           sample(1:100, size=365, replace=T))
dat<-data.frame(date,location,product,value)

p <- ggplot(dat, aes(x=date, y=value, color=location, group=location)) +
    geom_line()+
    facet_grid(product ~ ., scale = "free_y")

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)