简体   繁体   中英

COVID19 data-how do I combine two data sets and plot in one figure?

I know how to plot the figures separately but don't know how to combine the data and plot in one figure. my problem is the original data doesn't include days so I have to calculate it and add to the data frame. and I feel my way of doing it is not very smart.

`library(tidyverse)`

`df <- read_csv("https://covid.ourworldindata.org/data/ecdc/full_data.csv")`

###days vs log  in US

`filt1_US <- df$location %in% "United States"`

`filt2_US <-df$total_cases > 100`

`filt3_US <-df[filt1_US & filt2_US,]`


`daysUS <- seq(1:23)`
         # 23 days (since total cases>100)  3/3 - 3/25

`newUS <- cbind(filt3_US,daysUS)`

`ggplot(data=newUS, aes(x=days, y=total_cases))+
 geom_line(stat = "identity",linejoin = "round",color="red")+
 scale_y_log10()+ 
 xlab("Day of Infection")+
 ylab("Total Number of Cases")+
 ggtitle("Total COVID19 Cases in "US")+
 theme( text = element_text(family="Arial"),
     axis.title.x = element_text(color="DarkGreen",size=20),
     axis.title.y = element_text(color="DarkGreen",size=15),
     axis.text.x = element_text(size=10),
     axis.text.y = element_text(size=10),
     legend.title = element_text(size=15,face = "bold"),
     legend.text = element_text(size=10),         
     plot.title = element_text(color="Dark blue",
                               size=20,face = "bold"))`

days vs log in China

`filt4_China <- df$location %in% "China"
 filt5_China <-df$total_cases > 100
 filt6_China <-df[filt4_China & filt5_China,]`

`daysChina <- seq(1:67)`            # 67days  1/19 - 3/25

`newChina <- cbind(filt6_China,daysChina)`

`ggplot(data=newChina, aes(x=daysChina, y=total_cases))+
 geom_line(stat = "identity",linejoin = "round",color="red")+
 scale_y_log10()+ 
 xlab("Day of Infection")+
 ylab("Total Number of Cases")+
 ggtitle("Total COVID19 Cases in China")+
 theme( text = element_text(family="Arial"),
     axis.title.x = element_text(color="DarkGreen",size=20),
     axis.title.y = element_text(color="DarkGreen",size=15),
     axis.text.x = element_text(size=10),
     axis.text.y = element_text(size=10),
     legend.title = element_text(size=15,face = "bold"),
     legend.text = element_text(size=10),
     plot.title = element_text(color="Dark blue",
                               size=20,face = "bold"))`

It is simpler to filter the rows for "China and the "United States" and then plot the graph with the colour aesthetics in the initial call to ggplot . To make the difference between the countries I have added a custom colour scheme. Change at will.

df %>%
  filter(location %in% c("China", "United States")) %>%
  group_by(location) %>%
  mutate(days = row_number()) %>%
  ggplot(aes(x = days, y = total_cases, colour = location))+
  geom_line(stat = "identity", linejoin = "round") +
  scale_color_manual(values = c("red", "blue")) +
  scale_y_log10() + 
  xlab("Day of Infection") +
  ylab("Total Number of Cases") +
  ggtitle("Total COVID19 Cases") +
  theme( text = element_text(family="Arial"),
         axis.title.x = element_text(color="DarkGreen",size=20),
         axis.title.y = element_text(color="DarkGreen",size=15),
         axis.text.x = element_text(size=10),
         axis.text.y = element_text(size=10),
         legend.title = element_text(size=15,face = "bold"),
         legend.text = element_text(size=10),
         plot.title = element_text(color="Dark blue",
                                   size=20,face = "bold"))

在此处输入图片说明

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