我正在尝试绘制两个因子变量,并在图中使用%标记结果。
我已经检查了此帖子以及他/她提供的链接:
How to center stacked percent barchart labels
您在此处使用的ggplot行实际上来自推荐的其中一篇帖子:
sex <- c("F","F","M", "M", "M", "F","M","F","F", "M", "M", "M", "M","F","F", "M", "M", "F")
behavior <- c("A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C", "B", "C", "A")
BehSex <- data.frame(sex, behavior)
ggplot(BehSex, aes(x= factor(sex), fill= factor(behavior), y = (..count..)/sum(..count..)))+
geom_bar() +
stat_bin(geom = "text",
aes(label = paste(round((..count..)/sum(..count..)*100), "%")),
vjust = 5)
但是,当我使用该行时,出现以下错误:
错误:StatBin需要连续的x变量:x变量为 离散的。也许您想要stat =“ count”?
我尝试在geom_bar()中使用stat =“ count”,但它似乎无法正常工作。
三个问题:
1)我做错了什么?
2)如何管理我想要的东西?
3)如何绘制:%,然后在另一张图中计数?
这是我现在拥有的情节
提前感谢您的帮助!
答案 0 :(得分:2)
这是另一种使用带有dplyr
的数据准备的方法:
编辑:增加计数。要显示一个或另一个,只需更改标签即可。
library(dplyr)
BehSexSum <- BehSex %>%
count(sex, behavior) %>%
mutate(pct = n / sum(n),
pct_label = scales::percent(pct))
ggplot(BehSexSum, aes(x= sex, fill = behavior, y = pct)) +
geom_col() +
geom_text(aes(label = paste(pct_label, n, sep = "\n")),
lineheight = 0.8,
position = position_stack(vjust = 0.5)) +
scale_y_continuous(labels = scales::percent)
答案 1 :(得分:1)
我认为使用scale_y_continuous(labels = scales::percent)
而不是stat_bin(...)
可以更容易地将y轴标签格式化为百分比格式。因此,代码可以保持几乎相同。
ggplot(BehSex, aes(x= factor(sex), fill= factor(behavior), y =(..count..)/sum(..count..)))+
geom_bar() +
#Set the y axis format as percentage
scale_y_continuous(labels = scales::percent)+
#Change the legend and axes names
labs(x = "Sex", y = "Percentage",fill = "Behavior")
答案 2 :(得分:1)
关于您提到的帖子的答案,您将必须使用position = position_stack()
显示百分比。
此外,您可以使用dplyr
包从数据框中获取百分比。我认为,这样可以更轻松地显示标签:
library(dplyr)
df <- BehSex %>% group_by(sex) %>% count(behavior) %>% mutate(Percent = n / sum(n)*100)
# A tibble: 6 x 4
# Groups: sex [2]
sex behavior n Percent
<fct> <fct> <int> <dbl>
1 F A 2 25
2 F B 3 37.5
3 F C 3 37.5
4 M A 4 40
5 M B 3 30
6 M C 3 30
然后,您可以像这样获得情节:
ggplot(df, aes(x = sex, y = Percent, fill = behavior))+
geom_bar(stat = "identity")+
geom_text(aes(label = paste(Percent,"%"), y = Percent),
position = position_stack(vjust = 0.5))+
coord_flip()+
labs(x = "Sex", y = "Percentage",fill = "Behavior")