I'm new to mapping in R. I've created a map with longitude/latitude data points and now I want to create a contour map/heat map. I heard I need to interpolate or grid the data beforehand, so I tried that (using the function interp()) but it didn't work. Neither did contouring using a variety of different functions (e.g. contour() and contourplot()). I tried mapping with the maps package and both the base package and also ggplot2.
Here is a subset of my data:
latitude longitude d13C
-65 -70 -27.7
-61 150 -32.2
-61 150 -28.3
-60 116 -26.8
-60 116 -24.7
-47 38 -24.8
-38 150 -20.5
19 -65.7 -19.9
19 -65.5 -18.5
18 -60.7 -20
18 -58.5 -18.2
18 -57.8 -19
17 -55.4 -18.6
17 -50.8 -18
17 -47.1 -18.3
17 -45.5 -19.4
16 -43.3 -17.9
15 -40.7 -18.5
14 -39.3 -19.9
12 -36.7 -19.9
12 -36.2 -19.9
11 -34.4 -19.2
10 -32 -18.5
9 -30.3 -19.3
8 -29.2 -19.4
7 -26.6 -18.2
7 -25.5 -19.3
6 23.9 -20
3 -21.3 -20.4
Here is my code to create map with datapoints:
samps <- read.csv("Petra_phytoplankton+POM_xydata_minusNAs.csv") #my data for sampling sites, contains a column of "lat" and a column of "lon" with GPS points in decimal degrees
sampsdf<-data.frame(samps)
map_data<-map(database="world",col="grey80", fill=TRUE, ylim=c(-70,-30)) #confine map area
maps::map.axes()
#Create a function to generate a continuous color palette
rbPal <- colorRampPalette(c('blue','green','yellow','red'))
#This adds a column of color values
# based on the y values
samps$Col <- rbPal(10)[as.numeric(cut(samps$d13C,breaks = 10))]
points(samps$longitude, samps$latitude, pch=19, col= samps$Col, cex=0.5) #plot my sample sites
OR with ggplot2:
ggplot() + geom_polygon(data=subset(map_data("world", ylim=c(-70,-35))), aes(x=long, y=lat, group=group)) + stat_summary2d(data=sampsdf, aes(x = longitude, y = latitude, z = d13C)) + geom_polygon(data=subset(map_data("world", ylim=c(-70,-35))), aes(x=long, y=lat, group=group), color="white", fill=NA)+ theme_bw()
Here is my map made with the base package:
Here is my map made with ggplot2:
But I want it to look more like this - a contour heat map: R - Contour map or Moving projected map on filled.contour plot
For instance, I tried interpolating like this post: Plotting contours on an irregular grid ...but no luck!
Here is the code for my failed attempts:
library(akima)
interp(sampsdf$longitude, sampsdf$latitude, sampsdf$d13C) #didn't work
#####error: Error in interp.old(x, y, z, xo = xo, yo = yo, ncp = 0, extrap = extrap, :
missing values and Infs not allowed
contour(sampsdf$longitude,sampsdf$latitude,t(sampsdf$d13C), add=TRUE) #transpose d13C
###error message: Error in contour.default(sampsdf$longitude, sampsdf$latitude, t(sampsdf$d13C), :
increasing 'x' and 'y' values expected
samps_long<-array(sampsdf$longitude)
samps_lat<-array(sampsdf$latitude)
samps_d13C<-array(sampsdf$d13C)
contour(samps_long, samps_lat, t(samps_d13C), add=TRUE)###error: Error in contour.default(samps_long, samps_lat, t(samps_d13C), add = TRUE) : increasing 'x' and 'y' values expected
library(lattice)
contourplot(d13C ~ longitude+latitude, data=sampsdf)### error: Error using packet 1 NAs are not allowed in subscripted assignments ****(BUT I checked and is.na=FALSE for all columns (x,y,z)!!)****
contourplot(sampsdf$d13C, region=TRUE, at=seq(-36, -16, 4),
labels=FALSE, row.values=sampsdf$longitude, column.values=sampsdf$latitude,
xlim=c(100, 160), ylim=c(-80, -50), xlab='longitude', ylab='latitude')
###error: Error in UseMethod("contourplot") :
no applicable method for 'contourplot' applied to an object of class "c('double', 'numeric')"
library(ggmap) #this didn't work
ggmap(map_data, extent = "panel", maprange=FALSE) +
ggpoint(data=sampsdf)+
geom_density2d(data = sampsdf, aes(x = longitude, y = latitude)) +
stat_density2d(data = sampsdf, aes(x = longitude, y = latitude), geom = 'polygon') +
scale_fill_gradient(low = "green", high = "red") +
scale_alpha(range = c(0.00, 0.25), guide = FALSE) +
theme(legend.position = "none", axis.title = element_blank(), text = element_text(size = 12))
###this didn't work - Error: ggmap plots objects of class ggmap, see ?get_map
############### FYI I tried to follow up all these error message #####################
but I hit dead ends - which makes me think that Im doing something fundamentally
wrong...Im a total novice at this so if Im missing a crucial basic step that would be great
if you could help. ############################