按组计算真/假值的条形图(闪避图) [英] Barchart of count of true/false values by group (dodged graphs)
问题描述
以下是我的一部分数据:
> MDF
批次ABCDE
1个G FALSE TRUE FALSE TRUE FALSE
2 G FALSE FALSE FALSE TRUE FALSE
的3G FALSE TRUE FALSE FALSE FALSE
4个G FALSE FALSE FALSE TRUE FALSE
5个G FALSE FALSE TRUE TRUE TRUE
6G的FALSE FALSE TRUE TRUE TRUE
7个G FALSE FALSE FALSE FALSE TRUE
8个G的FALSE FALSE TRUE TRUE TRUE
9分配ģFALSE FALSE FALSE TRUE FALSE
10个G FALSE FALSE FALSE TRUE TRUE
图11G FALSE FALSE FALSE FALSE TRUE
将12g FALSE FALSE FALSE TRUE FALSE
将13g FALSE FALSE FALSE TRUE FALSE
地下14 FALSE FALSE FALSE TRUE FALSE
15政FALSE FALSE FALSE TRUE FALSE
地下16 FALSE FALSE FALSE TRUE FALSE
17克FALSE FALSE TRUE TRUE FALSE
18G的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和其他列(AE)都是布尔/逻辑值。
我可以创建一个图表,它可以计算列B中的TRUE值,如下所示:
是的,您可能需要先重新整理数据,然后才能使用 position =dodge
为每个键
绘制一个栏。 tidyr
:
library(tidyr)
pre>
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)
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
where Batch can be any of A, B, G, R, S, and the other columns (A-E) are all boolean/logical values.
I was able to create a graph which counts the TRUE values in column B as follows:
using:
ggplot(data = mdf, aes(x = Batch, y = as.numeric(B), fill = Batch)) + stat_summary(fun.y = sum, geom = "bar")
Similarly, I can easily create 4 more graphs for the other columns (A, C, D, E).
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?
Update: Here is what I was looking for (created after using the suggestion from @mtoto)
解决方案Yes it's possible, you just need to reshape your data first, then you can use
position = "dodge"
to draw a bar for eachkey
. Withtidyr
: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屋!