【问题标题】:Scatterplot of a binary variable (ggplot)二元变量的散点图 (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 轴)吗?

标签: r ggplot2 scatter-plot


【解决方案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 中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-30
  • 2020-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-01
  • 1970-01-01
相关资源
最近更新 更多