【问题标题】:How to combine multiple ggplot geom_col() into one graph?如何将多个 ggplot geom_col() 组合成一张图?
【发布时间】: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()fillgroup 参数来分离或分组数据。
  • @Kat 谢谢你,所以试试...ONE1 &lt;- ggplot(Ireland, aes(TargetGroup, FirstDosePC))+geom_bar(stat = 'identity',width = 0.8, fill = "green") + facet_grid(.~Vaccine) 然后TWO2 &lt;- ONE1 + geom_bar(FDPercent=Italy, aes(Italy$TargetGroup, Italy$FirstDose))+ facet_grid(.~Vaccine) 这是更接近了吗?有错误Warning message: Ignoring unknown parameters: FDPercent
  • 在对geom_ 的不同调用中使用不同的数据集时,您需要设置参数datamapping。因此,对于拨打意大利的电话,请写:geom_col(data = Italy, aes(x = TargetGroup, y = FirstDose))(只是想我会提到,将geom_bar() 视为一个变量,geom_col() 用于同时具有 x 和 y 的情况。不需要身份。)跨度>
  • 谢谢你@Kat 新年;晚餐烧焦了,家人不高兴 - 但想通了,买了 thakeawy(接下来几天的三明治!)新年快乐!

标签: r ggplot2 geom-bar


【解决方案1】:

在 cmets 中 @kat 的帮助下 - 从 geom_bar() 更改为 geom_col() 并删除了第三个变量

Ireland <- subset(FDPercent, Country == "IE") #contructed a subset by country
Italy <- subset(FDPercent, Country == "IT")
Latvia <- subset(FDPercent, Country == "LV")

ONE1 <- ggplot(Italy,  aes(Date, as.numeric(FirstDosePC))) +
  geom_col(fill = "red", alpha = 1, width = 7) +  theme_minimal(base_size = 8) +  
  xlab(NULL) + ylab(NULL) + scale_x_date(date_labels = "%Y/%m/%d") #reduced the theme formating
OneTwo <- ONE1 + geom_col(data=Ireland,  aes(Date, 
    as.numeric(FirstDosePC)),  
    fill="Green",  alpha = 1,width = 5)
OneTwoThree <- OneTwo + geom_col(data=Latvia,  aes(Date, 
    as.numeric(FirstDosePC)),  
    fill="black",  alpha = 1, width = 2)
OneTwoThree + labs(title="Ireland, Italy, & Latvia - First Dose comparision", #added labels to the data
    subtitle="Using First Dose Delivered per day as a percentage of population",              
    caption = "(ECDC, 2021)",
    x="Date Administered",
    y="% Population treated")

【讨论】:

    猜你喜欢
    • 2018-09-27
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    相关资源
    最近更新 更多