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?


