Calmcode - ggplot: facet

Facet

1 2 3 4 5 6 7 8

Make sure that the following libraries are loaded before running other code.

library(ggplot2)
library(tidyverse)

You can split up the visualisation in groups by using facet_grid.

sub_df <- ChickWeight %>%
  group_by(Chick) %>%
  mutate(max_time = max(Time)) %>%
  ungroup() %>%
  filter(max_time < max(Time))

ggplot() +
  geom_line(data=ChickWeight, aes(x=Time, y=weight, group=Chick), alpha=0.5) +
  geom_line(data=sub_df, aes(x=Time, y=weight, group=Chick), color="red", size=1) +
  facet_grid(Diet ~ .) +
  ggtitle("Growth of Chicken Weight",
          subtitle="Notice that some chickens die prematurely")