我来自一个有3个不同区域的省。我有一个数据框,其中包含全省covid19的所有日子和死亡情况。我的想法是按周或按月绘制数据。这7天或30天的总和。但是我想用3种颜色来区别3个不同区域。
这就是我的代码。我可以绘制总列。这3个区域称为:阿利坎特,巴伦西亚,卡斯特利翁。
我不知道该怎么做!
library(ggplot2)
library(scales)
log <- read.csv('https://dadesobertes.gva.es/es/datastore/dump/69c32771-3d18-4654-8c3c-cb423fcfa652?bom=True',stringsAsFactors = F,encoding = 'UTF-8')
colnames(log) <- c("code", "Date", "total", "hombres", "mujeres", "alicante", "castellon", "valencia", "dvinaros", "dcastellon", "dlaplana", "dsangunto", "dmalvarrosa", "dvilanova", "dlafe", "drequena", "dvalenciageneral", "dpeset", "dlaribera", "dgandia", "ddenia", "dxativa", "dalcoy", "dlamarina", "dsanjuan", "delda", "dalicantegeneral", "delchegeneral", "dorihuela", "dtorrevieja", "dmanises", "delchecrevillente" )
#log$Date <- as.Date(log$Date,
log$Date <- as.Date(log$Date,
"%Y-%m-%dT%H:%M:%S") # tabulate all the options here
# create variables of the week and month of each observation:
log$Mes <- as.Date(cut(log$Date,
breaks = "month"))
log$Week <- as.Date(cut(log$Date,
breaks = "week",
start.on.monday = FALSE)) # changes weekly break point to Sunday
# graph by month:
ggplot(data = log,
aes(Week, total, fill="Defunciones semanales")) +
stat_summary(fun.y = sum, # adds up all observations for the month
geom = "bar") +
labs(fill = "Color", y = "") +
#geom_text(aes(y = total,label = total), vjust=0, hjust= 0,size=4) +
labs(title = "Defunciones semanales en la Comunidad Valenciana hasta el 17 de Enero",
subtitle = "Fuente:dadesobertes.gva.es/es/dataset/covid-19-series-personas-fallecidas. ") +
scale_x_date(
#labels = date_format( "%B"),
labels = date_format( "%d-%m"),
limits=c(as.Date("2020-03-01"), as.Date("2021-02-01")),
breaks = "1 week") + # custom x-axis labels
theme(axis.text.x=element_text(angle=60, hjust=1))
这在数据整理中比在绘图中更多。为了获得理想的结果,请使用例如将数据重整为长格式tidy::pivot_longer
。另外设置position
为"stack"
,stat_summary
以堆叠区域的条形图。
library(ggplot2)
library(scales)
library(tidyr)
library(dplyr)
log <- read.csv("https://dadesobertes.gva.es/es/datastore/dump/69c32771-3d18-4654-8c3c-cb423fcfa652?bom=True", stringsAsFactors = F, encoding = "UTF-8")
colnames(log) <- c("code", "Date", "total", "hombres", "mujeres", "alicante", "castellon", "valencia", "dvinaros", "dcastellon", "dlaplana", "dsangunto", "dmalvarrosa", "dvilanova", "dlafe", "drequena", "dvalenciageneral", "dpeset", "dlaribera", "dgandia", "ddenia", "dxativa", "dalcoy", "dlamarina", "dsanjuan", "delda", "dalicantegeneral", "delchegeneral", "dorihuela", "dtorrevieja", "dmanises", "delchecrevillente")
log$Date <- as.Date(
log$Date,
"%Y-%m-%dT%H:%M:%S"
) # tabulate all the options here
log$Mes <- as.Date(cut(log$Date,
breaks = "month"
))
log$Week <- as.Date(cut(log$Date,
breaks = "week",
start.on.monday = FALSE
)) # changes weekly break point to Sunday
# select desired or needed variables and reshape to long format
log_area <- select(log, 1:2, 6:8, Mes, Week) %>%
pivot_longer(-c(code, Date, Mes, Week), names_to = "area")
# graph by month:
ggplot(
data = log_area,
aes(Week, value, fill = area)
) +
stat_summary(
fun.y = sum, # adds up all observations for the month
geom = "bar",
position = "stack"
) +
labs(
fill = "Color", y = "",
title = "Defunciones semanales en la Comunidad Valenciana hasta el 17 de Enero",
subtitle = "Fuente:dadesobertes.gva.es/es/dataset/covid-19-series-personas-fallecidas. "
) +
scale_x_date(
labels = date_format("%d-%m"),
limits = c(as.Date("2020-03-01"), as.Date("2021-02-01")),
breaks = "1 week"
) + # custom x-axis labels
theme(axis.text.x = element_text(angle = 60, hjust = 1))
#> Warning: `fun.y` is deprecated. Use `fun` instead.
#> Warning: Removed 87 rows containing non-finite values (stat_summary).
#> Warning: Removed 3 rows containing missing values (geom_bar).
由reprex软件包(v1.0.0)创建于2021-01-30
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句