Skip to content

Instantly share code, notes, and snippets.

@linzhp
Last active January 23, 2020 10:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save linzhp/46e514251a8138ad1656 to your computer and use it in GitHub Desktop.
Save linzhp/46e514251a8138ad1656 to your computer and use it in GitHub Desktop.
Plot CCDF with an optional fitting line, inspired by the implementation at https://www.net.t-labs.tu-berlin.de/~fabian/sources/plot.ccdf.R
library(ggplot2)
ggplot.ccdf <- function(data, xlab='x', ylab='CCDF', xbreaks=NULL, ybreaks=NULL, c=NULL, alpha=NULL) {
x <- sort(data)
y <- 1-((1:(length(x))-1)/length(x))
df <- data.frame(x=x, y=y)
scale_x <- scale_x_log10()
if (!is.null(xbreaks)) {
scale_x <- scale_x_log10(breaks=xbreaks)
}
scale_y <- scale_y_log10()
if (!is.null(ybreaks)) {
scale_y <- scale_y_log10(breaks=ybreaks)
}
g <- qplot(x, y, data=df, xlab=xlab, ylab=ylab) + scale_x + scale_y
if (!is.null(c) && !is.null(alpha)) {
g <- g + geom_line(aes(x, c*x^(-alpha+1)))
}
g
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment