2011-05-22 97 views
4

我有這種結構3個dataframes(但不同的值)Y軸的值:R/GGPLOT2 - 通過數據幀

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

我想要做在GGPLOT2的線圖與3行,每一個數據集。問題是我想在Y軸上顯示相對於每個數據集而不是全局數據集的百分比。

我該怎麼做?我應該合併兩個數據框,還是爲不同的數據框調用三次geom_line()並改變那裏的Y值?

回答

5

有很多方法可以做到這一點,有些可能比這個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)) 
+0

只有一個問題:你爲什麼用'group'呢? – Barata 2011-05-23 08:15:18

+0

組告訴ggplot製作3行而不是1(嘗試刪除group = type並查看會發生什麼)。你可以用三個不同的調用geom_line的三行,但是然後着色它們並創建一個圖例更加尷尬。 – joran 2011-05-23 13:53:51