【发布时间】:2022-01-01 17:57:18
【问题描述】:
问题
我正在尝试使用 dodge 将三个 ggplot geom_bar 组合成一个 geom_bar 图,这样我就可以直观地比较两个分类变量和一个数值变量的数据。我做错了什么?
单独的图表工作 每个图表都可以独立工作(有格式问题),我一直在关注How to overlay two geom_bar? 这样的答案,但我不明白需要做什么。
ONE <- ggplot(Ireland, aes(TargetGroup, FirstDosePC))+geom_bar(stat = 'identity',width = 0.8, fill = "green") +
facet_grid(.~Vaccine) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+
labs(title="1st Dose Ireland by Group & Vaccine Type",
caption = "(ECDC, 2021)",
x="Target Groups over 18",
y="First Dose Administered")
TWO <- ggplot(Italy, aes(TargetGroup, FirstDosePC))+ geom_bar(stat = 'identity',width = 0.8, fill = "blue") +
facet_grid(.~Vaccine) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+
labs(title="1st Dose Italy by Group & Vaccine Type",
caption = "(ECDC, 2021)",
x="Target Groups over 18",
y="First Dose Administered")
THREE <- ggplot(Latvia, aes(TargetGroup, FirstDosePC))+geom_bar(stat = 'identity',width = 0.8, fill = "red") +
facet_grid(.~Vaccine) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+
labs(title="1st Dose Latvia by Group & Vaccine Type",
caption = "(ECDC, 2021)",
x="Target Groups over 18",
y="First Dose Administered")
失败代码示例
我的编码尝试看起来很接近这个,但它似乎失败了——我不明白为什么。我希望学习如何将三个图表与标签一起添加并使用dodge
OneTwo <- ONE + geom_bar(FDPercent=Italy, aes(TargetGroup, FirstDose))+ geom_bar(stat = 'identity',width = 0.8, fill = "blue") +
facet_grid(.~Vaccine) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+
labs(title="1st Dose Italy by Group & Vaccine Type",
caption = "(ECDC, 2021)",
x="Target Groups over 18",
y="First Dose Administered")
我的个人图表如下所示
我的目标图表类型 我的目标是这样的,但通过疫苗类型将其分解以扩展我的学习等(来源https://towardsdatascience.com/track-covid-19-data-yourself-with-r-eb3e641cd4b3)
我的原始数据来自
data <- read.csv("https://opendata.ecdc.europa.eu/covid19/vaccine_tracker/csv/data.csv", na.strings = "", fileEncoding = "UTF-8-BOM")
并被操纵以测试 R 函数,它给我留下了一个名为 FDPercent 的数据框,其中一个名为 FirstDosePC 的数字列(每个国家人口的第一剂百分比)与 30 个欧盟国家的 Country 相关联(ISO 3166-1-alpha-2 分类数据)和数据框中的 10 个TargetGroup 类型(分类)。
> dput(head(FDPercent,3))
structure(list(Country = c("AT", "AT", "AT"), NumberDosesReceived = c(0L,
0L, 61425L), NumberDosesExported = c(0L, 0L, 0L), FirstDose = c(0L,
0L, 87L), FirstDoseRefused = c(NA_integer_, NA_integer_, NA_integer_
), SecondDose = c(0L, 0L, 0L), UnknownDose = c(0L, 0L, 0L), TargetGroup = c("Age18_24",
"Age18_24", "Age18_24"), Vaccine = c("UNK", "AZ", "COM"), Population = c(8901064L,
8901064L, 8901064L), Date = structure(c(18624, 18624, 18624), class = "Date"),
FirstDosePC = c("0.0000", "0.0000", "0.0010")), row.names = 21:23, class = "data.frame")
> str(FDPercent)
'data.frame': 116532 obs. of 12 variables:
$ Country : chr "AT" "AT" "AT" "AT" ...
$ NumberDosesReceived: int 0 0 61425 0 0 0 61425 0 0 0 ...
$ NumberDosesExported: int 0 0 0 0 0 0 0 0 0 0 ...
$ FirstDose : int 0 0 87 0 0 0 1299 0 0 0 ...
$ FirstDoseRefused : int NA NA NA NA NA NA NA NA NA NA ...
$ SecondDose : int 0 0 0 0 0 0 0 0 0 0 ...
$ UnknownDose : int 0 0 0 0 0 0 0 0 0 0 ...
$ TargetGroup : chr "Age18_24" "Age18_24" "Age18_24" "Age18_24" ...
$ Vaccine : chr "UNK" "AZ" "COM" "MOD" ...
$ Population : int 8901064 8901064 8901064 8901064 8901064 8901064 8901064 8901064 8901064 8901064 ...
$ Date : Date, format: "2020-12-28" "2020-12-28" "2020-12-28" "2020-12-28" ...
$ FirstDosePC : chr "0.0000" "0.0000" "0.0010" "0.0000" ...
【问题讨论】:
-
patwork 这样的任务是否过于复杂? github.com/thomasp85/patchwork>
-
您不能堆叠
gggplot()、theme()或scale_。您只能堆叠geom_、stat_或annotate。或者,您可以删除分离的数据集,在ggplot()之前使用filter()和fill或group参数来分离或分组数据。 -
@Kat 谢谢你,所以试试...
ONE1 <- ggplot(Ireland, aes(TargetGroup, FirstDosePC))+geom_bar(stat = 'identity',width = 0.8, fill = "green") + facet_grid(.~Vaccine)然后TWO2 <- ONE1 + geom_bar(FDPercent=Italy, aes(Italy$TargetGroup, Italy$FirstDose))+ facet_grid(.~Vaccine)这是更接近了吗?有错误Warning message: Ignoring unknown parameters: FDPercent -
在对
geom_的不同调用中使用不同的数据集时,您需要设置参数data或mapping。因此,对于拨打意大利的电话,请写:geom_col(data = Italy, aes(x = TargetGroup, y = FirstDose))(只是想我会提到,将geom_bar()视为一个变量,geom_col()用于同时具有 x 和 y 的情况。不需要身份。)跨度> -
谢谢你@Kat 新年;晚餐烧焦了,家人不高兴 - 但想通了,买了 thakeawy(接下来几天的三明治!)新年快乐!