简体   繁体   English

如何在 R 中 plot 多折线图

[英]How to plot a multiple line graph in R

I have to create a plot with the number of COVID-19 confirmed cases by date, in each country.我必须创建一个 plot,其中包含每个国家/地区按日期划分的 COVID-19 确诊病例数。 I have to use the data inside the package: https://cran.r-project.org/web/packages/coronavirus/index.html .我必须使用 package 中的数据: https://cran.r-project.org/web/packages/coronavirus/index.html

I managed to create a subset with the variables "Country Region", "Type" (confirmed only), "date", and "total of cases".我设法用变量“国家地区”、“类型”(仅确认)、“日期”和“病例总数”创建了一个子集。 However, i don't know to plot a graph with multiple lines.但是,我不知道 plot 有多条线的图。

I have to plot a graph with all countries in it, based on the: https://twitter.com/thomasfujiwara/status/1249817958874001412?s=20我必须 plot 一个包含所有国家的图表,基于: https://twitter.com/thomasfujiwara/status/1249817958874001412?s=20

I also want to exclude mainland china from the dataset我也想从数据集中排除中国大陆

Can someone help me?有人能帮我吗?

It's very difficult to help without a reproducible example, but one way would be to use ggplot2 .如果没有可重复的示例,很难提供帮助,但一种方法是使用ggplot2 You can use either the group or the color aesthetic in your plot:您可以在 plot 中使用groupcolor美学:

library(coronavirus)
library(dplyr)
library(ggplot2)
data(coronavirus)

coronavirus %>%
    filter(type == "confirmed") %>%
    filter(Country.Region != "Mainland China") %>% 
    group_by(Country.Region, date) %>%
    summarise(total = sum(cases)) %>% 
    ggplot(aes(x = date, y = total, color = Country.Region)) + 
    geom_line()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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