Skip to content
Prev 385274 / 398502 Next

Ggplot2 Line Problem

Hello,

This type of problem is almost always a data reshaping problem.
ggplot graphics work better if the data is in the long format and you 
have 3 columns for counts, one column for each category. If you reformat 
from the current wide format to the long format you will have a date 
vector, a categorical variable and a counts variable.

In the code below just change geom_point to geom_line and the problem is 
solved.


library(tidyverse)
library(lubridate)

datO <- read.csv("https://api.covidtracking.com/v1/states/oh/daily.csv")
datO[ ,1] <- ymd(datO[ ,1])

dfO <- tibble::as_tibble(data.frame(date = datO[ ,"date"],
                                     positive = datO[ ,"positive"],
                                     negative = datO[ ,"negative"],
                                     total = datO[ ,"total"]))

dfO %>%
   pivot_longer(
     cols = -date,
     names_to = "cases",
     values_to = "count"
   ) %>%
   mutate(cases = factor(cases, levels = c("positive", "negative", 
"total"))) %>%
   ggplot(aes(date, count, color = cases)) +
   geom_point() +
   scale_color_manual(name = "Test",
                      labels = c("Positive", "Negative", "Total"),
                      values = c("red", "blue", "green")) +
   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 02:00 de 17/08/20, Stephen P. Molnar escreveu: