有很多方法可以做到這一點,有些可能比這個pithier,但是這讓你有:
#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))
只有一個問題:你爲什麼用'group'呢? – Barata 2011-05-23 08:15:18
組告訴ggplot製作3行而不是1(嘗試刪除group = type並查看會發生什麼)。你可以用三個不同的調用geom_line的三行,但是然後着色它們並創建一個圖例更加尷尬。 – joran 2011-05-23 13:53:51