Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you facet on a indicator variable in ggplot2?

Tags:

r

ggplot2

types = c("A", "B", "C")
df = data.frame(n = rnorm(100), type=sample(types, 100, replace = TRUE))
ggplot(data=df, aes(n)) + geom_histogram() + facet_grid(~type)

Above is how I normally used facetting. But can I use it when instead of a categorical variable I have a set of columns that are indicator variables such as:

df = data.frame(n = rnorm(100), A=rbinom(100, 1, .5), B=rbinom(100, 1, .5), C=rbinom(100, 1, .5))

Now the "Type" variable from my previous example isn't mutually exclusive. An observation can be "A and B" or "A and B and C" for example. However, I'd still like an individual histogram for any observation that has the presence of A, B, or C?

like image 604
Andrew Cassidy Avatar asked Mar 23 '26 20:03

Andrew Cassidy


2 Answers

I would reshape the data with tidyr so that data in more that one category are duplicated. filter to remove unwanted cases.

df <- data.frame(
  n = rnorm(100),
  A = rbinom(100, 1, .5),
  B = rbinom(100, 1, .5),
  C = rbinom(100, 1, .5)
)

library("tidyr")
library("dplyr")
library("ggplot2")

df %>% gather(key = "type", value = "value", -n) %>%
  filter(value == 1) %>%
  ggplot(aes(x = n)) +
    geom_histogram() +
    facet_wrap(~type)
like image 137
Richard Telford Avatar answered Mar 26 '26 09:03

Richard Telford


I've always despised gather, so I'll add another method and one for the data.table fans.

library(data.table)
DT <- melt(setDT(df), id= "n", variable = "type")[value > 0]
ggplot(DT,aes(n)) + geom_histogram() + facet_grid(~type)

#tidyland
library(reshape2)
library(dplyr)
library(ggplot2)
df %>% 
  melt(id = "n", variable = "type") %>% 
  filter(value > 0) %>% 
  ggplot(aes(n)) + geom_histogram() + facet_grid(~type)
like image 35
Pierre L Avatar answered Mar 26 '26 09:03

Pierre L



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!