基于另一列中的值的 R boxplot 子集列 [英] R boxplot Subset column based on value in another column

查看:34
本文介绍了基于另一列中的值的 R boxplot 子集列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在大范围搜索后需要一些帮助!

I need some help following a big search!

我需要根据另一列数据对一列进行子集化.

I need to subset a column based on another column of data.

我的数据集如下所示:

<头>
期限姓名真实结果
T1姓名 1真的4
T2名称 2错误6
T3名称 3真的5.5
T3名称 4错误4.6

dataset <-
  structure(
    list(
      Term = c("T1", "T2", "T3", "T3"),
      Name = c("Name1",
               "Name2", "Name3", "Name4"),
      TRUE. = c(TRUE, FALSE, TRUE, FALSE),
      Result = c(4, 6, 5.5, 4.6)
    ),
    class = "data.frame",
    row.names = c(NA,-4L)
  )

我希望能够对箱线图进行子集化,以仅显示其中一条记录的值为 True 的项.

I want to be able to subset a boxplot to only show terms where there is a value True for one of the records.

例如T1 的值为 True;T3 有两个值——一个真,一个假.我希望 x 轴显示箱线图的 T1 和 T3:

e.g. T1 has a value True; and T3 has two values - one true and one false. I want the x axis to show T1 and T3 for the box plot:

  ggplot(dataset, aes(x=Term, y=Result)) + 
  geom_boxplot()

推荐答案

根据 any 按Term"分组的 TRUE 值的存在对数据进行子集化,并在 ggplot

Subset the data based on the presence of any TRUE values grouped by 'Term' and use that in the ggplot

library(dplyr)
library(ggplot2)
dataset %>% 
    group_by(Term) %>% 
    filter(any(TRUE.)) %>%
    ggplot(aes(x = Term, y = Result)) + 
       geom_boxplot()


或者在 base R 中使用 subset

data_sub <-  subset(dataset, Term %in% unique(Term[TRUE.]))
ggplot(data_sub, aes(x=Term, y=Result)) + 
     geom_boxplot()

这篇关于基于另一列中的值的 R boxplot 子集列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆