0

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!

4
  • 1
    Looks like your Case_date column is either character or factor data type. ggplot2 is looking for a date or datetime data type to map it to a timeline. Commented Mar 9, 2021 at 22:41
  • @JonSpring Thing is if I check the column data it says 'date' everything maps fine but it seems like the points are just out of wack. Commented Mar 10, 2021 at 0:34
  • It would help to include in your question the output that comes from dput(head(COVID_CASES)). This will generate code that will allow us to create an exact replica of some (b/c head just gets the first 6 rows) of your data, including the data types. Commented Mar 10, 2021 at 1:14
  • 1
    The speckles make me suspect that there might be various data points with the same Case_date, and that you are plotting them on top of each other. I suspect there are multiple data points per date, and that you might want to filter or summarize further before plotting. Commented Mar 10, 2021 at 1:19

1 Answer 1

1

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")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.