开发者

How to plot ratios based on characteristic in another column

I'm new to R and ggplot2 - any advice appreciated!

For each year I am trying to plot the ratios of incomes between different skill groups in ggplot. I.e. I want to have the ratio of incomes Level_4/Level_3, Level_4/Level_2 and Level_4/Level_1 in the same plot.

Data frame column headings are Skills (Levels 1 to 4), Year and MeanIncome

Here an extract from my data frame:

   Skills Year MeanIncome


1  Level_1 1970        9330.00
2  Level_1 1973       1开发者_开发百科1525.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

Thanks in advance for any advice.

(edited for legibility)


You can calculate the values by eg reshaping the dataframe (here called Data) :

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

Ratio is the data you can plot. As I don't really understand how you want your plot to look, I can't help you further. But with the ratio dataframe this can't be difficult.


Edit : just to aid the TS, I include the code I used to read in the data :

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)


Thanks, that helped a lot. I modified your answer to get what I want:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜