二元变量的散点图 (ggplot)

Posted

技术标签:

【中文标题】二元变量的散点图 (ggplot)【英文标题】:Scatterplot of a binary variable (ggplot) 【发布时间】:2021-11-12 18:42:34 【问题描述】:

我需要一些帮助来尝试通过 ggplot 绘制散点图。在下面的数据集中,我想按会议年份在两个面板中查看 x 轴上的女性百分比和 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")

【问题讨论】:

您提供的示例数据不清楚您的最终结果。 “单位”是分类变量,那么它是一个因变量(即 y 轴)吗? 【参考方案1】:

对于每个年份和单位,您可以计算会议中女性的比例,并在不同方面绘制每年的散点图。

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)

【讨论】:

谢谢!当我这样做时,我遇到了一个问题;它说'没有适用于'group_by'的方法应用于“函数”类的对象' df 替换为您的数据框的名称。我将您共享的dput 输出保存在df 中。

以上是关于二元变量的散点图 (ggplot)的主要内容,如果未能解决你的问题,请参考以下文章

R语言ggplot2可视化散点图实战:绘制基础散点图为所有散点添加标签只为大于阈值的散点添加标签

R语言使用ggplot2绘制带有边缘直方图的散点图实战

R语言ggplot2可视化:ggplot2可视化分组散点图并使用geom_smooth函数在散点图图中为不同的散点簇添加对应的回归曲线

带有 ggplot2 的散点图按 r 中的特定日期间隔着色

2018-10-31用R绘制散点图矩阵(成对的散点图)

R语言ggplot2可视化:ggplot2可视化分组散点图并使用geom_smooth函数在散点图图中为不同的散点簇添加对应的回归曲线并使用se参数设置拟合回归线的置信区间