Calmcode - ggplot: story

Story

1 2 3 4 5 6 7 8

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

library(ggplot2)
library(tidyverse)

Let's combine dplyr with ggplot2 in the codesnippet below. First we'll do some data wrangling, after which it should be easier to make a proper chart.

# Create a subset dataframe
sub_df <- ChickWeight %>%
  group_by(Chick) %>%
  mutate(max_time = max(Time)) %>%
  ungroup() %>%
  filter(max_time < max(Time))

# Make a good looking chart
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) +
  ggtitle("Growth of Chicken Weight",
          subtitle="Notice that some chickens die prematurely")

Pay attention to the details here. The goal is to make a chart that is easy to consume.