我有一個看起來像這樣的資料集:
english math science history art geography
<fct> <fct> <fct> <fct> <fct> <fct>
1 1 1 0 1 1 0
2 0 0 0 1 0 1
3 1 0 1 0 0 1
4 0 1 0 1 1 0
5 1 1 0 0 0 0
6 1 1 1 0 1 1
7 1 1 0 0 1 1
8 1 1 0 0 0 1
9 0 0 0 1 0 0
10 1 0 1 1 1 0
11 1 0 0 1 1 0
我試圖計算整個資料框中出現兩個變數的實體,例如:數學和英語的 5 個實體的值都為 1。
我可以使用此代碼計算所有實體:,并且可以為所有主題執行此操作
sum(df$english==1 & df$math==1)
但是,我正在嘗試創建一個看起來像這個圖的圖;這可以在 R 中做嗎?我曾嘗試使用 ggplot 但不確定如何創建它?
資料框的代碼是這樣的:
structure(list(english = structure(c(2L, 1L, 2L, 1L, 2L, 2L,
2L, 2L, 1L, 2L, 2L), .Label = c("0", "1"), class = "factor"),
math = structure(c(2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L,
1L), .Label = c("0", "1"), class = "factor"), science = structure(c(1L,
1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L), .Label = c("0",
"1"), class = "factor"), history = structure(c(2L, 2L, 1L,
2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("0", "1"), class = "factor"),
art = structure(c(2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L,
2L), .Label = c("0", "1"), class = "factor"), geography = structure(c(1L,
2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L), .Label = c("0",
"1"), class = "factor")), row.names = c(NA, -11L), class = c("tbl_df",
"tbl", "data.frame"))
uj5u.com熱心網友回復:
實作所需結果的一種選擇是通過widyr包,它可以輕松計算計數,widyr::pairwise_count并以整潔的資料格式回傳結果,可以通過以下方式輕松繪制ggplot2:
- 為觀察添加識別符號變數
- 使用例如將您的資料幀轉換為長格式或整潔格式
tidyr::pivot_longer - 過濾資料并計算計數
- 陰謀
library(widyr)
library(dplyr)
library(tidyr)
library(ggplot2)
dd <- d %>%
mutate(id = row_number()) %>%
pivot_longer(-id) %>%
filter(value == 1) %>%
pairwise_count(name, id)
ggplot(dd, aes(item1, item2))
geom_point(aes(size = n), color = "steelblue")
geom_text(aes(label = n), show.legend = FALSE)
scale_size_area(max_size = 10)
guides(size = "none")

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/343061.html
