2015-02-10 43 views
0

我正在尋找繪製x = season和function = event的許多y變量的箱形圖的方法。我想提請許多情節都在一起,就像:使用ggplot2,qplot或其他繪製多個y變量的箱形圖

拳頭情節:X =季節,Y = VAR1通過函數=事件 第二幅圖:X =季節,Y = VAR1,由機能的研究=事件 ..

我的數據如下所示。其實,我有很多變數。

.............................................. ...........................

event  season  var1  var2  var3 
1 event_free wet.summer 14.193489 16.786347 22.65968 
2 event_free  fall 3.209114 9.948187 15.44799 
3  event  fall 4.564315 10.647883 34.24658 
4  event  fall 20.152646 31.114422 33.04899 
5 event_free  fall 3.944427 6.431695 10.27153 
6 event_free  fall 9.994351 16.110569 22.73702 
7 event_free  fall 3.100501 6.507310 14.37157 
8 event_free  winter 2.631117  NA 13.88889 
9  event  winter 20.745972 22.629357 29.27042 
10  event  winter 15.929737 21.355657 36.45409 
11 event_free  winter 7.383920 7.418910 11.85094 
12  event  winter 17.011810 20.320714 44.18071 
13  event  spring 12.501078 14.260404 39.08531 
14  event  spring 26.224773 32.536549 46.90560 

................ .................................................. .......

我發現很多方法可以用ggplot2或qplot來繪製一個結合了函數(例如:event,here)的y變量,但是找不到如何爲多個y變量繪製多個圖表。
非常感謝您的幫助!

苗條

回答

2

這裏是一種與ggplot2做到這一點:

library(reshape2) 
library(ggplot2) 

# Assume your data frame is named dat 
dat.m = melt(dat, id.var=c("event","season")) 
dat.m$season = factor(dat.m$season, levels=c("winter", "spring","wet.summer","fall")) 

# If you want the two levels of event plotted side by side 
ggplot(dat.m, aes(season, value, colour=event)) + 
    facet_grid(. ~ variable) + 
    geom_boxplot(width=0.7) 

enter image description here

# If you want the levels of event to be faceted (plotted in separate panels) 
ggplot(dat.m, aes(season, value)) + 
    facet_grid(event ~ variable) + 
    geom_boxplot() 

enter image description here

+0

非常感謝!這正是我想要的!請再給我一個。我有很多變數。我該如何給(1)我想一起繪製的一系列變量,以及(2)我想一起繪製的變量名稱? – user2928318 2015-02-16 08:41:06

+0

除非您的實際數據集與您提供的樣本數據不同,否則您只需要按照上述相同的方式將數據融入長格式。你能否詳細說明一下,具體來說,你是否試圖去做,並提供一些樣本數據,如果它與你已經提供的不同? – eipi10 2015-02-16 15:57:17

+0

我正在尋找直接從您提供的代碼中創建子集,但在應用您的代碼之前,只需添加諸如「dat = mydata [c(7,15:20)]」之類的內容即可。非常感謝,eipi10爲您提供了極大的幫助! :) – user2928318 2015-02-21 12:56:58

相關問題