2015-12-02 29 views
1
> sleep 
extra group ID 
1 0.7  1 1 
2 -1.6  1 2 
3 -0.2  1 3 
4 -1.2  1 4 
5 -0.1  1 5 
6 3.4  1 6 
7 3.7  1 7 
8 0.8  1 8 
9 0.0  1 9 
10 2.0  1 10 
11 1.9  2 1 
12 0.8  2 2 
13 1.1  2 3 
14 0.1  2 4 
15 -0.1  2 5 
16 4.4  2 6 
17 5.5  2 7 
18 1.6  2 8 
19 4.6  2 9 
20 3.4  2 10 

我有這樣的數據和我應該把它除以GROUP對不同人的影響,並把它放到兩個不同的boxplot中,但你可以看到theres組1和組2,他們在相同的數據是組,所以我不知道如何將數據轉移到組1和組2,可以幫助我嗎?劃分數據並放到兩個盒子裏

回答

1

你並不需要劃分的數據放到一個箱線圖:

boxplot(extra~group,data=sleep) 

enter image description here

您可以探索可利用?boxplot不同的選項。

有些人喜歡用ggplot2包:

library(ggplot2) 

ggplot(sleep,aes(x=group,y=extra,group=group))+geom_boxplot() 

enter image description here

有些人喜歡lattice

bwplot(group~extra,data=sleep) 

enter image description here

+0

這使得意義很多,它的更簡單的是什麼我以爲我在嘗試東西像boxplot(extra(group == 1))哈哈感謝你! –

+0

對於直方圖和scaterplots它會起作用嗎?像hist(extra〜group,data = sleep,breaks = 5)??? SOrry煩擾xD –

+0

它不適用於'hist()',但你可以做'ggplot(sleep,aes(x = extra,fill = as.factor(group)))+ geom_histogram(position =「identity」 ,alpha = 0.5)'或'ggplot(sleep,aes(x = extra))+ geom_histogram()+ facet_grid(〜group)'。對於散點圖,您可以執行'plot(sleep [,1:2])或'ggplot(sleep,aes(x = group,y = extra))+ geom_point()' –

1

這是使用ggplot2的好數據集。

library(ggplot2) 
ggplot(sleep, aes(x=factor(group), y=extra)) + geom_boxplot()