简体   繁体   English

如何过滤多个类别分类变量的值以在R中绘制图?

[英]How do I filter by multiple values of muliple categorical variables to make a plot in R?

Suppose I have data like this: 假设我有这样的数据:

type      source   weight
cabbage   store    2.2
cabbage   farm     2.3
cabbage   farm     1.9
celery    store    2.1
celery    farm     2.0
celery    store    1.7
turnip    farm     1.5
turnip    store    2.5

1) How do I make a boxplot of weights for cabbage and celery combined? 1)如何制作卷心菜和芹菜的砝码箱图? Ie a single boxplot in which the data comes from the column weight , but only if the column type is "cabbage" or "celery". 即是一个单独的箱线图,其中数据来自列weight ,但仅当列类型为“白菜”或“芹菜”时才行。

2) How do I make a boxplot filtering by both categorical variables? 2)如何通过两个分类变量进行箱线图过滤? Ie a single boxplot in which the data comes from the column weight , but only if the column type is "cabbage" or "celery" AND the column source is "farm". 即一个箱形图,其中数据来自列weight ,但仅当列type为“ cabbage”或“ celery”并且列source为“ farm”时。

Just provide boxplot with the filtered data as follows 只需向boxplot提供经过过滤的数据,如下所示

df<-data.frame(type=c("cabbage","cabbage","cabbage","celery","celery","celery","turnip","turnip"), weight=c(2.2,2.3,1.9,2.1,2.0,1.7,1.5,2.5))
> df
     type weight
1 cabbage    2.2
2 cabbage    2.3
3 cabbage    1.9
4  celery    2.1
5  celery    2.0
6  celery    1.7
7  turnip    1.5
8  turnip    2.5
> boxplot(df$weight[df$type %in% c("cabbage","celery")])

This uses plain strings for type , but it will also work for factors. 这对type使用普通字符串,但对因数也适用。

在此处输入图片说明

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

相关问题 如何从 R 中的 plot 中删除一些分类变量? - How do I drop some categorical variables from a plot in R? 如何在R中的图形上绘制多个类别变量? - How do I plot a number of categorical variables on a graph in R? 如何在R中绘制多个类别变量 - How to plot multiple categorical variables in R 如何在R中绘制带有多个分类变量的平行坐标 - How to plot parallel coordinates with multiple categorical variables in R 如何映射分类变量以在 R Plotly 中为 3D 散点图中的点的轮廓着色? - How do I map categorical variables to color the outline of points in a 3D scatter plot in R Plotly? R图-分层的数值和分类多个变量 - R Plot - Multiple Variables, Numeric and Categorical, Layered 带R的分类变量的图 - plot of categorical variables with R 如何在 R 中将一个分类变量转换为多个虚拟变量? - How do convert a categorical variable into multiple dummy variables in R? 如何创建具有多个分类变量和交互作用的数据框,并按ID分组? - How do I make my create a dataframe with multiple categorical variables and interaction effects, grouped by ID? 如何通过分类变量过滤R中的data.frame? - How do I filter a data.frame in R by categorical variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM