将用于堆叠条形图ggplot的数据转换为可用于堆叠百分比图的数据的工作流程

时间:2019-03-01 19:44:20

标签: r ggplot2

我有以下数据集:

(df<-structure(list(age_group = structure(c(3L, 3L, 5L, 3L, 5L, 5L, 
5L, 3L, 5L, 5L, 4L, 4L, 4L, 3L, 5L), .Label = c("65+", "55-64", 
"45-54", "35-44", "25-34", "18-24"), class = "factor"), Gender = c("F", 
"M", "M", "M", "F", "M", "M", "M", "F", "M", "M", "F", "M", "F", 
"M")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-15L), .Names = c("age_group", "Gender")))
    # A tibble: 15 x 2
   age_group Gender
   <fct>     <chr> 
 1 45-54     F     
 2 45-54     M     
 3 25-34     M     
 4 45-54     M     
 5 25-34     F     
 6 25-34     M     
 7 25-34     M     
 8 45-54     M     
 9 25-34     F     
10 25-34     M     
11 35-44     M     
12 35-44     F     
13 35-44     M     
14 45-54     F     
15 25-34     M 

由此,我使用ggplot创建了以下堆叠式barplot:

enter image description here

我现在想制作一个堆积百分比图,如以下SO问题所示:Create stacked barplot where each stack is scaled to sum to 100%

准备我的数据以生成堆积百分比图的工作流程是什么?在我上面发布的SO问题中,数据有一个值的附加字段,而我没有。

1 个答案:

答案 0 :(得分:1)

SilentInstall silent