代码之家  ›  专栏  ›  技术社区  ›  Anakin Skywalker

ggplot2堆叠在一起并将条形图分组

  •  0
  • Anakin Skywalker  · 技术社区  · 6 年前

    我想复制这个 enter image description here

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

    但我需要相反的-年以下和国家在顶部作为标签。

    两个答案在这里

    First second

    代码

    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 = Year facet_grid(~ Country) 在X轴上有个问题,我得到了一个更好的结果。R对待年份的方式与我预期的不同。我有2014年和2017年,它为我提供了2014年、2016年和2017年。

    enter image description here

    3 回复  |  直到 6 年前
        1
  •  1
  •   Amadou Kone    6 年前

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

    但是你可能需要在标签上花很多时间才能得到一个与你的目标完全相同的图表。

        2
  •  1
  •   Chase    6 年前

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

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

    创建日期:2019-02-09 reprex package (v0.2.1)

        3
  •  0
  •   Anakin Skywalker    6 年前

    亲自解决。我补充说 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