我正在尝试在ggplot2中使用两个x变量生成一个分组的箱线图。这很简单
ggplot(boxplot_classes, aes(x=Group, y=Value, fill=Mutation)) +
geom_boxplot(position=position_dodge(0.8))
但是,我不需要比较第二个x变量定义的两个子组,但是对于第一个x变量定义的每个组,我需要将该组中的所有样本与第二个x变量中的一个子组进行比较。
这里举个例子。数据如下所示:
Value Mutation Group
32.00 Yes 1
5.00 no 1
18.00 no 1
3.00 no 1
16.00 no 1
14.00 Yes 1
28.00 Yes 1
28.00 Yes 1
49.00 Yes 1
15.00 Yes 1
43.00 no 2
49.00 Yes 2
40.00 Yes 2
17.00 Yes 2
9.00 no 2
31.00 Yes 2
8.00 Yes 2
43.00 no 2
50.00 Yes 2
48.00 Yes 2
11.00 Yes 3
42.00 no 3
0.00 Yes 3
15.00 Yes 3
8.00 no 3
1.00 Yes 3
41.00 no 3
15.00 no 3
4.00 no 3
31.00 Yes 3
我想生成一个图,在每个“组”中(在上面的示例中:1、2、3),将生成两个箱形图:一个用于该“组”中的所有样本,一个仅用于该组中的那些样本,其中也有mutation ==“ Yes”。在实际数据中,还有更多的“组存在”。
我希望我能很好地解释我的问题。不幸的是,我以某种方式缺少正确的语法或必须重新排列数据的方式。
非常感谢您的帮助!
编辑:我在https://s28.postimg.org/hvq8pb25p/Folie1.jpg上传了一个试图生成的图形示例
如果我们稍微处理您的数据,我们就可以做到。假设您的数据在dat
:
dat_yes <- dat[dat$Mutation == 'Yes',] #subset only Yes
dat_yes$Mutation_2 <- 'Yes' #add column
dat$Mutation_2 <- 'All' #add column
dat_full <- rbind(dat, dat_yes) #put together
#plot
ggplot(dat_full, aes(x = factor(Group), y = Value))+
geom_boxplot(aes(fill = Mutation_2))+
xlab('Group') +
scale_fill_brewer(palette = 'Set1', name = 'Mutation')
首先,我们创建一个名为的数据子集dat_yes
,其中仅包含带有的行Mutation == 'Yes'
。然后,我们创建一个新的列dat_yes
称为Mutation_2
这需要的价值'Yes'
而已。然后,我们在您的原始数据中添加一列,该列Mutation_2
仅采用的值'All'
。然后,我们rbind
dat
与dat_yes
创建dat_full
。最后,我们发送dat_full
给ggplot
。
dat <- structure(list(Value = c(32, 5, 18, 3, 16, 14, 28, 28, 49, 15,
43, 49, 40, 17, 9, 31, 8, 43, 50, 48, 11, 42, 0, 15, 8, 1, 41,
15, 4, 31), Mutation = c("Yes", "no", "no", "no", "no", "Yes",
"Yes", "Yes", "Yes", "Yes", "no", "Yes", "Yes", "Yes", "no",
"Yes", "Yes", "no", "Yes", "Yes", "Yes", "no", "Yes", "Yes",
"no", "Yes", "no", "no", "no", "Yes"), Group = c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L)), .Names = c("Value",
"Mutation", "Group"), class = "data.frame", row.names = c(NA,
-30L))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句