簡體   English   中英

如何過濾多個類別分類變量的值以在R中繪制圖?

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM