[英]How do I filter by multiple values of muliple categorical variables to make a plot in R?
假设我有这样的数据:
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)如何制作卷心菜和芹菜的砝码箱图? 即是一个单独的箱线图,其中数据来自列weight
,但仅当列类型为“白菜”或“芹菜”时才行。
2)如何通过两个分类变量进行箱线图过滤? 即一个箱形图,其中数据来自列weight
,但仅当列type
为“ cabbage”或“ celery”并且列source
为“ farm”时。
只需向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")])
这对type
使用普通字符串,但对因数也适用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.