Skip to content

Instantly share code, notes, and snippets.

@SwampThingPaul
Last active June 7, 2022 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SwampThingPaul/4ebf2e3607ba14d5375b0f2c756b2108 to your computer and use it in GitHub Desktop.
Save SwampThingPaul/4ebf2e3607ba14d5375b0f2c756b2108 to your computer and use it in GitHub Desktop.
Multi panel plot
library(sp)
library(rgeos)
library(rworldmap)
# epsg.io
wgs=CRS("+init=epsg:4326")
map_dat=getMap()
map_dat=spTransform(map_dat,wgs)
# fake data
set.seed(123)
foo.dat=data.frame(long=runif(150,-100,180),
lat=runif(150,-90,100),
val=rnorm(150,200,150))
foo.dat.shp=SpatialPointsDataFrame(foo.dat[,c("long","lat")],
data=foo.dat,
proj4string = wgs)
par(oma=c(0.5,0.5,0.5,0.5),mar=c(2,2,0.5,0.5))
layout(matrix(1:2,1,2),widths=c(1,0.5))
plot(map_dat,xlim=c(-180,180),ylim=c(-90,90),bg="lightblue",col="cornsilk",yaxs="i",xaxs="i")
plot(foo.dat.shp,add=T,pch=21,bg="red")
box(lwd=1)
# aggregate data into Latitude bins
bks.val=seq(-90,90,20)
lat.bin=findInterval(foo.dat$lat,bks.val)
foo.dat$lat.bin=bks.val[lat.bin]
mean.val=aggregate(val~lat.bin,foo.dat,mean)
#pseudo histogram
x=barplot(mean.val$val,horiz = T,space=0,xlim=c(0,400))
axis(2,x,bks.val,las=2)
box(lwd=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment