r - ggplot2 help - superimposing GAMs to data + aesthetic changes -
so i'm trying create several plots on 1 time series plot using ggplot2
.
here's reproducible code (i hope):
# read in data , libraries fishdata <- read.csv("http://dl.dropbox.com/s/nsttbnaj7bzpk1g/fishdata.hilsatotal.csv", header=t) attach(fishdata) require(mgcv) require(ggplot2) # gams total.gam <- gam(totalfishfao ~ s(tonsaquaculturefao) +s(totalmarinefao), quasipoisson, fishdata) inland.gam <- gam(totalinlandfao ~ s(tonsaquaculturefao) + s(totalmarinefao), quasipoisson) hilsainland.gam <- gam(hilsashad.inland ~ s(tonsaquaculturefao) + s(totalmarinefao), quasipoisson) hilsatotal.gam <- gam(hilsasald.total ~ s(tonsaquaculturefao) + s(totalmarinefao), quasipoisson) gtfao.gam <- gam(grandtotal ~ s(totalmarinefao) + s(tonsaquaculturefao), quasipoisson) # plot df3 <- data.frame(cbind(year, totalfishfao, totalinlandfao, hilsashad.inland, hilsasald.total, grandtotal)) df3 ddd <- melt(df3, id.vars="year") ddd ppp <- ggplot(ddd, aes(year, value, shape=variable, colour=variable)) + geom_point() ppp_final <- ppp + xlab("year") + ylab("catches (tons)") + ggtitle("time series of target variables (1950-2011)") ppp_final2 <- ppp_final + stat_smooth() + scale_colour_discrete(name = "target variable (fao)", breaks = c("totalfishfao", "totalinlandfao", "hilsashad.inland", "hilsasald.total", "grandtotal"), labels=c("total (marine + inland)", "inland", "hilsa shad inland", "hilsa shad total \n (marine + inland)", "grand total \n (marine + inland + aquaculture)")) + scale_shape_discrete(name = "target variable (fao)", breaks = c("totalfishfao", "totalinlandfao", "hilsashad.inland", "hilsasald.total", "grandtotal"), labels=c("total (marine + inland)", "inland", "hilsa shad inland", "hilsa shad total \n (marine + inland)", "grand total \n (marine + inland + aquaculture)")) + scale_x_continuous(breaks=seq(1950,2011,10)) ppp_final2
i have 2 questions,
1) stat_smooth()
uses loess
method default, want use gam instead. want use gams above each corresponding target variable - there way of doing this? need superimpose model each target separately; if so, how?
2) there way change grand total (marine + inland + aquaculture)
& hilsa shad total (marine + inland)
filled shapes?
update 29-8-2013
i can want using plot:
plot(year, grandtotal, xlab="year", ylim=c(0,max(grandtotal)),ylab="catches (tons)", main="time series of target variables \n gams & 95% ci intervals") #plot(gtfao.gam, select=1) mydata = data.frame(year, totalmarinefao, tonsaquaculturefao, grandtotal) pv <- predict(gtfao.gam, mydata, type="response", se=t) lines(mydata$year, pv$fit) lines(mydata$year, pv$fit + 2*pv$se.fit, lty=2) lines(mydata$year, pv$fit - 2*pv$se.fit, lty=2) #inland par(new=t) plot(year, totalinlandfao, ylim=c(min(0), max(grandtotal)), xlab="", ylab="") pvv <- predict(inland.gam, mydata, type="response", se=t) lines(mydata$year, pvv$fit) lines(mydata$year, pvv$fit + 2*pvv$se.fit, lty=2) lines(mydata$year, pvv$fit - 2*pvv$se.fit, lty=2) ### etc......
when use:
stat_smooth(method="gam", family="quasipoisson", method="gam", family="quasipoisson", formula = totalfishfao ~ tonsaquaculturefao + totalmarinefao)
i these errors:
error in data.frame(x = xseq, y = model$family$linkinv(as.vector(pred$fit)), : arguments imply differing number of rows: 80, 62 in addition: warning messages: 1: in predict.gam(model, newdata = data.frame(x = xseq), se = se, type = "link") : not required variables have been supplied in newdata! 2: 'newdata' had 80 rows variables found have 62 rows error in if (nrow(layer_data) == 0) return() : argument of length 0 in addition: warning message: removed 68 rows containing missing values (geom_point).
Comments
Post a Comment