绘制3个与前一列相关的分类变量
我有以下数据,我想使用r(ggplot2或任何其他包)
问题是:什么是最好的方法在R中可视化,以了解那些快乐=是,支付=否和捐赠=是(和其他组合)之间的关系。
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]
[可重复的代码]
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))))
我尝试了使用ggplot2 / plotluck的VennDiagram,SankeyDiagram,Bar和Mosiac Plot,但是我无法控制它们中的任何一个。 我尝试过使用reshape2,但我无法理解如何将这些数据点更改为计数矩阵,然后将其绘制。 我搜遍了全部。 请帮忙 - 我哪里错了?
如果它是关于幸福的数据集,那么为什么不用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))
更多关于UpSetR的小插曲。
尝试这个:
require(ggplot2)
ggplot(df, aes(WouldYouPay)) + geom_bar(aes(fill = WouldYouDonate)) +
facet_wrap(~AreYouHappy, labeller = "label_both")
链接地址: http://www.djcxy.com/p/24935.html
上一篇: Plot 3 categorical variables with dependencies on previous column