百分比标签为100%堆栈图表尝试"变异"但没有工作

时间:2018-02-02 07:13:31

标签: r ggplot2

我必须在ggplot2中绘制带有%年龄标签的100%堆栈图表。使用我正在使用的数据附加代码。问题可能是重复的,但已经尝试过解决方案,但是给出了错误或显示了与下面相同的情节。

输出输出:

structure(list(Category = structure(c(2L, 3L, 4L, 5L, 2L, 3L, 
4L, 5L, 2L, 3L, 4L, 5L, 2L, 3L, 4L, 5L, 2L, 3L, 4L, 5L), .Label = c("_", 
"1", "2", "3", "4"), class = "factor"), variable = structure(c(1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 
5L, 5L, 5L), .Label = c("IL1_Flag", "IL2_Flag", "IL3_Flag", "IL4_Flag", 
"IL5_Flag"), class = "factor"), value = c(21, 17, 16, 219, 20, 
17, 15, 207, 20, 15, 15, 204, 20, 15, 15, 202, 20, 15, 15, 208
)), row.names = c(NA, -20L), .Names = c("Category", "variable", 
"value"), class = "data.frame")

library(ggplot2)
library(Hmisc)
library(plyr)
library(reshape2)
library(dplyr)
library(scales)
library(tidyverse)

#Plotting the 100% Stacked Chart
ggplot() + geom_bar(
  aes(y = value, x = Category, fill = variable),
  data = agg_melt,
  stat = "identity" ,
  position = "fill"
) + scale_y_continuous(labels = percent) +labs(x="Category" , y= "Percentage (%)")

我已经使用此代码尝试替换值,但它无效。

agg_melt %>%
  mutate(Percentage = value / sum(value)) %>%
  ggplot(aes(
    x = Category,
    y = Percentage,
    fill = `variable`,
    label = paste0(round(Percentage * 1000), "%")
  )) +
  geom_bar(position = "fill",
           color = "black" ,
           stat = "identity") +
  geom_text(position = position_fill(vjust = .5)) +
  scale_y_continuous(labels = scales::percent_format())

有人可以帮助他们获得百分比标签吗? enter image description here

1 个答案:

答案 0 :(得分:3)

您可以在将结果传递给ggplot之前计算数据框中的百分比。

在这里,我假设您想要每个类别中的百分比(但如果您想要总体百分比,只需注释掉group_by()行):

library(dplyr)

agg_melt <- agg_melt %>%
  group_by(Category) %>%
  mutate(p = value / sum(value))

> head(agg_melt)
# A tibble: 6 x 4
# Groups: Category [4]
  Category variable value     p
  <fctr>   <fctr>   <dbl> <dbl>
1 1        IL1_Flag  21.0 0.208
2 2        IL1_Flag  17.0 0.215
3 3        IL1_Flag  16.0 0.211
4 4        IL1_Flag 219   0.211
5 1        IL2_Flag  20.0 0.198
6 2        IL2_Flag  17.0 0.215

将此修改后的数据框传递给ggplot。您还可以在顶级ggplot()中指定常见的美学参数,而不是在单个geoms中指定:

ggplot(data = agg_melt,
       aes(y = value, 
           x = Category, 
           label = percent(p),
           fill = variable)) + 
  geom_col(position = "fill") + 
  geom_text(position = position_fill(vjust = 0.5)) +
  scale_y_continuous(labels = percent) +
  labs(x = "Category", 
       y = "Percentage (%)")

plot