[英]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.