简体   繁体   中英

Frequency and cumulative frequency curve on the same graph in R

Is there a way (in R with ggplot or otherwise) to draw frequency and cumulative frequency curves in a single column (two rows) ie one top of the other such that a given quartile can be shown on both the curves using straight lines? I hope I am clear on this..

You may use this data..

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

在此处输入图像描述

or

Add a cumulative frequency column

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~.)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM