R中同一图表上的频率和累积频率曲线

时间:2012-04-05 14:29:11

标签: r plot ggplot2 frequency-distribution cumulative-frequency

是否有一种方法(在R中使用ggplot或其他方式)在单个列(两行)中绘制频率和累积频率曲线,即另一个顶部,使得可以使用直线在两条曲线上显示给定的四分位数行呢?我希望我对此很清楚..

您可以使用此数据..

mydata<-structure(list(speed = c(10, 15, 20, 25, 30, 35, 40, 45, 50),frequency = c(0, 1, 5, 10, 20, 10, 6, 3, 0)), .Names = c("speed","frequency"), row.names = c(NA, -9L), class = "data.frame")

1 个答案:

答案 0 :(得分:8)

mydata<-structure(list(speed = c(10, 15, 20, 25, 30, 35, 40, 45, 50),frequency = c(0, 1, 5, 10, 20, 10, 6, 3, 0)), .Names = c("speed","frequency"), row.names = c(NA, -9L), class = "data.frame")


library(ggplot2)

qplot(data=mydata,
      x=speed,
      y=frequency,
      geom=c("point", "line"))+
      geom_line(aes(y=cumsum(frequency)))

enter image description here

添加累积频率列

mydata$sum.freq<-with(mydata, cumsum(frequency))

library(reshape)
qplot(data=melt(mydata, id.vars="speed"),
       x=speed,
       y=value,
       geom=c("point", "line"), facets=variable~.)

enter image description here