首页 > 解决方案 > 关于从 COVID19 WHO 数据集生成条形图的问题

问题描述

我是新来的R。我试图从 WHO COVID19 数据集生成条形图。我能够看到图表,但它似乎无法准确反映实际数据。请看一下我的代码,让我知道哪里出错了。

数据:完整数据集

3个国家的新病例

#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")

标签: rgraph

解决方案


你可以试试这个:

不同之处在于,它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 包(v0.3.0)于 2020-03-20 创建


推荐阅读