我有一个数据集,其中文章的名称按性别分隔。每篇文章都有一个索引号,所以它看起来像这样:
索引 | 姓名 | 性别 | 角色 |
---|---|---|---|
1 | A | M | 引用 |
1 | B | M | 引用 |
2 | C | M | 引用 |
2 | D | F | 引用 |
2 | A | F | 提到过 |
3 | E | M | 引用 |
所以所有索引2都是针对同一篇文章的,并且文章之间的名称可以重复。我试图了解每篇文章中引用/提及的人数与女性人数之间的关系。那么也许是散点图?为了表明文章中包含的人越多,女性的可能性就越大,反之亦然。
我试过这个:
data %>%
group_by(index) %>%
count(name) %>%
tally()
但是它生成了一个没有任何性别的数据集,那么我如何制作包含它的绘图?
当我这样做时:
data %>%
group_by(index, gender) %>%
count(index)
它在每个索引中分别生成 M 和 F,但我如何制作一个图来比较每个索引中的总名称与编号。女性?还用这种方法吗?
使用
scatterpie
库似乎是进行此比较的一个不错的选择。
read.table(text= "Index Name Gender Role
1 A M Quoted
1 B M Quoted
2 C M Quoted
2 D F Quoted
2 A F Mentioned
3 E M Quoted", header = T, stringsAsFactors = F) -> data
library(dplyr)
library(tidyr)
library(ggplot2)
library(scatterpie)
data %>%
count(Index, Gender) %>%
mutate(DummyY = 1) %>%
pivot_wider(names_from = Gender, values_from = n, values_fill = 0) -> test
ggplot() +
geom_scatterpie(aes(x = Index, y = DummyY, r = 0.45),
data = test, cols = c("F", "M")) +
coord_equal() +
scale_x_continuous(breaks = test$Index) +
scale_fill_discrete("Gender") +
theme(axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank())
创建于 2024-05-03,使用 reprex v2.0.2