我有一个性别变量和10个不同的治疗变量。我在ggplot中使用了geom_histogram。因此,对于每种治疗,我都希望看到男性和女性的百分比,但都已标准化,因此它们的y轴最大值可以为1,因此可以按百分比进行比较。
尤其是我对生成这样的图形感兴趣
https://cengel.github.io/R-data-viz/R-data-viz_files/figure-html/color-bar-gender-1.png
一些数据想要这样的东西
structure(list(Treatment = structure(c(3L, 3L, 3L, 3L, 3L, 4L
), .Label = c("", "{\"ImportId\":\"Treatment\"}", "Altruism",
"Altruism - White", "Piece Rate - 0 cents", "Piece Rate - 3 cents",
"Piece Rate - 6 cents", "Piece Rate - 9 cents", "Reciprocity",
"Reciprocity - Black", "Reciprocity - White", "Treatment"), class = "factor"),
Gender = structure(c(5L, 3L, 5L, 5L, 5L, 3L), .Label = c("",
"{\"ImportId\":\"QID2\"}", "Female", "Gender you most closely identify with: - Selected Choice",
"Male", "Other", "Prefer not to answer"), class = "factor"),class="data.frame")
ggplot(Data1, aes(x=Treatment, fill=Gender))+
geom_histogram(bins = 15, col="black",stat="count")+
ggtitle("Gender")+
xlab("Treatment")+ylab("Density")+
theme_classic()+
theme(axis.line = element_blank(),
axis.ticks = element_blank())
我得到这样的东西
到目前为止,这是我的代码,可以正常工作。我唯一不知道怎么做的是让我所有的条形图都具有相同的高度(1,代表标准值),这样我就可以看到每种处理的百分比(分隔条形图)。
答案 0 :(得分:2)
让我们生成一些示例数据:
library(dplyr)
library(ggplot2)
set.seed(1001)
Data1 <- data.frame(Treatment = sample(LETTERS[1:5], 100, replace = TRUE),
Gender = sample(c("Male", "Female"), 100, replace = TRUE))
现在我们可以使用dplyr::count
,关键是使用position = "fill"
:
Data1 %>%
count(Treatment, Gender) %>%
ggplot(aes(Treatment, n)) +
geom_col(aes(fill = Gender), position = "fill")