ggplot2 使用 ...prop... 和按另一个类别对条形图进行分组的问题
ggplot2 problems with using ...prop... and grouping bar graph by another category
StudentData <- data.frame(gender = sample( c("male","female"), 100, replace=TRUE),
degree = sample( c("Associates", "Masters", "PhD"), 100, replace=TRUE),
category = sample( c("Audit", "Credit"), 100, replace=TRUE))
在以下数据集中,我试图创建一个条形图,绘制具有副学士、硕士或博士学位的样本百分比,按性别分开(使用 facet_grid() 完成) .这是我到目前为止生成的:
StudentData %>% ggplot(., aes(x=degree, group=gender)) +
geom_bar(aes(y=..prop..), stat="count", position=position_dodge()) +
geom_text(aes(label=scales::percent(round(..prop..,2)),
y=..prop..), stat="count", vjust=-.5) +
scale_y_continuous(limits=c(0,1),labels = scales::percent) +
ylab("Percent of Sample") +
facet_grid(~gender)
不过,我还想在每个图表上以条形并排显示组 "Audit" 和 "Credit" 之间的差异。然而,当我将 "fill=category" 添加到 ggplot 的美学中时,没有任何变化:
StudentData %>% ggplot(., aes(x=degree, group=gender, fill=category)) +
geom_bar(aes(y=..prop..), stat="count", position=position_dodge()) +
geom_text(aes(label=scales::percent(round(..prop..,2)),
y=..prop..), stat="count", vjust=-.5) +
scale_y_continuous(limits=c(0,1),labels = scales::percent) +
ylab("Percent of Sample") +
facet_grid(~gender)
我意识到这通常是使用 geom_bar(stat="identity", position=position_dodge())
完成的,但是当我更改 stat="identity"
时,会出现以下错误消息:
Error in FUN(X[[i]], ...) : object 'prop' not found
知道如何制作分面图、使用特殊字符(例如 ..prop..)并向 ggplot2 图表添加另一个填充吗?
您可以像这样创建必要的四个 group
(而不是两个):
StudentData %>%
ggplot(., aes(x=degree, group=interaction(gender, category), fill=category)) +
geom_bar(aes(y=..prop..), stat="count", position=position_dodge()) +
geom_text(aes(label=scales::percent(round(..prop..,2)),
y=..prop..), stat="count", vjust=-.5, position=position_dodge(.9)) +
scale_y_continuous(limits=c(0,1),labels = scales::percent) +
ylab("Percent of Sample") +
facet_grid(~gender)
StudentData <- data.frame(gender = sample( c("male","female"), 100, replace=TRUE),
degree = sample( c("Associates", "Masters", "PhD"), 100, replace=TRUE),
category = sample( c("Audit", "Credit"), 100, replace=TRUE))
在以下数据集中,我试图创建一个条形图,绘制具有副学士、硕士或博士学位的样本百分比,按性别分开(使用 facet_grid() 完成) .这是我到目前为止生成的:
StudentData %>% ggplot(., aes(x=degree, group=gender)) +
geom_bar(aes(y=..prop..), stat="count", position=position_dodge()) +
geom_text(aes(label=scales::percent(round(..prop..,2)),
y=..prop..), stat="count", vjust=-.5) +
scale_y_continuous(limits=c(0,1),labels = scales::percent) +
ylab("Percent of Sample") +
facet_grid(~gender)
不过,我还想在每个图表上以条形并排显示组 "Audit" 和 "Credit" 之间的差异。然而,当我将 "fill=category" 添加到 ggplot 的美学中时,没有任何变化:
StudentData %>% ggplot(., aes(x=degree, group=gender, fill=category)) +
geom_bar(aes(y=..prop..), stat="count", position=position_dodge()) +
geom_text(aes(label=scales::percent(round(..prop..,2)),
y=..prop..), stat="count", vjust=-.5) +
scale_y_continuous(limits=c(0,1),labels = scales::percent) +
ylab("Percent of Sample") +
facet_grid(~gender)
我意识到这通常是使用 geom_bar(stat="identity", position=position_dodge())
完成的,但是当我更改 stat="identity"
时,会出现以下错误消息:
Error in FUN(X[[i]], ...) : object 'prop' not found
知道如何制作分面图、使用特殊字符(例如 ..prop..)并向 ggplot2 图表添加另一个填充吗?
您可以像这样创建必要的四个 group
(而不是两个):
StudentData %>%
ggplot(., aes(x=degree, group=interaction(gender, category), fill=category)) +
geom_bar(aes(y=..prop..), stat="count", position=position_dodge()) +
geom_text(aes(label=scales::percent(round(..prop..,2)),
y=..prop..), stat="count", vjust=-.5, position=position_dodge(.9)) +
scale_y_continuous(limits=c(0,1),labels = scales::percent) +
ylab("Percent of Sample") +
facet_grid(~gender)