【问题标题】:ggplot2 stacked and group barchart togetherggplot2堆叠和分组条形图在一起
【发布时间】:2019-02-10 00:54:58
【问题描述】:

我正在尝试重现这个

到目前为止我有

但我需要相反的年份和顶部的国家作为标签。

这里有两个 SO 答案

Firstsecond

代码

ggplot(ownership, aes(x = Country, y = Percent, fill = Category)) + 
geom_bar(stat = 'identity', position = 'stack') + facet_grid(~ Year) +
theme_tufte() +
scale_fill_brewer(palette = "Paired") +
theme(axis.title.y = element_blank()) +
theme(axis.title.x = element_blank()) +
theme(legend.text = element_text(size = 10)) +
theme(axis.text.x = element_text(size = 12)) +
theme(axis.text.y = element_text(size = 12)) +
theme(plot.margin = margin(0.1, 0.1, 0.1, 0.1, "cm")) +
theme(legend.position = "bottom") +
theme(legend.title = element_blank())

数据

> dput(ownership)

structure(list(Country = c("Cote d'Ivoire", "Cote d'Ivoire", 
"Ethiopia", "Ethiopia", "Kenya", "Kenya", "Nigeria", "Nigeria", 
"Senegal", "Senegal", "South Africa", "South Africa", "Uganda", 
"Uganda", "Cote d'Ivoire", "Cote d'Ivoire", "Ethiopia", "Ethiopia", 
"Kenya", "Kenya", "Nigeria", "Nigeria", "Senegal", "Senegal", 
"South Africa", "South Africa", "Uganda", "Uganda", "Cote d'Ivoire", 
"Cote d'Ivoire", "Ethiopia", "Ethiopia", "Kenya", "Kenya", "Nigeria", 
"Nigeria", "Senegal", "Senegal", "South Africa", "South Africa", 
"Uganda", "Uganda"), Year = c(2014, 2017, 2014, 2017, 2014, 2017, 
 2014, 2017, 2014, 2017, 2014, 2017, 2014, 2017, 2014, 2017, 2014, 
 2017, 2014, 2017, 2014, 2017, 2014, 2017, 2014, 2017, 2014, 2017, 
 2014, 2017, 2014, 2017, 2014, 2017, 2014, 2017, 2014, 2017, 2014, 
 2017, 2014, 2017), Percent = c(10, 7, 22, 35, 16, 9, 42, 34, 
 9, 11, 56, 50, 9, 9, 5, 7, 0, 0, 39, 47, 2, 5, 3, 10, 13, 17, 
 18, 24, 19, 27, 0, 0, 19, 26, 0, 0, 4, 22, 2, 2, 17, 26), Category = 
 c("Category A", 
 "Category A", "Category A", "Category A", "Category A", "Category A", 
 "Category A", "Category A", "Category A", "Category A", "Category A", 
 "Category A", "Category A", "Category A", "Category B", "Category B", 
 "Category B", "Category B", "Category B", "Category B", "Category B", 
 "Category B", "Category B", "Category B", "Category B", "Category B", 
 "Category B", "Category B", "Category C", "Category C", "Category C", 
 "Category C", "Category C", "Category C", "Category C", "Category C", 
 "Category C", "Category C", "Category C", "Category C", "Category C", 
 "Category C")), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
 ), row.names = c(NA, -42L), spec = structure(list(cols = list(
 Country = structure(list(), class = c("collector_character", 
 "collector")), Year = structure(list(), class = c("collector_double", 
 "collector")), Percent = structure(list(), class = c("collector_double", 
 "collector")), Category = structure(list(), class = 
 c("collector_character", 
 "collector"))), default = structure(list(), class = c("collector_guess", 
 "collector")), skip = 1), class = "col_spec"))

感谢任何提示!

更新

更改X = Yearfacet_grid(~ Country) 后,我得到了更好的结果,但在x 轴上有一些问题。 R 对待年份的方式与我预期的不同。我有 2014 年和 2017 年,它为我提供 2014、2016、2017 年。

【问题讨论】:

  • 请更新您的问题dput(ownership)

标签: r ggplot2 bar-chart stacked-chart


【解决方案1】:

这会让你接近(使用模拟数据,因为你没有提供任何数据):

library(ggplot2)
#make similar data
df <- expand.grid(country = letters[1:4],
                 year = c("2014", "2017"),
                 category = LETTERS[1:3])
df$percent <- runif(nrow(df))


ggplot(df, aes(year, percent, fill = category)) +
  geom_bar(stat = "identity") +
  facet_wrap(~country, ncol = 4, strip.position = "bottom") +
  theme(legend.position = "none")

reprex package (v0.2.1) 于 2019 年 2 月 9 日创建

【讨论】:

  • 我添加了数据。有趣的解决方案,我会保存它。谢谢!
【解决方案2】:

不要使用facet_grid(~ Year),这将在两年内制作两张并排的图表(就像你现在一样)。

您可以使用国家-年份变量获得与您想要的图表相似的图表,因此:

ownership$CountryYear <- paste(ownership$Country, ownership$Year) 

然后:

ggplot(ownership, aes(x = CountryYear, y = Percent, fill = Category)) + 
geom_bar(stat = 'identity', position = 'stack') + 
...

但您可能需要大量使用标签才能获得与您的目标完全相同的图表。

【讨论】:

    【解决方案3】:

    个人解决。我在 x 轴上添加了as.character()

    最终代码

    ggplot(ownership, aes(x = as.character(Year), y = Percent, fill = Category)) + 
    geom_bar(stat = 'identity', position = 'stack') + facet_grid(~ Country) +
    theme_tufte() +
    scale_fill_brewer(palette = "Paired") +
    theme(axis.title.y = element_blank()) +
    theme(axis.title.x = element_blank()) +
    theme(legend.text = element_text(size = 10)) +
    theme(axis.text.x = element_text(size = 12)) +
    theme(axis.text.y = element_text(size = 12)) +
    theme(legend.position = "bottom") +
    theme(legend.title = element_blank()) 
    

    完成!谢谢!

    【讨论】:

      猜你喜欢
      • 2017-11-01
      • 2019-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多