【问题标题】:How to add percentages to bar chart facets in ggplot2 in R?如何在 R 中的 ggplot2 中向条形图方面添加百分比?
【发布时间】:2016-11-24 18:22:28
【问题描述】:

我想使用 ggplot2 制作一个图,以便有条形图显示人们在每种城市/农村环境(方面)中的度数(条形)。我做到了。

现在我想为每个方面添加具有各种资格的人的比例。我使用下面的代码得到的是整个人口的百分比

如何更改代码以便在每个方面计算百分比?

这是我使用的数据集中包含 1,000 行的示例:link

library(ggplot2)
library(scales)

# plot urban/rural by degree in facets
 myplot <- ggplot(data = si
                     ,aes(DEGREE)
    ) 
    myplot <- myplot + geom_bar()
    myplot <- myplot + labs(title = "Degree by Urban/Rural", y = "Percent", x = "DEGREE")
    myplot <- myplot + geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))), stat = "count", vjust = -0.25)
    myplot <- myplot + facet_wrap(~URBRURAL)
    myplot <- myplot + theme(axis.text.x = element_text(angle = 20, hjust = 1))
    myplot

【问题讨论】:

  • 可以分享示例数据吗?
  • 很多潜在的重复,包括hereherehere。您是否尝试过这些答案中的任何选项?
  • 我检查了这些答案,但没有帮助。
  • 您已重命名 y 轴并将比例更改为百分比,但条形的高度仍然很重要。这真的是你的意图吗?

标签: r ggplot2 visualization data-visualization


【解决方案1】:

您始终可以在绘制数据之前转换数据以计算您想要的数据。我还添加了一些调整(条形顶部的标签、x 轴上的字符串环绕、轴限制和标签)。

library(dplyr)
library(ggplot2)
library(stringr)

plot_data <- df %>% 
  group_by(URBRURAL, DEGREE) %>% 
  tally %>% 
  mutate(percent = n/sum(n))

ggplot(plot_data, aes(x = DEGREE, y = percent)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = percent(percent)), vjust = -0.5) +
  labs(title = "Degree by Urban/Rural", y = "Percent", x = "DEGREE") +
  scale_y_continuous(labels = percent, limits = c(0,1)) +
  scale_x_discrete(labels = function(x) str_wrap(x, 10)) +
  facet_wrap(~URBRURAL) 

【讨论】:

  • 用于计算和改进图形的简洁明了的解决方案。但是,您提供的代码仍会在 x 轴上打印倾斜的标签,而不是如图所示包装。拜托,你能添加包装标签的代码吗?谢谢。
  • 谢谢杰克。 scales_x_discrete() 创建一个错误。它应该是scale_x_discrete()。不幸的是,我不允许进行少于 6 个字符的编辑,所以请您自己更正错字 - 谢谢。
  • 错字已修复
【解决方案2】:

我认为这是可行的:

si <- read.csv('sampledata.csv', sep=' ')
myplot <- ggplot(data = si
                 ,aes(DEGREE)
) 
myplot <- myplot + geom_bar()
myplot <- myplot + labs(title = "Degree by Urban/Rural", y = "Percent", x = "DEGREE")
myplot <- myplot +  geom_text(aes(y = ((..count..)/tapply(..count..,..PANEL..,sum)[..PANEL..]), label = scales::percent((..count..)/tapply(..count..,..PANEL..,sum)[..PANEL..])), stat = "count", vjust = -0.25)
myplot <- myplot + facet_wrap(~URBRURAL)
myplot <- myplot + theme(axis.text.x = element_text(angle = 20, hjust = 1))
myplot

实际上 y 轴标签不是百分比而是实际计数,因为它们在您的原始图中,条形图上的标签代表百分比,请看下面的第 18 行,这表明 45 不是百分比,而是该组的实际计数在您提供的示例数据中,而在相应方面的同一条上的 15.7% 表示百分比。

library(dplyr)
as.data.frame(si %>% group_by(URBRURAL, DEGREE) %>% summarise(n=n()))

1  Country village, other type of community Above higher secondary level, other qualification  6
2  Country village, other type of community                        Above lowest qualification 16
3  Country village, other type of community                        Higher secondary completed  9
4  Country village, other type of community                       Lowest formal qualification 31
5  Country village, other type of community                           No formal qualification 20
6  Country village, other type of community                       University degree completed  1
7               Farm or home in the country                        Above lowest qualification  1
8               Farm or home in the country                        Higher secondary completed  1
9               Farm or home in the country                       Lowest formal qualification  5
10              Farm or home in the country                           No formal qualification  1
11              Farm or home in the country                       University degree completed  1
12           Suburb, outskirt of a big city Above higher secondary level, other qualification 45
13           Suburb, outskirt of a big city                        Above lowest qualification 57
14           Suburb, outskirt of a big city                        Higher secondary completed 75
15           Suburb, outskirt of a big city                       Lowest formal qualification 48
16           Suburb, outskirt of a big city                           No formal qualification 23
17           Suburb, outskirt of a big city                       University degree completed 15
18                       Town or small city Above higher secondary level, other qualification 45

【讨论】:

  • 条形高度与百分比比例不匹配,例如,kower 左侧面(“城镇或小城市”)的第一个条形是标签 15.7%,但条形达到最高为 45%。
  • 实际上 y 轴标签不是百分比,而是 实际计数,因为它们在您的原始图中,条形图上的标签代表 百分比,更新了发布。
猜你喜欢
  • 2020-10-12
  • 1970-01-01
  • 1970-01-01
  • 2016-06-16
  • 2021-07-17
  • 2022-01-18
  • 2023-01-04
  • 1970-01-01
相关资源
最近更新 更多