按组划分的真/假值计数条形图(斜线图) [英] Barchart of count of true/false values by group (dodged graphs)

查看:11
本文介绍了按组划分的真/假值计数条形图(斜线图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 R 和 ggplot 比较陌生.而且我不确定我想要的是否可行.

I am rather new in R and ggplot. And I am not sure if what I want is doable.

这是我的(部分)数据:

Here is (a portion of) my data:

> mdf
   Batch     A     B     C     D     E
1      G FALSE  TRUE FALSE  TRUE FALSE
2      G FALSE FALSE FALSE  TRUE FALSE
3      G FALSE  TRUE FALSE FALSE FALSE
4      G FALSE FALSE FALSE  TRUE FALSE
5      G FALSE FALSE  TRUE  TRUE  TRUE
6      G FALSE FALSE  TRUE  TRUE  TRUE
7      G FALSE FALSE FALSE FALSE  TRUE
8      G FALSE FALSE  TRUE  TRUE  TRUE
9      G FALSE FALSE FALSE  TRUE FALSE
10     G FALSE FALSE FALSE  TRUE  TRUE
11     G FALSE FALSE FALSE FALSE  TRUE
12     G FALSE FALSE FALSE  TRUE FALSE
13     G FALSE FALSE FALSE  TRUE FALSE
14     G FALSE FALSE FALSE  TRUE FALSE
15     G FALSE FALSE FALSE  TRUE FALSE
16     G FALSE FALSE FALSE  TRUE FALSE
17     G FALSE FALSE  TRUE  TRUE FALSE
18     G FALSE FALSE  TRUE  TRUE  TRUE
19     A FALSE FALSE FALSE  TRUE  TRUE
20     A FALSE FALSE FALSE  TRUE  TRUE

其中 Batch 可以是 A、B、G、R、S 中的任何一个,其他列 (A-E) 都是布尔值/逻辑值.

where Batch can be any of A, B, G, R, S, and the other columns (A-E) are all boolean/logical values.

我能够创建一个图表来计算 B 列中的 TRUE 值,如下所示:

I was able to create a graph which counts the TRUE values in column B as follows:

使用:

ggplot(data = mdf, aes(x = Batch, y = as.numeric(B), fill = Batch))
    + stat_summary(fun.y = sum, geom = "bar")

同样,我可以轻松地为其他列(A、C、D、E)创建另外 4 个图表.

Similarly, I can easily create 4 more graphs for the other columns (A, C, D, E).

但是,是否可以将这 5 个图形合并"在一个图形中?换句话说,我想要一个图表,其中我将有 5 个组(如上图所示)用于 'Batch' 的 5 个值,并且在每个组中,我需要一个单独的条形图,其中包含 5 列中每一列的计数(AE).这可行吗?

But, is it possible to 'merge' these 5 graphs in a single graph? In other words, I would like a graph where I would have 5 groups (as in the above graph) for the 5 values of 'Batch', and in each group I would need a separate bar with the count of each of the 5 columns (A-E). Is this doable?

更新:这是我正在寻找的(使用@mtoto 的建议后创建的)

Update: Here is what I was looking for (created after using the suggestion from @mtoto)

推荐答案

是可以的,你只需要先对数据进行整形,然后你就可以使用 position = "dodge" 来绘制一个条形对于每个 key.使用 tidyr:

Yes it's possible, you just need to reshape your data first, then you can use position = "dodge" to draw a bar for each key. With tidyr:

library(tidyr)
library(dplyr)
library(ggplot2)

mdf %>% gather(key, value, -Batch) %>%
  ggplot(.,(aes(Batch, as.numeric(value), fill = key))) +
  stat_summary(fun.y = sum, geom = "bar", position = "dodge")

这篇关于按组划分的真/假值计数条形图(斜线图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆