R/ggplot2 - Value of Y axis by dataframe
I have three dataframes with this structure (but different values):
V1 V2
2010-04-30 30
2010-07-31 17
2010-10-02 20
I want to do a line chart in ggplot2 with 3 lines, one for each dataset. The problem is that i want to display in the Y axis the percentage relative to each dataset and not the global one.
How can i do this? Should i merge the two dataframes, or call 开发者_如何学Cthree times geom_line() for the different dataframes and change there Y value?
There are lots of ways to do this, some probably pithier than this, but this gets you there:
#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))
精彩评论