How to create layered line plots looping over a variable

Viewed 36
City    Year  Month Deaths  Guns     Shootings
Miami   2010    1   69      73800       701        
Miami   2010    2   99      85050       738         
Miami   2010    3   122     92650       784        
Houston 2010    1   98      92100       789          
Houston 2010    2   146     103900      799         
Houston 2010    3   162     136100      772         

For each city, I want to create a layered line plot with the Month and Year on the x-axis and colored lines corresponding to deaths, guns, and shootings. But I don't see how to do this.

So far I tried

df <- shootings %>%
  select(city, date, sales, volume, median, listings, inventory) %>%
  gather(key = "variable", value = "value", -c(city,date))
head(df)

df<-df[df$city=='Miami',]

ggplot(df, aes(x = date, y = value)) + 
  geom_line(aes(color = variable, linetype = variable))

And that can give me the layered line plot for one specific city, but is there a way to loop this through all the cities in my dataframe? Also how do I have this show the month and year labeled on the x-axis?

Last...

when I do

library(ggplot2)
library(reshape2)
library(tidyverse)

for (city_name in df$city){
    df  %>% filter(city == city_name) %>%
      pivot_longer(-c(city, year, month, date),
               names_to = "Statistic") %>%
      ggplot(aes(x = date, y = log(value))) +
      geom_line(aes(color = Statistic,
                linetype = Statistic,
                group = Statistic))
}

I get no output. Why is this?

1 Answers

Using facet_wrap():

library(tidyverse)

df <- read.table(text = "City    Year  Month Deaths  Guns     Shootings
Miami   2010    1   69      73800       701        
Miami   2010    2   99      85050       738         
Miami   2010    3   122     92650       784        
Houston 2010    1   98      92100       789          
Houston 2010    2   146     103900      799         
Houston 2010    3   162     136100      772         ",
header = TRUE)

df %>%
  mutate(Month = factor(Month, labels = month.name[1:3])) %>%
  pivot_longer(-c(City, Year, Month),
               names_to = "Statistic") %>%
  ggplot(aes(x = Month, y = value)) +
  geom_line(aes(color = Statistic,
                linetype = Statistic,
                group = Statistic)) +
  facet_wrap(City ~ Year)


# The difference between the number of guns and the number of
# death is pretty large; perhaps plot 'log(value)' instead?
df %>%
  mutate(Month = factor(Month, labels = month.name[1:3])) %>%
  pivot_longer(-c(City, Year, Month),
               names_to = "Statistic") %>%
  ggplot(aes(x = Month, y = log(value))) +
  geom_line(aes(color = Statistic,
                linetype = Statistic,
                group = Statistic)) +
  facet_wrap(~ City)


# For Month and Year on the x-axis, one option would be:
df %>%
  mutate(Month = factor(Month, labels = month.abb[1:3])) %>%
  mutate(Month_Year = paste(Month, Year)) %>%
  mutate(Date = factor(Month_Year,
                       levels = paste(
                         month.abb,
                         2010:2010,
                         sep = " "
                         ), ordered = TRUE)) %>%
  select(-Month_Year) %>%
  pivot_longer(-c(City, Year, Month, Date),
               names_to = "Statistic") %>%
  ggplot(aes(x = Date, y = log(value))) +
  geom_line(aes(color = Statistic,
                linetype = Statistic,
                group = Statistic)) +
  facet_wrap(~ City)

Created on 2022-07-03 by the reprex package (v2.0.1)

Related