male FALSE TRUE
0 50.0% 66.7%
1 50.0% 33.3%
structure(list(male = 0:1, `FALSE` = c("50.0%", "50.0%"), `TRUE` = c("66.7%",
"33.3%")), row.names = c(NA, -2L), core = structure(list(male = 0:1,
`FALSE` = c(1, 1), `TRUE` = c(4, 2)), class = "data.frame", row.names = c(NA,
-2L)), tabyl_type = "two_way", var_names = list(row = "male",
col = "dummy"), class = c("tabyl", "data.frame"))
如何使用由看門人構建的這張表的 ggplot2 制作情節?問題是我想要兩個并排的圖:一個用于 dummy=TRUE ,另一個用于 dummy=FALSE (但更改標簽以使 TRUE 被 a 替換,FALSE 被 b 替換——我遇到了困難這是因為 TRUE 和 FALSE 是合乎邏輯的)。我還想分別替換 c 和 d 的值 0 和 1。任何幫助都深表感謝。
uj5u.com熱心網友回復:
你可以試試tidyverse。訣竅是將資料從寬轉換為長,因為這是ggplot. 這里我使用了pivot_longer,但你也可以使用reshapeor melt。
library(tidyverse)
df %>%
pivot_longer(-1) %>%
mutate(name = ifelse(name, "a", "b")) %>%
ggplot( aes(factor(male), value, fill =name))
geom_col(position = position_dodge())

使用 baseR你可以試試
# transform percentages to numerics
df$a <- as.numeric(gsub("%", "", df$`TRUE`))
df$b <- as.numeric(gsub("%", "", df$`FALSE`))
barplot(cbind(a, b) ~ male, df, beside=T,legend.text = TRUE)

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/427640.html
下一篇:如何用誤差條調整標簽距離?
