r - Add datapoints to existing scatterplot -
i have existing ggplot2 scatterplot shows results of parameter against normal database. want add 2 additional points graph pass command line arguments script script age value1 value2. show these points red r , l geom_text above each point. have following code far not know how add finishing touches
pkgload <- function(x) { if (!require(x,character.only = true)) { install.packages(x,dep=true, repos='http://star-www.st-andrews.ac.uk/cran/') if(!require(x,character.only = true)) stop("package not found") } } pkgload("ggplot2") #load current normals database df<-data.frame(read.csv("dat_normals.txt", sep='\t', header=t)) args<-commandargs(true) #specify each argument age <- args[1] rsbr <- args[2] lsbr <- args[3] # run regression , append prediction intervals lm_fit = lm(sbr ~ age, data = df) sbr_with_pred = data.frame(df, predict(lm_fit, interval='prediction')) p <- ggplot(sbr_with_pred, aes(x=age, y=sbr)) + geom_point(shape=19, alpha=1/4) + geom_smooth(method = 'lm', aes(fill = 'confidence'), alpha = 0.5) + geom_ribbon(aes(y = fit, ymin = lwr, ymax = upr, fill = 'prediction'), alpha = 0.2) + scale_fill_manual('interval', values = c('green', 'blue')) + theme_bw() + theme(legend.position = "none") ggsave(filename=paste("/home/data/wolf/fv_dat/dat_results.png",sep="")) browseurl(paste("/home/data/wolf/fv_dat/dat_results.png",sep"")) essentially, want see if 2 new points fall within 95% confidence intervals normal database (blue ribbon) 
your example not reproducible. constructive create data , reproducible example. not waste of time. solution, write said in comment. add new layer new data.
newdata <- data.frame(age = args[1], sbr = c(args[2],args[3])) p + geom_point(data=newdata,colour="red",size=10) for example:
sbr_with_pred <- data.frame(age = sample(15:36,50,rep=t), sbr = rnorm(50))
p <- ggplot(sbr_with_pred, aes(x=age, y=sbr)) + geom_point(shape=19, alpha=1/4) + geom_smooth(method = 'lm', aes(fill = 'confidence'), alpha = 0.5) args <- c(20,rnorm(1),rnorm(2)) newdata <- data.frame(age = args[1], sbr = c(args[2],args[3])) p + geom_point(data=newdata,colour="red",size=10) 
Comments
Post a Comment