简体   繁体   中英

Plotting aggregated data in R

I have an aggregated data set in R. Now I wanted to plot the data according to the two keys in the data ie two columns. Below is the sample data set . Grades range from 1-25 and are divided in group of 5

Date             Grade         price 
01/01/2012        1             50
01/01/2012        2             20
01/01/2012        3             41
01/01/2012        4             56
02/01/2012        1             78
02/01/2012        2             89
02/01/2012        3             100
02/01/2012        4             12
02/01/2012        5             0
02/01/2012        6             60

sortmax=aggregate(a$price, by=list(a$Date), FUN=max)

If i aggregate the price by date and using the FUN=max, I will get the maximum price of a particular day. I want to find the maximum price of a particular grade group like max price in 1-5 grade and then max price for 6-10 grade and so on for everyday . So in simple words I will have 5 values for each day. Example

Date            Grade      Max price 
01/01/2012       1-5            56
01/01/2012       6-10           52
01/01/2012       11-15          56
01/01/2012       15-20          78
01/01/2012       21-25          50   
02/01/2012       1-5            100
02/01/2012       6-10           110
02/01/2012       11-15          56
02/01/2012       15-20          85 
02/01/2012       21-25          25

How do I plot the date vs max price for each group each day , like plotting daily max price of grade 1-5 for each day and one final question is how do I plot all of these lines on a single plot with different colors . Many packages are not available for R 3.1.1 , windows Xp

To group your grades, create a data.frame with the relationships - easy:

gradeRange <- data.frame(c(rep('1-5',5),rep('6-10',5)),1:10)

(keep going for up to 25 of course) and give it names (Grade, Range) for example; then append it to x:

x$gradeRange <- gradeRange[gradeRange$Grade == x$Grade]

Or write it with math (based on mults. of 5):

x$gradeRange <- floor((x$Grade-1)/5)+1

(and then you can make those factor values, or just leave them 1/2/3/4/5).

Then you can use ggplot to graph them, or summarize and use lattice or plot, using gradeRange for group.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM