【问题标题】:boxplot with filtered values带有过滤值的箱线图
【发布时间】:2020-05-27 15:51:59
【问题描述】:

我是编码新手,想根据我的数据创建箱线图。

为此,我想按特定值过滤箱线图:

我的数据结构称为“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))

但第二个公式向我显示的箱线图与以前完全相同,所以我猜该子集不起作用?

【问题讨论】:

    标签: r plot boxplot


    【解决方案1】:

    知道了,抱歉。

    boxplot(Donation ~ Treatment)
    boxplot(Donation[Manipulation == "passed"] ~ Treatment[Manipulation == "passed"]
    

    【讨论】:

    • boxplot 函数确实有一个 subset 参数,如果这有帮助的话。
    • 我知道,我使用子集函数尝试过(我需要它基于两列),但它不起作用。
    • 如果你要使用boxplot的公式方法,你真的应该使用data参数,一个可选的subset参数。我也想知道你是否附加了数据框?
    【解决方案2】:

    如果您的数据大致是这样的结构:

    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"]))
    

    结果会是这样的:

    【讨论】:

    • boxplot(Donation ~ Treatment, data=Auswertungen, subset=Manipulation=="passed") 更具可读性。
    猜你喜欢
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 2015-07-04
    • 1970-01-01
    • 2012-12-10
    • 1970-01-01
    相关资源
    最近更新 更多