2010-08-27 67 views
0

我是R和ggplot2的新手 - 感謝您的任何建議!如何根據另一列中的特性來繪製比率

對於每一年我試圖繪製ggplot中不同技能組之間的收入比率。即我想在同一個地塊中有收入比例Level_4/Level_3,Level_4/Level_2和Level_4/Level_1。

數據幀的列標題是技巧(級別1〜4),年份和MeanIncome

在這裏從我的數據幀的提取物:

Skills Year MeanIncome 


1 Level_1 1970  9330.00 
2 Level_1 1973  11525.00 
3 Level_1 1976  12740.00 
4 Level_1 1979  15533.33 
14 Level_2 1970  10171.00 
15 Level_2 1973  12400.00 
16 Level_2 1976  12012.50 
17 Level_2 1979  18550.00 
27 Level_3 1970  8580.00 
28 Level_3 1973  12433.33 
29 Level_3 1976  14673.33 
30 Level_3 1979  14400.00 
40 Level_4 1973  35000.00 
41 Level_4 1976  30000.00 
42 Level_4 1979  36000.00 

預先感謝任何建議。

(編輯易讀性)

+0

編輯你的問題,這樣的數據框將是可見的(選擇它,然後按下按鈕與0和1)。 – mbq 2010-08-27 12:33:11

回答

0

可以計算例如通過重塑數據幀(這裏稱爲數據)的值:

require(reshape) 
tmp <- cast(Data,Skills~Year,value="MeanIncome") 
ratio <- tmp[,5]/ tmp[,2:4] 
rownames(ratio) <- tmp$Year 

比是你可以繪製數據。由於我不太瞭解你想要你的情節,所以我不能幫你進一步。但是對於數據比率來說,這並不困難。


編輯:只是爲了幫助TS,我包括我以前在數據讀取代碼:

Data <- read.table(textConnection(
"Skills Year MeanIncome 
Level_1 1970 9330.00 
Level_1 1973 11525.00 
Level_1 1976 12740.00 
Level_1 1979 15533.33 
Level_2 1970 10171.00 
Level_2 1973 12400.00 
Level_2 1976 12012.50 
Level_2 1979 18550.00 
Level_3 1970 8580.00 
Level_3 1973 12433.33 
Level_3 1976 14673.33 
Level_3 1979 14400.00 
Level_4 1973 35000.00 
Level_4 1976 30000.00 
Level_4 1979 36000.00") 
, header=T) 
0

謝謝,這幫助了很多。我修改了你的答案,以得到我想要的:

require(reshape) 
tmp <- cast(data,Year~Skills,value="Meanincome") 
ratio <- tmp[,3:5]/tmp[,2] 
rownames(ratio) <- tmp$Year 
+0

不客氣。我注意到我在代碼中搞亂了大寫,現在已經修正了。 – 2010-08-27 15:17:40

相關問題