Adrian
Adrian

Reputation: 9803

R: stratify by 2 factor variables in ggplot

library(ggplot2)
iris$Sepal.Length2 <- ifelse(iris$Sepal.Length < 5, 1, 0)
iris$Sepal.Width2 <- ifelse(iris$Sepal.Width < 3, 1, 0)

SmallLength <- data.frame(Petal.Length = iris$Petal.Length[iris$Sepal.Length2 == 1],
                         status = "Small Length")
LargeLength <- data.frame(Petal.Length = iris$Petal.Length[iris$Sepal.Length2 == 0],
                         status = "Large Length")
SmallWidth <- data.frame(Petal.Length = iris$Petal.Length[iris$Sepal.Width2 == 1],
                         status = "Small Width")
LargeWidth <- data.frame(Petal.Length = iris$Petal.Length[iris$Sepal.Width2 == 0],
                         status = "Large Width")

Length <- rbind(SmallLength, LargeLength)
Width <- rbind(SmallWidth, LargeWidth)
ggplot(Length, aes(Petal.Length, fill = status)) + geom_density(alpha = 0.2) + labs(x = "Petal Length")

enter image description here

I have a continuous variable, Petal.Length, and I would like to stratify it by Sepal.Length, and Sepal.Width, both of which I have coded as binary variables. In the above plot, I stratified Petal.Length by Sepal.Length only. How can I further stratify it by Sepal.Width? The resulting plot should perhaps have 4 colors I think...1 for Petal.Length that have small length and small width, 1 for small length and large width, 1 for large length and small width, and 1 for large length and large width.

Upvotes: 2

Views: 1476

Answers (3)

B Williams
B Williams

Reputation: 2050

Here is an example using pipes - using your data as is you would need to rbind the length and weight data.frames.

library(tidyverse)
iris %>% 
   mutate(statusl = factor(ifelse(Sepal.Length<5,'Small length', 'Large length')),
          statusw = factor(ifelse(Sepal.Width<3,'Small width', 'Large width')))  %>% 
   ggplot(aes(Petal.Length, fill=interaction(statusl, statusw))) + 
         geom_density(alpha = 0.2) + xlab("Petal Length")

Upvotes: 2

Marius
Marius

Reputation: 60160

There's no need to create separate dataframes for this, you can achieve everything you need using the full iris dataset:

iris$length_binary <- ifelse(iris$Sepal.Length < 5, "Small", "Large")
iris$width_binary <- ifelse(iris$Sepal.Width < 3, "Small", "Large")
iris$length_width = interaction(iris$length_binary, iris$width_binary, sep=", ")

ggplot(iris, aes(Petal.Length, fill = length_width)) + 
    geom_density(alpha = 0.2) + 
    labs(x = "Petal Length",
         fill = "Length, Width")

Result:

enter image description here

Upvotes: 3

bjpreisler
bjpreisler

Reputation: 118

One way to accomplish this is by placing the variable that you would like to stratify the graph upon inside the geom_density layer as such:

ggplot(data = df, aes(x = , y = ) +
  geom_line(aes(color = factored_variable))

For more detail: Plotting two variables as lines using ggplot2 on the same graph

Upvotes: 0

Related Questions