有关从COVID19 WHO数据集生成条形图的问题

时间:2020-03-20 16:15:10

标签: r graph

我是 R 的新手。我试图从WHO COVID19数据集生成条形图。我能够看到该图,但它似乎无法准确反映实际数据。请查看我的代码,并告诉我哪里出了问题。

数据:Full dataset

new cases in 3 countries

#COVID 19

library(ggplot2)

library(tidyverse)

stats <- read.csv(file.choose())

stats

dim(stats)

colnames(stats) <- c("date","location","new_cases","new_deaths","total_cases","total_deaths")


ThreeCountries <- subset(stats, location =="United States" | location =="China" | location =="Italy")

dim(ThreeCountries)

ggplot(ThreeCountries, aes(x=date, y=new_cases, fill = location)) + 
  geom_bar(stat="identity", position = "dodge")

1 个答案:

答案 0 :(得分:2)

您可以尝试以下方法:

不同之处在于read.csv不会将日期检测为日期,read_csv同时会检测到日期,因此在绘制时,x轴已正确缩放为日期。

library(tidyverse)

df <- read_csv("https://covid.ourworldindata.org/data/ecdc/full_data.csv")
#> Parsed with column specification:
#> cols(
#>   date = col_date(format = ""),
#>   location = col_character(),
#>   new_cases = col_double(),
#>   new_deaths = col_double(),
#>   total_cases = col_double(),
#>   total_deaths = col_double()
#> )

df %>% 
  # use dplyr's filter function instead of subset (dplyr is part of the tidyverse)
  filter(location %in% c("United States", "China", "Italy")) %>% 
  ggplot(aes(x = date, y = new_cases, fill = location)) +
  # geom_col is better suited than geom_bar, as it does not compute stats on the data
  geom_col(position = "dodge")

reprex package(v0.3.0)于2020-03-20创建