二元变量的散点图(ggplot)
Scatterplot of a binary variable (ggplot)
我需要一些帮助来尝试通过 ggplot 绘制散点图。在下面的数据集中,我想按会议年份在两个面板中查看 x-axis 上的女性百分比和 y 轴上的单位变量(参见图片以供参考 Scatter plot。
我尝试将数据集子集化为仅女性,然后尝试绘制图表,但我不确定该怎么做。
有人可以帮我吗?
谢谢!
structure(list(gender = c("Male", "Male", "Female", "Male", "Female",
"Female", "Male", "Female", "Female", "Unknown"), race_ethnicity = c("Latino or Hispanic American",
"Black, Afro-Caribbean, or African American", "Latino or Hispanic American",
"East Asian or Asian American", "Latino or Hispanic American",
"Non-Hispanic White or Euro-American", "Non-Hispanic White or Euro-American",
"Non-Hispanic White or Euro-American", "Non-Hispanic White or Euro-American",
"No Response"), year_of_birth = c("1979", "1976", "1981", "1977",
"1985", "No Response", "No Response", "1961", "1978", "No Response"
), primary_field = c("American Politics", "American Politics",
"American Politics", "American Politics", "American Politics",
"American Politics", "American Politics", "American Politics",
"International Politics", "No Response"), role_s = c("Chair Presenter Author",
"Discussant", "Author", "Author", "Author", "Discussant", "Chair",
"Discussant", "Author", "Author"), unit = c("Elections, Public Opinion, and Voting Behavior",
"Elections, Public Opinion, and Voting Behavior", "Elections, Public Opinion, and Voting Behavior",
"Elections, Public Opinion, and Voting Behavior", "Elections, Public Opinion, and Voting Behavior",
"Political Communication", "Political Communication", "Political Communication",
"Political Communication", "Political Communication"), conference_year = c(2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L
)), row.names = c(NA, 10L), class = "data.frame")
对于每一年和每一单元,您可以计算会议中女性的比例,并绘制每年不同方面的散点图。
library(dplyr)
library(ggplot2)
df %>%
group_by(conference_year, unit) %>%
summarise(percent_female = mean(gender == 'Female')) %>%
ggplot(aes(unit, percent_female)) +
geom_point() +
facet_wrap(~conference_year)
我需要一些帮助来尝试通过 ggplot 绘制散点图。在下面的数据集中,我想按会议年份在两个面板中查看 x-axis 上的女性百分比和 y 轴上的单位变量(参见图片以供参考 Scatter plot。
我尝试将数据集子集化为仅女性,然后尝试绘制图表,但我不确定该怎么做。
有人可以帮我吗?
谢谢!
structure(list(gender = c("Male", "Male", "Female", "Male", "Female",
"Female", "Male", "Female", "Female", "Unknown"), race_ethnicity = c("Latino or Hispanic American",
"Black, Afro-Caribbean, or African American", "Latino or Hispanic American",
"East Asian or Asian American", "Latino or Hispanic American",
"Non-Hispanic White or Euro-American", "Non-Hispanic White or Euro-American",
"Non-Hispanic White or Euro-American", "Non-Hispanic White or Euro-American",
"No Response"), year_of_birth = c("1979", "1976", "1981", "1977",
"1985", "No Response", "No Response", "1961", "1978", "No Response"
), primary_field = c("American Politics", "American Politics",
"American Politics", "American Politics", "American Politics",
"American Politics", "American Politics", "American Politics",
"International Politics", "No Response"), role_s = c("Chair Presenter Author",
"Discussant", "Author", "Author", "Author", "Discussant", "Chair",
"Discussant", "Author", "Author"), unit = c("Elections, Public Opinion, and Voting Behavior",
"Elections, Public Opinion, and Voting Behavior", "Elections, Public Opinion, and Voting Behavior",
"Elections, Public Opinion, and Voting Behavior", "Elections, Public Opinion, and Voting Behavior",
"Political Communication", "Political Communication", "Political Communication",
"Political Communication", "Political Communication"), conference_year = c(2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L
)), row.names = c(NA, 10L), class = "data.frame")
对于每一年和每一单元,您可以计算会议中女性的比例,并绘制每年不同方面的散点图。
library(dplyr)
library(ggplot2)
df %>%
group_by(conference_year, unit) %>%
summarise(percent_female = mean(gender == 'Female')) %>%
ggplot(aes(unit, percent_female)) +
geom_point() +
facet_wrap(~conference_year)