问题描述
我正在尝试使用ggplot2绘制日期列与数字列。
我有一个数据框,我正在尝试与作为中国或不属于中国的国家/地区进行操作,并成功创建了以下与以下链接的数据框:
is_china<-Confirmed_cases_worldwide%>%
过滤器(国家==中国,类型==已确认)%>%
group_by(国家/地区)%&%;%
变异(cumu_cases =累计(cases))
is_not_china<-Confirmed_cases_worldwide%>%
filter(country! =中国,类型==已确认)%&%;%
变异(cumu_cases =累计(例))
is_not_china $ country<-非中国
china_vs_world<-rbind(is_china,is_not_china)
现在基本上我正在尝试在中国和非中国 $ b $之间绘制带有 cumu_cases
和 date
的折线图b我正在尝试执行以下代码:
plt_china_vs_world<-ggplot(china_vs_world)+
geom_line(aes( x = date,y = cumu_cases,group = country, color = country))+
ylab(累计确诊病例)
现在我继续得到的图形如下所示:
不知道为什么会这样,正在尝试转换数据类型并其他方法。
感谢您的帮助,我在下面同时链接了两个csv
注意:此比例尺显示的中国数字很小。
@Edward提到对数刻度会更易于理解
china_vs_world%>%
group_by(国家/地区,日期)%&%;%
summarise(cumu_cases = sum(case))%&%;%
ungroup%&%;%
mutate(cumu_cases = cumsum(cumu_cases))%>%
ggplot()+
geom_line(aes(x = date,y = cumu_cases, group = country,color = country))+
ylab(累计确诊病例)+
scale_y_continuous(trans ='log')
或者使用 facet_wrap
china_vs_world%>%
group_by(国家/日期)%>%
summarise(cumu_cases = sum (cases))%>%
ungroup%&%;%
mutate(cumu_cases = cumsum(cumu_cases))%>%
ggplot()+
geom_line(aes( x = date,y = cumu_cases,group = country,color = country))+
ylab(累计确诊病例)+
facet_wrap(〜country,scales ='free_y')
数据
china_vs_world<-read.csv(" https://raw.githubusercontent.com/ king-sules / Covid / master / china_vs_world.csv,stringsAsFactors = FALSE)
china_vs_world $ date<-as.Date(china_vs_world $ date)
I am trying to use ggplot2 to plot a date column vs. a numeric column.
I have a dataframe that I am trying to manipulate with country as either china or not china, and successfully created the dataframe linked below with:
is_china <- confirmed_cases_worldwide %>%
filter(country == "China", type=='confirmed') %>%
group_by(country) %>%
mutate(cumu_cases = cumsum(cases))
is_not_china <- confirmed_cases_worldwide %>%
filter(country != "China", type=='confirmed') %>%
mutate(cumu_cases = cumsum(cases))
is_not_china$country <- "Not China"
china_vs_world <- rbind(is_china,is_not_china)
Now essentially I am trying to plot a line graph with cumu_cases
and date
between "china" and "not china"I am trying to execute this code:
plt_china_vs_world <- ggplot(china_vs_world) +
geom_line(aes(x=date,y=cumu_cases,group=country,color=country)) +
ylab("Cumulative confirmed cases")
Now I keep getting a graph looking like this:
Don't understand why this is happening, been trying to convert data types and other methods.Any help is appreciated, I linked both csv below
https://github.com/king-sules/Covid
The 'date' for other 'country' are repeated because the 'country' is now changed to 'Not China'. It would be either changed in the OP's 'is_not_china' step or do this in 'china_vs_world'
library(ggplot2)
library(dplyr)
china_vs_world %>%
group_by(country, date) %>%
summarise(cumu_cases = sum(cases)) %>%
ungroup %>%
mutate(cumu_cases = cumsum(cumu_cases)) %>%
ggplot() +
geom_line(aes(x=date,y=cumu_cases,group=country,color=country)) +
ylab("Cumulative confirmed cases")
-output
NOTE: It is the scale that shows the China numbers to be small.
As @Edward mentioned a log scale would make it more easier to understand
china_vs_world %>%
group_by(country, date) %>%
summarise(cumu_cases = sum(cases)) %>%
ungroup %>%
mutate(cumu_cases = cumsum(cumu_cases)) %>%
ggplot() +
geom_line(aes(x=date,y=cumu_cases,group=country,color=country)) +
ylab("Cumulative confirmed cases") +
scale_y_continuous(trans='log')
Or with a facet_wrap
china_vs_world %>%
group_by(country, date) %>%
summarise(cumu_cases = sum(cases)) %>%
ungroup %>%
mutate(cumu_cases = cumsum(cumu_cases)) %>%
ggplot() +
geom_line(aes(x=date,y=cumu_cases,group=country,color=country)) +
ylab("Cumulative confirmed cases") +
facet_wrap(~ country, scales = 'free_y')
data
china_vs_world <- read.csv("https://raw.githubusercontent.com/king-sules/Covid/master/china_vs_world.csv", stringsAsFactors = FALSE)
china_vs_world$date <- as.Date(china_vs_world$date)
这篇关于使用cumsum在ggplot中绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!