简体   繁体   English

多个虚拟变量的分组条形图

[英]Grouped barchart for multiple dummy variables

I am trying to create a bar chart that shows the mean damage corresponding to 0 and 1 values for multiple dummy variables, but I really have no clue where to start.我正在尝试创建一个条形图,显示对应于多个虚拟变量的 0 和 1 值的平均损伤,但我真的不知道从哪里开始。

这是我的数据的负责人

也许这个插图有助于理解

You can use the following code:您可以使用以下代码:

library(ggplot2)
ggplot(data = subset(stack(df), !is.na(values)), aes(x = ind, fill = as.factor(values))) +
  geom_bar(position = "dodge", na.rm = T) +
  labs(fill = "values")

Output:输出:

在此处输入图像描述

Data数据

df <- data.frame(d1 = c(0,0,0,0,0,0),
                 d2 = c(1,1,1,0,0,0),
                 d3 = c(1,0,1,0,0,1),
                 d4 = c(0,0,1,0,1,1),
                 d5 = c(NA, 0,1,0,1,NA),
                 d6 = c(0,1,1,0,1,NA),
                 d7 = c(1,1,1,NA,0,0),
                 d8 = c(1,1,1,0,1,0))  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM