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? Ie a single boxplot in which the data comes from the column weight
, but only if the column type is "cabbage" or "celery".
2) How do I make a boxplot filtering by both categorical variables? 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".
Just provide boxplot
with the filtered data as follows
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.