Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I am still working with @3wen on visualizations of the North Pole. So far, it was not that difficult to generate maps, but we started to have problems with the ice region in the Arctic. More precisely, it was complicated to compute the area of this region (even if we can easily get a shapefile). Consider the globe,
worldmap <- ggplot() + geom_polygon(data = world.df, aes(x = long, y = lat, group = group)) + scale_y_continuous(breaks = (-2:2) * 30) + scale_x_continuous(breaks = (-4:4) * 45)
and then, add three points in the northern hemisphere, and plot the associated triangle
P1 <- worldmap + geom_polygon(data = triangle, aes(x = long, y = lat, group = group), fill ="blue", alpha = 0.6, col = "light blue", size = .8)+ geom_point(data = triangle, aes(x = long, y = lat, group = group),colour = "red")+
for some given projection, e.g.
coord_map("ortho", orientation=c(61, -74, 0))
This can be done with the following function
proj1=function(x=75){ triangle <- data.frame(long=c(-70,-110,-90*(x<90)+90*(x>90)), lat=c(60,60,x*(x<90)+(90-(x-90))*(x>90)),group=1, region=1) worldmap <- ggplot() + geom_polygon(data = world.df, aes(x = long, y = lat, group = group)) + scale_y_continuous(breaks = (-2:2) * 30) + scale_x_continuous(breaks = (-4:4) * 45) P1 <- worldmap + geom_polygon(data = triangle, aes(x = long, y = lat, group = group), fill ="blue", alpha = 0.6, col = "light blue", size = .8)+ geom_point(data = triangle, aes(x = long, y = lat, group = group),colour = "red")+ coord_map("ortho", orientation=c(61, -74, 0)) print(P1) }
rotation=function(Z,theta){ lon=Z[,1]/180*pi; lat=Z[,2]/180*pi x=cos(lon)*cos(lat) y=sin(lon)*cos(lat) z=sin(lat) pt1=cbind(x,y,z) M=matrix(c(cos(theta),0,-sin(theta),0,1,0,sin(theta),0,cos(theta)),3,3) pt2=t(M%*%t(pt1)) lat=asin(pt2[,3])*180/pi lon=atan2(pt2[,2],pt2[,1])*180/pi return(cbind(lon,lat))}
With a rotation from
From now on, it is possible to compute the area of any region containing the North Pole ! One should simply apply the function on all datebases generated from shapefiles ! We can then compute the centroid of the ice region,
r.glace=glace r.glace[,1:2]=rotation(glace[,1:2],pi/2) M=matrix(NA,length(unique(glace$id)),3) j=0 for(i in unique(glace$id)){j=j+1 Polyglace <- as(r.glace[glace$id==i,c("long","lat")],"gpc.poly") M[j,1]=area.poly(Polyglace) M[j,2:3]=centroid(r.glace[r.glace$id==i,c("long","lat")]) } Z=c(weighted.mean(M[,2],M[,1]),weighted.mean(M[,3],M[,1])) rotation(rbind(Z),-pi/2)[1,])
And we get below, we can visualize all the locations of the centroid of the ice region in the past 25 years
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.