R / ggplot2 - 数据帧的Y轴值

时间:2011-05-22 18:08:55

标签: r ggplot2

我有三个具有此结构的数据帧(但值不同):

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

我想在ggplot2中用3行做一个折线图,每个数据集对应一行。问题是我想在Y轴上显示相对于每个数据集而不是全局数据集的百分比。

我该怎么做?我应该合并两个数据帧,还是为不同的数据帧调用三次geom_line()并更改Y值?

1 个答案:

答案 0 :(得分:5)

有很多方法可以做到这一点,有些可能比这更简单,但是这可以让你到达那里:

#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))