Created
March 13, 2013 02:48
-
-
Save joshbode/5149009 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(plyr) | |
| library(ggplot2) | |
| formatter = function(x) formatC(x, format='d', big.mark=',') | |
| # create some fake data | |
| variability = c(A=0.1, B=0.2, C=0.3) | |
| d = as.data.frame(mutate(list(), | |
| source=sample(c('A', 'B', 'C'), 1000, replace=TRUE), | |
| x=runif(1000, 200e3, 2e6), | |
| y=(0.8 + runif(1000, 0, 1) * variability[source]) * x + rnorm(1000, 0, 100e3) | |
| )) | |
| # fit a LTO for each source | |
| fits = ddply(d, .(source), function(s) { | |
| c(slope=coef(lm(y ~ x + 0, data=s))[[1]], intercept=0) | |
| }) | |
| # plot it all - multiple lines of fit | |
| p = ggplot(data=d) + | |
| geom_point(aes(x=x, y=y, colour=source), size=1) + | |
| geom_abline(data=fits, aes(intercept=intercept, slope=slope, colour=source), size=1) + | |
| theme_minimal() + labs(x="Market Value", y="Realised Value") + | |
| scale_x_continuous(labels=formatter) + scale_y_continuous(labels=formatter) | |
| plot(p) | |
| # log-log transformed with log line-hack | |
| slope = coef(lm(y ~ x + 0, data=d))[[1]] | |
| p = ggplot(data=d) + | |
| geom_point(aes(x=x, y=y)) + | |
| geom_line( | |
| aes(x=c(min(x), max(x)), y=c(min(x), slope * max(x))), | |
| colour='red', size=2 | |
| ) + | |
| theme_minimal() + labs(x="Market Value", y="Realised Value") + | |
| scale_x_log10(labels=formatter) + scale_y_log10(labels=formatter) | |
| #scale_x_continuous(labels=formatter) + scale_y_continuous(labels=formatter) | |
| plot(p) | |
| dev.copy2pdf(file='scatter_example.pdf') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment