我是编码的新手,想根据自己的数据创建箱形图。
为此,我想按特定值过滤箱形图:
我的数据结构称为“ Auswertungen”,其结构如下:
Participant Donation Treatment Manipulation
1 0 1 passed
2 0.4 2 passed
3 0.2 2 failed
4 0 3 failed
5 0.3 3 passed
现在我想使用箱线图绘制基于治疗的捐赠。我要绘制图形,一个包含所有数据点,另一个没有那些操作失败的图形。
我发现了
boxplot(Donation ~ Treatment)
with(subset(Auswertungen, Manipulation == "passed"), boxplot(Donation ~ Treatment))
但是第二个公式正向我显示与以前相同的箱线图,所以我猜该子集不起作用?
答案 0 :(得分:0)
知道了,抱歉。
boxplot(Donation ~ Treatment)
boxplot(Donation[Manipulation == "passed"] ~ Treatment[Manipulation == "passed"]
答案 1 :(得分:0)
如果数据的结构大致如下:
set.seed(222)
Donation <- abs(rnorm(20))
Treatment <- sample(1:3, 20, replace = T)
Manipulation <- sample(c("passed", "failed"), 20, replace = T)
df <- data.frame(Donation, Treatment, Manipulation)
df
Donation Treatment Manipulation
1 1.487757090 3 passed
2 0.001891901 2 failed
3 1.381020790 1 failed
4 0.380213631 3 passed
5 0.184136230 1 failed
6 0.246895883 3 passed
7 1.215560910 3 failed
8 1.561405098 1 failed
9 0.427310197 2 passed
10 1.201023506 3 passed
11 1.052458495 2 passed
12 1.305063566 2 failed
13 0.692607634 3 failed
14 0.602648854 3 failed
15 0.197753074 2 failed
16 1.185874517 2 passed
17 2.005512989 3 passed
18 0.007509885 2 passed
19 0.519490356 2 failed
20 0.746295471 2 failed
要拥有两个箱形图,可以先定义一个两面板布局:
par(mfrow = c(1,2))
然后将您的两个箱形图填充到其中,第一个未过滤:
boxplot(df$Donation ~ df$Treatment)
,第二个过滤条件是Manipulation=="passed"
:
boxplot((df$Donation[df$Manipulation=="passed"] ~ df$Treatment[df$Manipulation=="passed"]))