facets in ggplot2

# Learn about API authentication here: https://plot.ly/ggplot2/getting-started
# Find your api_key here: https://plot.ly/settings/api

library(reshape2)
library(plotly)

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

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

ggplotly()
Write feedback@plot.ly with questions or submit an issue. See the ggplot2 → plotly test tables for ggplot2 conversion coverage. Docs inspired by Cookbook for R.
# Learn about API authentication here: https://plot.ly/ggplot2/getting-started
# Find your api_key here: https://plot.ly/settings/api

library(reshape2)
library(plotly)

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

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

ggplotly()
Inspired by Cookbook for R.
# Learn about API authentication here: https://plot.ly/ggplot2/getting-started
# Find your api_key here: https://plot.ly/settings/api

library(reshape2)
library(plotly)

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

# Divide by day, going horizontally and wrapping with 2 columns
sp + facet_wrap( ~ day, ncol=2)

ggplotly()
Inspired by Cookbook for R.
# Learn about API authentication here: https://plot.ly/ggplot2/getting-started
# Find your api_key here: https://plot.ly/settings/api

library(reshape2)
library(plotly)

# A histogram of bill sizes
hp <- ggplot(tips, aes(x=total_bill)) + geom_histogram(binwidth=2,colour="white")

ggplotly()
# Learn about API authentication here: https://plot.ly/ggplot2/getting-started
# Find your api_key here: https://plot.ly/settings/api

library(reshape2)
library(plotly)

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

# Histogram of total_bill, divided by sex and smoker
hp + facet_grid(sex ~ smoker)

ggplotly()
Inspired by Cookbook for R.
# Learn about API authentication here: https://plot.ly/ggplot2/getting-started
# Find your api_key here: https://plot.ly/settings/api

library(reshape2)
library(plotly)

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

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

ggplotly()
Inspired by Cookbook for R.
# Learn about API authentication here: https://plot.ly/ggplot2/getting-started
# Find your api_key here: https://plot.ly/settings/api

library(reshape2)
library(plotly)

hp <- 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)
hp + facet_grid(sex ~ smoker, scales="free", space="free")

ggplotly()
Inspired by Cookbook for R.
# Learn about API authentication here: https://plot.ly/ggplot2/getting-started
# Find your api_key here: https://plot.ly/settings/api

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

tolerance$id <- with(tolerance, reorder(id, tolerance[ifelse(time == 0, TRUE, NA)], FUN = mean, na.rm = TRUE))

ggplot(data = tolerance, aes(x = time, y = tolerance)) + geom_point() +
    stat_smooth(method = "lm", se = FALSE) + facet_wrap(~id)

ggplotly()
Inspired by the IDRE at UCLA.
# Learn about API authentication here: https://plot.ly/ggplot2/getting-started
# Find your api_key here: https://plot.ly/settings/api

library(plotly)

set.seed(42)
n <- 100

df <- data.frame(location = rep(LETTERS[1:4], n),
                 score    = sample(45:80, 4*n, replace = TRUE))

df$p    <- inv.logit(0.075 * df$score + rep(c(-4.5, -5, -6, -2.8), n))
df$pass <- sapply(df$p, function(x){rbinom(1, 1, x)})

preds <- predict(g, newdata = new.data, type = 'response',se = TRUE)
new.data$pred.full <- preds$fit

new.data$ymin <- new.data$pred.full - 2*preds$se.fit
new.data$ymax <- new.data$pred.full + 2*preds$se.fit

ggplot(df,aes(x = score, y = pass)) +
    facet_wrap(~location) +
    geom_point(size=3) +
    geom_ribbon(data = new.data,aes(y = pred.full, ymin = ymin, ymax = ymax),alpha = 0.25) +
    geom_line(data = new.data,aes(y = pred.full),colour = "blue")

ggplotly()
Inspired by Stack Overflow.
# Learn about API authentication here: https://plot.ly/ggplot2/getting-started
# Find your api_key here: https://plot.ly/settings/api

library(plotly)

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

ggplotly()
Inspired by R Study Group.
# Learn about API authentication here: https://plot.ly/ggplot2/getting-started
# Find your api_key here: https://plot.ly/settings/api

library(plotly)

ggplot(mpg, aes(displ, hwy))+
    geom_point()+
    facet_wrap(~manufacturer)

ggplotly()
Inspired by R Study Group.