簡體   English   中英

ggplot2堆疊在一起並將條形圖分組

[英]ggplot2 stacked and group barchart together

我正在嘗試重現 在此處輸入圖片說明

到目前為止,我有 在此處輸入圖片說明

但是我需要相反的內容-下一年和上一個國家/地區作為標簽。

兩個SO答案在這里

第一第二

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。

在此處輸入圖片說明

這可以使您關閉(使用模擬數據,因為您未提供任何數據):

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軟件包 (v0.2.1)創建於2019-02-09

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

但是您可能需要在標簽上進行很多操作才能獲得與您想要的圖形完全相同的圖形。

親自解決。 我將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()) 

做完了! 謝謝!

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM