r - 在条形图中为分类变量添加计数百分比;ggplots
问题描述
我需要有人帮我编辑我的代码以添加计数百分比作为列标签
我有代码
scandals_deal_breaker <- data[, c("Q19.A: Sexual harassment",
"Q19.B: Racism",
"Q19.C: Violence",
"Q19.D: Fraud and money laundering",
"Q19.E: Alcohol and drug use")]
scandals_deal_breaker_count1 <- ldply(scandals_deal_breaker,
function(c) sum(c=="1")/52)
ggplot(scandals_deal_breaker_count1, aes(x=reorder(.id, -V1), y=V1)) +
geom_bar(stat="identity") +
xlab("") + ylab("") +
scale_x_discrete(breaks = c("Q19.A: Sexual harassment",
"Q19.B: Racism",
"Q19.C: Violence",
"Q19.D: Fraud and money laundering",
"Q19.E: Alcohol and drug use"),
labels = str_wrap(c("Sexual harassment","Racism","Violence",
"Fraud and money laundering",
"Alcohol and drug use"), width = 14)) +
scale_y_continuous(labels = percent) +
ggtitle("What types of scandals are \"deal-breakers\" for you?") +
theme(plot.title = element_text(hjust = 0.5 , size = 8.5))
这是一道选择题,百分比的总和应超过 100%。我希望添加每列的百分比。
我尝试添加以下代码,但它为每个选项添加了 20% 的标签
geom_text(
stat='count',
aes(y=after_stat(..count..),
label=after_stat(scales::percent(..count../sum(..count..),1))),
position=position_fill(0.5),
)
解决方案
推荐阅读
- lean - 从终端运行精益的自动证明检查程序
- chart.js - 如何摆脱chartjs工具提示中的白色方形轮廓或边框?
- c# - ASP.NET MVC 应用程序,HTML 视频不起作用
- python - 创建一个链表函数而不创建另一个类
- java - 找到单词后如何从循环中中断
- python - 我该如何解决这个问题?追溯:文件“C:/Users/rawaa/PycharmProjects/huffman/deempade.py”,第 56 行,seed=pickle.load(fp) EOFError
- javascript - 如何通过对象名称动态检索对象属性?
- c++ - Clang-Format:多行语句后在换行符上打开大括号
- loopback - Loopback 4:调试 SQL 语句
- assembly - 汇编语言 addw vs addw vs