barplot():每组的频率百分比 [英] barplot(): Frequency percentages per group
问题描述
我正在尝试在y轴上创建频率为(%),在x轴上为 depression_meds
(N/Y)的分组条形图,并按另一个变量 score进行分组
(低/高).
I'm trying to make a grouped barplot with frequency (%) on the y-axis and depression_meds
(N/Y) on the x-axis, grouped by another variable score
(LOW/HIGH).
到目前为止,我的代码:
My code so far:
meds <- table(data2$depression_meds,data2$score)/sum(table(data2$score)) * 100
bp <- barplot(meds, beside=TRUE, axes=FALSE, xlab="Anti-depression meds use", names=c("No", "Yes"), col=c("azure3", "azure"), ylab="Frequency (%)", ylim=c(0,100))
axis(2, at=seq(0,100,10))
legend("topright", legend=c("LOW", "HIGH"), bty="n", fill=c("azure3", "azure"))
text(bp, 0, round(medtimerx, 1), cex=1, pos=3)
哪个很棒,并制作了以下barplot:
Which is great and makes the following barplot:
但是百分比使用的是n的总数243(下表的第三列),而不是每个分数组的n(下表的第一列和第二列),这是有道理的,因为这是我除法时所做的总和.但这不是我想要的.我一直在尝试获取每个乐谱组的频率,以使四个小节与下面的第一栏和第二栏相匹配,但是我已经没有足够的想法了.有人有什么建议吗?
But the percentages are using the total n of 243 (3rd column of the table below), not the n per score group (1st and 2nd columns in table below), which makes sense because that is what I do when I divide by the sum. But that's not what I want. I keep trying to get the frequencies per score group so that the four bars match the 1st and 2nd columns below, but I have run out of ideas. Does anyone have any suggestions?
Depression meds (0=N, 1=Y) LOW (N=99) HIGH (N=144) TOTAL (N=243)
0 96 (97.0%) 116 (80.6%) 212 (87.2%)
1 3 (3.0%) 28 (19.4%) 31 (12.8%)
这是我的数据:
> dput(data2)
structure(list(depression_meds = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L,
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 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L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L)), .Names = c("depression_meds", "score"), row.names = c(NA,
-243L), class = "data.frame")
感谢您的帮助,我的大脑不再工作了.
Thanks for your help, my brain isn't working any longer.
推荐答案
将其用作汇总表:
meds <- table(data2)
# score
#depression_meds 0 1
# 0 96 124
# 1 3 20
meds <- scale(meds, FALSE, colSums(meds)) * 100
# score
#depression_meds 0 1
# 0 96.969697 86.111111
# 1 3.030303 13.888889
无需更改其余代码:
bp <- barplot(meds, beside=TRUE, axes=FALSE, xlab="Anti-depression meds use", names=c("No", "Yes"), col=c("azure3", "azure"), ylab="Frequency (%)", ylim=c(0,100))
axis(2, at=seq(0,100,10))
legend("topright", legend=c("LOW", "HIGH"), bty="n", fill=c("azure3", "azure"))
text(bp, 0, round(meds, 1), cex=1, pos=3)
这篇关于barplot():每组的频率百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!