R / ggplot2 - Y-axis value according to the frame data

I have three data frames with this structure (but different values):

V1 V2 2010-04-30 30 2010-07-31 17 2010-10-02 20 

I want to make a line chart in ggplot2 with 3 rows, one for each dataset. The problem is that I want to display on the y-axis the percentage relative to each data set, not the global one.

How can i do this? Should I merge two data frames or call geom_line () three times for different data frames and change the value of Y there?

+4
source share
1 answer

There are many ways to do this, some probably cooler than that, but this allows you to:

 #Create three data frames along the lines of your example df1 <- data.frame(V1=c("2010-04-30","2010-07-31","2010-10-02"),V2=c(30,17,20)) df2 <- data.frame(V1=c("2010-04-30","2010-07-31","2010-10-02"),V2=c(10,5,42)) df3 <- data.frame(V1=c("2010-04-30","2010-07-31","2010-10-02"),V2=c(3,15,12)) #Combine them and create a variable to distinguish between them df <- rbind(df1,df2,df3) df$type <- rep(letters[1:3],each=3) #Use ddply to calculate the proportion by group (there are _lots_ of other ways to do this part) df <- ddply(df,.(type),.fun=function(x){x$V3 <- x$V2/sum(x$V2);return(x)}) #And plot ggplot(df,aes(x=as.Date(V1),y=V3)) + geom_line(aes(group=type,colour=type)) 
+5
source

All Articles