2012-10-19 134 views
0

數據我有數據,顯示誰是存儲爲CSV文件中的一張照片:情節多值(一對多)與GGPLOT2

|------------+---------------------------| 
| image  | people     | 
|------------+---------------------------| 
| image1.png | John, Paul    | 
| image2.png | John      | 
| image3.png |       | 
| image4.png | George, Paul, John, Ringo | 
| ...  |       | 
|------------+---------------------------| 

我想將其加載到R,並在繪製這不同的方式,但是我們假設我想要一個條形圖來顯示每個人的出場次數。

如果有幫助,我可以重構數據。

謝謝

+0

您的數據在R中的實際外觀如何?使用'dput(yourdata)'或你的數據的一個子集。上面的圖片不是R數據結構。但是,你問的是非常簡單的。從'reshape2'包中查看'melt'。 – Justin

+0

是的,這仍然是原始格式(例如CSV)。我也想弄清楚如何最好的將它加載到R. – oneself

+0

你可以把csv的一部分呢?我認爲它看起來不像那樣! – Justin

回答

1

像這樣的數據集描述你在你的問題提的情況:

require(plyr) 
people_list = c("Edward", "Smith", "Neo", "Mr. Anderson", 
       "Red John", "Blackbeard", "Lily", "Anne") 
dat = data.frame(image = sprintf("image%d.png", 1:100)) 
dat = ddply(dat, .(image), function(x) { 
    people = sample(people_list, size = sample(1:length(people_list), 1)) 
    return(data.frame(image = x$image, people)) 
}) 
> head(dat) 
     image  people 
1 image1.png Blackbeard 
2 image1.png  Edward 
3 image1.png  Anne 
4 image1.png  Lily 
5 image1.png  Neo 
6 image1.png Red John 

如果在此成型鑄造你的數據集,你可以從這個使用ddplyplyr計算彙總:

# Number of occurences of people 
occ = ddply(dat, .(people), summarise, no_occurence = length(people)) 
> occ 
     people no_occurence 
1   Anne   48 
2 Blackbeard   56 
3  Edward   46 
4   Lily   55 
5 Mr. Anderson   55 
6   Neo   51 
7  Red John   60 
8  Smith   56 

...並藉此創建barplot例如:

require(ggplot2) 
theme_set(theme_bw()) 
ggplot(occ, aes(x = people, y = no_occurence)) + geom_bar() 

enter image description here

這或許可以讓你在創建其他可視化開始。