r - 关于从 COVID19 WHO 数据集生成条形图的问题
问题描述
我是新来的R。我试图从 WHO COVID19 数据集生成条形图。我能够看到图表,但它似乎无法准确反映实际数据。请看一下我的代码,让我知道哪里出错了。
数据:完整数据集
#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")
解决方案
你可以试试这个:
不同之处在于,它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 创建
推荐阅读
- gradle - Kotlin 1.3.20 与 Gradle 5.1.1 compileKotlin2Js 执行热重载
- powershell - “-ErrorAction Stop”在 try/catch 中不起作用
- visual-studio-2017 - 在 ASP.NET Core 中使用自定义环境
- python-2.7 - python - 如何在python中刷新chatterbot bot的训练?
- python - Selenium:复杂的 XPath 示例
- git - 当 git 创建树?
- django - Django Migrations:在使用数据库后修改模型的字段
- java - Optional 的 GC 开销
在爪哇 - google-analytics - 在 Google Analytics 中 - 我如何直接链接到今天的统计数据?
- javascript - 如何使用字节数据在 React js 中生成 PDF