R:2组的ggplot条形图
R: ggplot barplot for 2 groups
> dput(df_male)
structure(list(Question = structure(c(1L, 2L, 3L, 1L, 2L, 3L), .Label = c("Q1",
"Q2", "Q3", "Q4", "Q5", "Q6", "Q7", "Q8"), class = "factor"),
Response = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("No",
"Yes"), class = "factor"), Proportion = c(0.569230769230769,
0.569230769230769, 0.492307692307692, 0.430769230769231,
0.430769230769231, 0.507692307692308)), .Names = c("Question",
"Response", "Proportion"), row.names = c(1L, 2L, 3L, 9L, 10L,
11L), class = "data.frame")
> dput(df_female)
structure(list(Question = structure(c(1L, 2L, 3L, 1L, 2L, 3L), .Label = c("Q1",
"Q2", "Q3", "Q4", "Q5", "Q6", "Q7", "Q8"), class = "factor"),
Response = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("No",
"Yes"), class = "factor"), Proportion = c(0.603092783505155,
0.65979381443299, 0.54639175257732, 0.396907216494845, 0.34020618556701,
0.45360824742268)), .Names = c("Question", "Response", "Proportion"
), row.names = c(1L, 2L, 3L, 9L, 10L, 11L), class = "data.frame")
我有 2 个数据框(每个性别一个)关于他们对 3 个问题的回答比例。
> df_male
Question Response Proportion
1 Q1 No 0.5692308
2 Q2 No 0.5692308
3 Q3 No 0.4923077
9 Q1 Yes 0.4307692
10 Q2 Yes 0.4307692
11 Q3 Yes 0.5076923
> df_female
Question Response Proportion
1 Q1 No 0.6030928
2 Q2 No 0.6597938
3 Q3 No 0.5463918
9 Q1 Yes 0.3969072
10 Q2 Yes 0.3402062
11 Q3 Yes 0.4536082
我想将其可视化,所以我使用了 ggplot 分段条形图。
ggplot(df_male, aes(x = Question, y = Proportion)) +
geom_bar(aes(fill = Response), stat = "identity") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.25)) + # Rotate tick mark labels
guides(fill = guide_legend(reverse = TRUE)) + ggtitle("Male") + theme(plot.title = element_text(hjust=0.5))
ggplot(df_female, aes(x = Question, y = Proportion)) +
geom_bar(aes(fill = Response), stat = "identity") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.25)) + # Rotate tick mark labels
guides(fill = guide_legend(reverse = TRUE)) + ggtitle("Female") + theme(plot.title = element_text(hjust=0.5))
这是将 2 个地块合并为 1 个地块的方法吗?即我想找到一种方法来可视化同一图中两个不同组的这 3 个问题的比例。
将数据堆叠在一起并使用 gender
和 question
之间的 interaction
作为 x 轴如何?
df <- rbind(cbind(df_male,gender="M"), cbind(df_female, gender = "F"))
ggplot(df, aes(x = interaction(Question,gender), y = Proportion)) +
geom_bar(aes(fill = Response), stat = "identity") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.25)) + # Rotate tick mark labels
guides(fill = guide_legend(reverse = TRUE)) + ggtitle("Male vs. Female") + theme(plot.title = element_text(hjust=0.5))
或者,我认为更好的解决方案是使用 facet_wrap()
按问题分组(如果需要,也可以按性别分组):
ggplot(df, aes(x =gender, y = Proportion)) +
geom_bar(aes(fill = Response), stat = "identity") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.25)) + # Rotate tick mark labels
guides(fill = guide_legend(reverse = TRUE)) + ggtitle("Male vs. Female") + theme(plot.title = element_text(hjust=0.5)) +
facet_wrap(~Question)
将您的数据与识别变量和方面结合起来:
library(tidyverse)
bind_rows('male' = df_male, 'female' = df_female, .id = 'gender') %>%
ggplot(aes(x = Question, y = Proportion, fill = Response)) +
geom_col() +
facet_wrap(~gender)
随意编辑外观。如果您更喜欢垂直布局,请在 facet_wrap
中设置 ncol = 1
。
> dput(df_male)
structure(list(Question = structure(c(1L, 2L, 3L, 1L, 2L, 3L), .Label = c("Q1",
"Q2", "Q3", "Q4", "Q5", "Q6", "Q7", "Q8"), class = "factor"),
Response = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("No",
"Yes"), class = "factor"), Proportion = c(0.569230769230769,
0.569230769230769, 0.492307692307692, 0.430769230769231,
0.430769230769231, 0.507692307692308)), .Names = c("Question",
"Response", "Proportion"), row.names = c(1L, 2L, 3L, 9L, 10L,
11L), class = "data.frame")
> dput(df_female)
structure(list(Question = structure(c(1L, 2L, 3L, 1L, 2L, 3L), .Label = c("Q1",
"Q2", "Q3", "Q4", "Q5", "Q6", "Q7", "Q8"), class = "factor"),
Response = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("No",
"Yes"), class = "factor"), Proportion = c(0.603092783505155,
0.65979381443299, 0.54639175257732, 0.396907216494845, 0.34020618556701,
0.45360824742268)), .Names = c("Question", "Response", "Proportion"
), row.names = c(1L, 2L, 3L, 9L, 10L, 11L), class = "data.frame")
我有 2 个数据框(每个性别一个)关于他们对 3 个问题的回答比例。
> df_male
Question Response Proportion
1 Q1 No 0.5692308
2 Q2 No 0.5692308
3 Q3 No 0.4923077
9 Q1 Yes 0.4307692
10 Q2 Yes 0.4307692
11 Q3 Yes 0.5076923
> df_female
Question Response Proportion
1 Q1 No 0.6030928
2 Q2 No 0.6597938
3 Q3 No 0.5463918
9 Q1 Yes 0.3969072
10 Q2 Yes 0.3402062
11 Q3 Yes 0.4536082
我想将其可视化,所以我使用了 ggplot 分段条形图。
ggplot(df_male, aes(x = Question, y = Proportion)) +
geom_bar(aes(fill = Response), stat = "identity") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.25)) + # Rotate tick mark labels
guides(fill = guide_legend(reverse = TRUE)) + ggtitle("Male") + theme(plot.title = element_text(hjust=0.5))
ggplot(df_female, aes(x = Question, y = Proportion)) +
geom_bar(aes(fill = Response), stat = "identity") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.25)) + # Rotate tick mark labels
guides(fill = guide_legend(reverse = TRUE)) + ggtitle("Female") + theme(plot.title = element_text(hjust=0.5))
这是将 2 个地块合并为 1 个地块的方法吗?即我想找到一种方法来可视化同一图中两个不同组的这 3 个问题的比例。
将数据堆叠在一起并使用 gender
和 question
之间的 interaction
作为 x 轴如何?
df <- rbind(cbind(df_male,gender="M"), cbind(df_female, gender = "F"))
ggplot(df, aes(x = interaction(Question,gender), y = Proportion)) +
geom_bar(aes(fill = Response), stat = "identity") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.25)) + # Rotate tick mark labels
guides(fill = guide_legend(reverse = TRUE)) + ggtitle("Male vs. Female") + theme(plot.title = element_text(hjust=0.5))
或者,我认为更好的解决方案是使用 facet_wrap()
按问题分组(如果需要,也可以按性别分组):
ggplot(df, aes(x =gender, y = Proportion)) +
geom_bar(aes(fill = Response), stat = "identity") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.25)) + # Rotate tick mark labels
guides(fill = guide_legend(reverse = TRUE)) + ggtitle("Male vs. Female") + theme(plot.title = element_text(hjust=0.5)) +
facet_wrap(~Question)
将您的数据与识别变量和方面结合起来:
library(tidyverse)
bind_rows('male' = df_male, 'female' = df_female, .id = 'gender') %>%
ggplot(aes(x = Question, y = Proportion, fill = Response)) +
geom_col() +
facet_wrap(~gender)
随意编辑外观。如果您更喜欢垂直布局,请在 facet_wrap
中设置 ncol = 1
。