Mia P
Mia P

Reputation: 73

issue with displaying Dates on Plot in R

Apologies if there is already a solution I am unable to see the other instances of this question at the moment.

I am currently plotting data from a COVID Dataset which takes the number of cases Daily. I want to plot the results with the dates as the x axis and the number of cases as the y. I found an example which works and have implemented it

ggplot(data = COVID_CASES, aes(x = Case_date, y = cases)) +
  geom_bar(stat = "identity", fill = "purple") +
  labs(title = "Total Cases within the US",
       subtitle = "2020 -2021",
       x = "Date", y = "Number of Cases")

I am, unfortunately, unhappy with the look of the plot

an unfortunate view of a plot which has too many dates to display properly

particularly, the fact that the dates can't be read at all... Does anyone have any idea how to fix this? what if it could simply list the months or what have you. All help is appreciated thank you!

Upvotes: 0

Views: 217

Answers (1)

s__
s__

Reputation: 9485

Glancing at the data, you should work on them before plot them, because it seems you have more than one cases row for each date.
Staying in the tidyverse, you can use the dplyr package.

library(dplyr)
library(ggplot)

COVID_CASES %>%
  group_by(Case_date) %>%
  summarise(cases_ = sum(cases)) %>%
ggplot( aes(x = Case_date, y = cases_)) +
   geom_bar(stat = "identity", fill = "purple") +
   labs(title = "Total Cases within the US",
       subtitle = "2020 -2021",
       x = "Date", y = "Number of Cases")

Upvotes: 1

Related Questions