2011-07-06 13 views
2

さて、本当に固まってしまった。私はこのようになり、データ・セットがあります。ggplot2のファセットに特定の値を使用する

    Species Latitude Longitude   Oiling Condition BirdCount  Date_ Oil_Cond  Date week.number 
1   Northern Gannet 30.32860 -89.19810 Not Visibly Oiled  Live   1 2010-07-21  1 2010-07-21   30 
2   Laughing Gull 30.23172 -88.32127 Not Visibly Oiled  Live   1 2010-05-05  1 2010-05-05   19 
3   Northern Gannet 30.26677 -87.59248  Visibly Oiled  Live   1 2010-05-05  2 2010-05-05   19 
4 American White Pelican 29.29649 -89.66432 Not Visibly Oiled  Live   1 2010-05-05  1 2010-05-05   19 
5   Brown Pelican 29.88244 -88.87624  Visibly Oiled  Live   1 2010-05-08  2 2010-05-08   19 
6   Brown Pelican 29.00290 -89.36961 Not Visibly Oiled  Live   1 2010-05-14  1 2010-05-14   20 
7   Northern Gannet 30.33390 -85.56565   Unknown  Live   1 2010-05-17  6 2010-05-17   21 
8    Common Loon 30.28177 -87.51028 Not Visibly Oiled  Live   1 2010-05-17  1 2010-05-17   21 
9   Brown Pelican 30.41410 -88.24542  Visibly Oiled  Live   1 2010-05-18  2 2010-05-18   21 
10  Northern Gannet 30.24063 -88.12451 Not Visibly Oiled  Live   1 2010-05-18  1 2010-05-18   21 

をそして私は鳥の5最も頻繁種(100以上のユニークな鳥種がある)のための変数Oil_Condをプロットする多面的なヒストグラムを取得しようとしています。

最初に私はすべての種で、ファセットを生産したかったし、次のコードを使用:

qplot(Oil_Cond, data = birds, facets = Species ~., geom = "histogram") 

しかし、もちろん、オーバーロードされたと100面の上にあっただろうので、動作しません。それで、私は本当にトップ5種を本当に気にかけることに決めました。そして、彼らはどんな頻度で出現したのでしょうか(Laughing Gull:3036、Brown Pelican:789、Northern Gannet:546、Royal Tern:321、ブラックスキマー:258)。しかし、私はそれをどうやって行うのか迷っています。

ご協力いただければ幸いです。

ありがとう:)

エイミー

+0

見ます。http:/ /stackoverflow.com/q/6073238/269476 – James

答えて

3

をここで行うための最も簡単な方法は、単にあなたのデータのサブセットをプロットすることであってもよいです。注意すべき唯一の潜在的な事は、種の変数が文字列ではなく因子として格納される場合です。

birdsSub <- subset(birds, Species %in% c('Laughing Gull','Brown Pelican', 
        'Northern Gannet','Royal Tern','Black Skimmer')) 
birdsSub$Species <- droplevels(birdsSub$Species) 

をして、あなたが前に持っているようにqplotに、このデータフレームを渡すことができる必要があります:最初のサブセットを作成します。 droplevelsの理由は、その変数が要素として格納されていると、もはや表示されなくなったすべての種が未使用の要素レベルとして '乗車するために来る'ことになります。 5つは空です。

+0

素晴らしい!本当にありがとう、私は先に行くとしよう! –

1

あなたが優れたplyrパッケージを使用して、これを取り組むことができ...最も数多くの因子レベルを選択することにより、密度プロットの行数を削減する上で同様の問題のため、この時

# If you don't already have plyr installed, uncomment the next line: 
# install.packages('plyr') 
require(plyr) 

# First, find out how many of each species you have... 

ns=ddply(birds,.(Species),summarise,n=length(Species)) 

# This will produce a table listing the number of each species you have 
# (in the column 'n'). Type 'ns' to see the table. 
# We can then rank the species occurrence, to see how important the different 
# species are 

ns$r = rank(-ns$n) # negative because 'rank' starts with the lowest number. 

# have a look at the top 5 species: 

subset(ns,r<=5) 

# There are a couple of ways to proceed from here. Either we could get the 
# top 5 species names from this 'ns' table: 
# names=as.character(subset(ns,r>=5)$Species) 
# and use joran's method, or we could merge the ns table and the original 
# dataset (so that each species has an 'n' and 'r' attribute) and subset the 
# data by species number or rank. I prefer the latter, as it allows you to 
# flexibly change the species number threshold. i.e.: 

birds=merge(birds,ns,by='Species') 

# We've now added 'n' and 'r' columns to the birds data, so we can select 
# our subset based on either of these columns: 

birds.by.r=subset(birds,r<=5) # selects only the top 5 bird species 
birds.by.n=subset(birds,r>=100) # selects all species with over 100 occurrences 

# Then just plot away! 

qplot(Oil_Cond,data=birds.by.r,facets=Species~.,geom='histogram') 

# or 

qplot(Oil_Cond,data=birds.by.n,facets=Species~.,geom='histogram') 
関連する問題