繁体   English   中英

如何在 ggplot2 中制作单个堆叠条形图?

[英]How to make single stacked bar chart in ggplot2?

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

我正在寻找创建类似于此的堆叠条形图:

在此处输入图像描述

您需要为x轴创建一个虚拟变量。 然后使用geom_col其类似于geom_bar(stat = "identity")绘制在层叠barplot + geom_text把栏上的文本。

该图显示,您使用theme_economistggthemes包。

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软件包 (v0.2.0.9000)创建于2018-08-26。

我们可以创建一个虚拟变量以将所有geom_bar分组为一组,然后使用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))

在此处输入图片说明

您无需为 x 轴创建虚拟分组变量即可创建单个堆积条。 您可以在特定轴的aes()中添加一个空字符串。 此外, theme_blank()删除所有不必要的网格以保持整洁。

library(tidyverse)
Ancestry <- tibble(Race = c("European", "African American", "Asian", "Hispanic", "Other"), 
                   Proportion = c(40, 30, 10, 15, 5))
ggplot(Ancestry, aes(x = "", y = Proportion, fill = Race)) +
       geom_col() +
       geom_text(aes(label = paste0(Proportion, "%")), 
                     position = position_stack(vjust = 0.5)) +
       scale_fill_brewer(palette = "Set2") +
       theme_blank() +
       labs(x="", y="Percentage")

单线堆叠条形图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM