Skip to content
Prev 385275 / 398502 Next

Ggplot2 Line Problem

Hello,

Sorry, I forgot you also want the line type changed.
Remove color and linetype from the initial call to ggplot and include 
aes(color = cases, linetype = cases) in geom_line. Then add a layer 
scale_linetype_manual with the same name and labels to merge it with the 
color legend.



dfO %>%
   pivot_longer(
     cols = -date,
     names_to = "cases",
     values_to = "count"
   ) %>%
   mutate(cases = factor(cases, levels = c("positive", "negative", 
"total"))) %>%
   ggplot(aes(date, count)) +
   #geom_point() +
   geom_line(aes(color = cases, linetype = cases)) +
   scale_color_manual(name = "Test",
                      labels = c("Positive", "Negative", "Total"),
                      values = c("red", "blue", "green")) +
   scale_linetype_manual(name = "Test",
                         labels = c("Positive", "Negative", "Total"),
                         values = c("solid", "dotdash", "twodash")) +
   ylim(0, 1750000) +
   labs(x = "Date", y = "Number of Tests")+
   ggtitle("COVID-19 Tests in Ohio \n (8/15/20)")+
   theme_bw() +
   theme(axis.text.x = element_text(angle = 30, hjust = 1),
         plot.title = element_text(hjust = 0.5))


Hope this helps,

Rui Barradas

?s 06:49 de 17/08/20, Rui Barradas escreveu: