简体   繁体   English

带有聚合数据的堆积条形图(ggplot2)

[英]stacked barplot with aggregated data (ggplot2)

I have some major problems with ggplot2. 我对ggplot2有一些重大问题。 Even though it might be a very easy question to you I couldnt manage yet to get it right (I have read a ggplot2-book and was looking on stackoverflow as well). 即使这对你来说可能是一个非常简单的问题,但我还是无法管理它(我已经阅读了ggplot2-book并且正在查看stackoverflow)。

Orginally there was a dataset consisting of a factor variable (country) and a dichotomous variable. 或者,有一个由因子变量(国家)和二分变量组成的数据集。 Unfortunately I dont have my data in this extended format myself: I have two variables "var1" and "var2". 不幸的是我自己没有这种扩展格式的数据:我有两个变量“var1”和“var2”。 var1 gives the number of cases in the original dataset where a certain condition is true and var2 gives the number of cases where the same condition is false: var1给出原始数据集中某个条件为true的个案数,var2给出相同条件为false的个案数:

country | var1 | var2
----------------------
"A" | 20 | 30
"B" | 24 | 53
"C" | 21 | 24
"D" | 30 | 66

Now I'd like to produce a stacked barplot with a y-axis showing percentages within each country (all bars should have the same height) plus the absolute numbers displayed within the bars : 现在我想制作一个叠加的条形图 ,其中y轴显示每个国家/地区的百分比 (所有条形应具有相同的高度)加上条形图中显示绝对数字

How it should look 应该怎么样

I found out that if the data were in an extended format, I could use 我发现如果数据是扩展格式,我可以使用

ggplot(data=dataset)+geom_bar(aes(x=country, fill=variable), position='fill')

However, I only have aggregated data. 但是,我只有汇总的数据。

Could anyone please help me? 有人可以帮帮我吗?

Thank you! 谢谢!

A quick solution by first reshaping and then plotting: 通过首先重塑然后绘制的快速解决方案:

library(ggplot2)
library(tidyr)

temp2 <- gather(temp, var, val, -country)
ggplot(temp2, aes(country, val, fill = var)) + 
  geom_col(position = 'fill') +
  geom_text(aes(label = val), position = position_fill(vjust = 0.5)) +
  scale_y_continuous(labels = scales::percent_format())

在此输入图像描述

library('ggplot2')
library('data.table')

df1 <- fread('country var1 var2
             "A" 20 30
             "B" 24 53
             "C" 21 24
             "D" 30 66')

df1 <- melt(df1, id.vars = 'country', )
df1[, percent := prop.table(value)*100, by = 'country']

ggplot(data = df1, aes( x = country, y = percent, fill = variable, label = value )) + 
  geom_bar(stat = 'identity') + 
  geom_text(size = 5, position = position_stack(vjust = 0.5))

在此输入图像描述

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

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