Skip to content

Instantly share code, notes, and snippets.

@shenqi
Last active August 29, 2015 14:01
Show Gist options
  • Save shenqi/4f1000b8507a943cfc5c to your computer and use it in GitHub Desktop.
Save shenqi/4f1000b8507a943cfc5c to your computer and use it in GitHub Desktop.
Pre-defined formats for ggplot
library(ggplot2)
require(ggplot2)
# also require ggplot_lm.R on https://gist.github.com/shenqi/29b42a6be83ec9e0517d
ggplot0 <- function(x, y, data, label, shape, ...){
return(ggplot(data, aes_string(x = x, y = y, label = label, shape = shape, ...)))
}
draw_ggplot <- function(x, y, data, point.size, label, label.hjust, shape, shape.solid, lm_formula, r2.adjusted, r2.pos_x, r2.pos_y, r2.digits, r2.color, legend.position, ...) {
g0 <- ggplot0(x, y, data, label, shape, ...) + geom_point(size = I(point.size)) + theme_classic()
if(!is.null(label)){g0 <- g0 + geom_text(hjust=label.hjust)}
if(!is.null(shape)){g0 <- g0 + scale_shape(solid=shape.solid)}
if(!is.null(lm_formula)){
lm0 <- lm(lm_formula, data=data)
g0 <- g0 + geom_lm_line(lm0, x=x)
if(!is.null(r2.adjusted)){
g0 <- g0 + stat_r2(lm0, r2.pos_x, r2.pos_y, r2.adjusted, r2.digits, color=r2.color)
}
}
if(!is.null(legend.position)) {g0 <- g0 + theme(legend.position=legend.position)}
return(g0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment