简体   繁体   中英

R Shiny ggplot2 leaflet - how to plot multiple fields on X axis of line graph

I'm new at creating plots in R, and I'm running into problems with plotting the data I have.

I'm working in R - Shiny app, using Leaflet to display counties in the US, and ggplot2 to create line graph plots when user clicks on the county.

Here's an example of my data, it counts the newest cases of covid-19 per day:

county  state   04/04/2020  04/05/2020  04/06/2020  04/07/2020  04/08/2020
Autauga Alabama    09           09          11         15           18

What I want is dates on the X axis, and number of cases on the Y axis, but because of the data's structure, each date is an individual field, and each new covid-19 case is a row. Everything I've seen on creating simple line graphs involves using two fields (IE -> X for one field, "date", and Y for field "cases".) But I'm not sure how to plug in those values based on the data structure I'm working off of (where the column is the date, and the rows are the cases).

Seems like there should be an easy solution, but I'm not finding it. If anyone knows where to find those resources, or how to get started, please let me know!

ggplot2 needs data on the long form while your data is wide .

You can convert your data with pivot_longer() from package tidyr :

long_data <- pivot_longer(my_data, c(-county, -state), names_to = "date", values_to = "cases)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM