Skip to content

Instantly share code, notes, and snippets.

@bmschmidt
Created April 1, 2014 13:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bmschmidt/9914296 to your computer and use it in GitHub Desktop.
Save bmschmidt/9914296 to your computer and use it in GitHub Desktop.
rm(list=ls())
source("SQLFunctions.R")
plotSet = function(filename,dck=701,alpha=.01,lwd=8) {
library(grid)
paths = tbl(tblsrc,"paths") %.%
filter(DCK==dck) %.%
arrange(voyagenum,yearday) %.%
select(LON,LAT,voyagenum) %.%
collect()
paths = paths %.% mutate(LON=LON+180)
paths$LON[paths$LON<0] = paths$LON[paths$LON<0] + 360
paths$LON[paths$LON>360] = paths$LON[paths$LON>360] - 360
#break groups that cross from 0 to 360 or vice versa, which otherwise leave streaks
paths$split = c(0,abs(paths$LON[-1]-paths$LON[1:(nrow(paths)-1)])>20)
paths$segment = 100*paths$voyagenum + cumsum(paths$split)
#desperately try to remove all elements: code stole from stack overflow, somewhere.
new_theme_empty <- theme_bw()
new_theme_empty$line <- element_blank()
new_theme_empty$rect <- element_blank()
new_theme_empty$strip.text <- element_blank()
new_theme_empty$axis.text <- element_blank()
new_theme_empty$plot.title <- element_blank()
new_theme_empty$axis.title <- element_blank()
new_theme_empty$plot.margin <- structure(c(0, 0, -1, -1), unit = "lines", valid.unit = 3L, class = "unit")
plotting = paths
#Actually make the plot:
plot = ggplot(plotting,
aes(x=LON,y=LAT,group=segment)) +
geom_path(alpha=alpha,lwd=lwd) +
labs(x="",y="",title="") +
theme(line=element_blank(),
rect=element_blank(),
axis.text=element_blank()
)+ coord_equal() + scale_x_continuous(expand=c(0,0),limits=c(0,360)) +
scale_y_continuous(expand=c(0,0),limits=c(-90,90))
png(filename=filename,width=4096*2,height=2048*2)
#plotting code from: [whoops forgot to paste it in, but SO somewhere again]
#plots just the central panel, which makes it compatible with the format Science on a Sphere wants.
gt <- ggplot_gtable(ggplot_build(plot))
ge = subset(gt$layout,name=="panel")
grid.draw(gt[ge$t:ge$b, ge$l:ge$r])
graphics.off()
}
dcks = tbl(tblsrc,"voyages") %.%
group_by(DCK) %.% summarize(count=n(),points=sum(points)) %.%
arrange(-count) %.% collect()
dcks %.% filter(points<3000000,points>10000) %.%
group_by(DCK) %.%
do(function(row) {
dck = row$DCK[1]
filename = paste0("~/Dropbox/",dck,"-2.png")
if (!file.exists(filename)) {
plotSet(filename=filename,dck=dck,alpha=.04,lwd=3)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment