【问题标题】:how to make single stacked bar chart in ggplot2如何在ggplot2中制作单个堆叠条形图
【发布时间】:2019-02-01 13:54:15
【问题描述】:
Ancestry <- data.frame(Race = c("European", "African American", "Asian", "Hispanic", "Other"), Proportion = c(40, 30, 10, 15, 5))
Ancestry %>% 
ggplot(aes(y = Proportion, fill = Race)) + 
  geom_bar(stat="identity", colour="white")

运行上面的代码给我以下错误:

Warning in min(x, na.rm = na.rm) :
  no non-missing arguments to min; returning Inf
Warning in max(x, na.rm = na.rm) :
  no non-missing arguments to max; returning -Inf
Warning in min(diff(sort(x))) :
  no non-missing arguments to min; returning Inf
Warning in x - width/2 :
  longer object length is not a multiple of shorter object length
Warning in x + width/2 :
  longer object length is not a multiple of shorter object length
Error in data.frame(list(y = c(40, 30, 10, 15, 5), fill = c(3L, 1L, 2L,  : 
  arguments imply differing number of rows: 5, 2947

我希望创建一个类似于此的堆积条形图:

【问题讨论】:

    标签: r ggplot2 bar-chart visualization


    【解决方案1】:

    您需要为 x 轴创建一个虚拟变量。然后使用geom_col 类似于geom_bar(stat = "identity") 绘制堆叠条形图+geom_text 将文本放在条形图上。

    您展示的绘图使用了ggthemes 包中的theme_economist

    library(tidyverse)
    
    Ancestry <- data.frame(Race = c("European", "African American", "Asian", "Hispanic", "Other"), 
                           Proportion = c(40, 30, 10, 15, 5))
    
    Ancestry <- Ancestry %>% 
      mutate(Year = "2006")
    
    ggplot(Ancestry, aes(x = Year, y = Proportion, fill = Race)) +
      geom_col() +
      geom_text(aes(label = paste0(Proportion, "%")),
                position = position_stack(vjust = 0.5)) +
      scale_fill_brewer(palette = "Set2") +
      theme_minimal(base_size = 16) +
      ylab("Percentage") +
      xlab(NULL)
    

    library(ggthemes)
    
    ggplot(Ancestry, aes(x = Year, y = Proportion, fill = Race)) +
      geom_col() +
      geom_text(aes(label = paste0(Proportion, "%")),
                position = position_stack(vjust = 0.5)) +
      theme_economist(base_size = 14) +
      scale_fill_economist() +
      theme(legend.position = "right", 
            legend.title = element_blank()) +
      theme(axis.title.y = element_text(margin = margin(r = 20))) +
      ylab("Percentage") +
      xlab(NULL)
    

    reprex package (v0.2.0.9000) 于 2018 年 8 月 26 日创建。

    【讨论】:

    • 你可以使用aes(x = "2006")代替虚拟变量
    【解决方案2】:

    我们可以创建一个虚拟变量来将所有输入分组到一个组中,然后使用geom_bar 创建一个堆积条形图并使用geom_text 命名Proportions

    library(ggplot2)
    
    #Dummy group variable
    Ancestry$row <- 1
    
    #Create a label variable
    Ancestry$percent <- (Ancestry$Proportion/sum(Ancestry$Proportion)) * 100
    
    ggplot(Ancestry, aes(x = row,y = Proportion, fill = Race)) +
        geom_bar(stat="identity") + 
        geom_text(aes(label = percent), 
            position = position_stack(vjust = 0.5))
    

    【讨论】:

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