GGPLOT2堆叠并组条形图一起

问题描述 投票:0回答:3

我试图重现thisenter image description here

到目前为止,我有enter image description here

但我需要相反 - 在顶部的标签,低于去年和和国家。

两个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年。

enter image description here

r ggplot2 bar-chart stacked-chart
3个回答
1
投票

不使用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') + 
...

但是你可能要玩很多有标签得到酷似你的目标的一个图。


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创建于2019年2月9日(v0.2.1)


0
投票

亲自解决。我添加as.character()到x轴。

最终代码

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()) 

完成!谢谢!

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.