Plot 3 categorical variables with dependencies on previous column
I have the following data that i want to plot using r (ggplot2 or any other package)
QUESTION IS: What is the best method to visualize in R, to understand the relationship between those who are Happy = Yes, Pay = No and Donate = Yes (and other combinations).
AreYouHappy | WouldYouPay | WouldYouDonate
Yes | No | Yes
Yes | No | No
Yes | No | Yes
Yes | Yes | Yes
Yes | No | No
No | Yes | Yes
Yes | No | No
Yes | No | Yes
...[58 data points]
[Reproducible code]
df <- data.frame(
cbind(AreYouHappy = sample( c("Yes", "No"), 58, replace = TRUE, prob = c(.75,.25)),
WouldYouPay = sample( c("Yes", "No"), 58, replace = TRUE, prob = c(.25,.75)),
WouldYouDonate = sample( c("Yes", "No"), 58, replace = TRUE, prob = c(.50,.50))))
I tried VennDiagram, SankeyDiagram, Bar, Mosiac Plot, using ggplot2/plotluck but I can't get a handle on any of them. I've tried using reshape2, but i can't understand how to change these data point into a matrix of count and then plot it. I searched all over. Please do help-- where am I going wrong?
If it's dataset concerning happiness then why not to use upset
:-)
# Data
df <- data.frame( cbind(AreYouHappy = sample( c("Yes", "No"), 58, replace = TRUE, prob = c(.75,.25)), WouldYouPay = sample( c("Yes", "No"), 58, replace = TRUE, prob = c(.25,.75)), WouldYouDonate = sample( c("Yes", "No"), 58, replace = TRUE, prob = c(.50,.50))))
# Transform original data into 0 and 1
df01 <- apply(df, 2, function(x) ifelse(x == "Yes", 1, 0))
# Plot using UpSetR
library(UpSetR)
upset(data.frame(df01))
More about UpSetR in the vignette.
尝试这个:
require(ggplot2)
ggplot(df, aes(WouldYouPay)) + geom_bar(aes(fill = WouldYouDonate)) +
facet_wrap(~AreYouHappy, labeller = "label_both")
链接地址: http://www.djcxy.com/p/24936.html
上一篇: R因子的算术运算
下一篇: 绘制3个与前一列相关的分类变量