简体   繁体   English

ggplot boxplot具有条件的多个列

[英]ggplot boxplot multiple columns with a factor with condition

Example data frame: 数据框示例:

a <- c(1, 0, 1)
b <- c(0, 1, 0)
c <- c(1, 0, 1)
total <- c(100,200,300)
my.data <- data.frame(a, b, c, total)

> my.data
  a b c total
1 1 0 1   100
2 0 1 1   200
3 1 0 1   300

I would like to create one single boxplot to show the distribution of "total" for each column: a, b, c but only consider those with value = 1. Example: Column a's row 2 is ignore because it is 0, so column a has a distribution of 100 and 300. Column B has a distribution of 200 and column c has a distribution of 100,200,300. 我想创建一个单一的箱形图来显示每一列的“总计”分布:a,b,c,但仅考虑值= 1的那些。示例:忽略列a的第2行,因为它是0,所以列a具有100和300的分布。列B具有200的分布,列c具有100,200,300的分布。

I can plot them separately: 我可以分别绘制它们:

ggplot(subset(my.data,a==1), aes(x=a,y=total)) + 
geom_boxplot() 

ggplot(subset(my.data,b==1), aes(x=b,y=total)) + 
geom_boxplot() 

ggplot(subset(my.data,c==1), aes(x=c,y=total)) + 
geom_boxplot()

I also tried the following, but it's not correct: 我也尝试了以下方法,但这是不正确的:

ggplot(my.data, aes(x=as.factor(c("a","b","c")),y=total)) + 
geom_boxplot() 

Hoping there is an awesome R function/method that let me do my plot in one shot. 希望有一个很棒的R函数/方法可以让我一次完成绘图。 Don't think I can use melt() because of the Total column. 不要认为由于Total列而不能使用melt()。 Thanks in advance. 提前致谢。


Edited: Apparently, I should/can use melt(), just need to use it correctly. 编辑:显然,我应该/可以使用melt(),只需要正确使用它即可。

Your data should be in long format, using the package Reshape2, for example 您的数据应为长格式,例如,使用软件包Reshape2

library(reshape2)
my.data <- melt(my.data, measure.vars=c("a","b","c"))

ggplot(subset(my.data, value==1), aes(x=variable,y=total)) + 
geom_boxplot() 

user3640617's answer is correct, but if you wanted to avoid the older reshape2 package, you could do the equivalent with the newer tidyverse : user3640617的答案是正确的,但是如果您想避免使用较旧的reshape2包,则可以使用较新的tidyverse进行等效tidyverse

library(tidyverse)
my.data <- gather(my.data, group, has.data, a:c) %>% 
    subset(has.data == 1)

plot.data <- ggplot(data = my.data, aes(x = group, y = total)) +
    geom_boxplot()
print(plot.data)

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

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