1

I have a data frame like this

yr mo dy hr   lon   lat  cell   sst  avg     moavg
1900  1 29 17 -73.5 -59.5 10907 6.0 3.299048  6.00
1900  1 28 17 -72.5 -58.5 11268 6.4 3.928571  6.40
1900  1 25 17 -74.5 -57.5 11626 6.7 4.748500  6.70
1900  1 21 17 -73.5 -57.5 11627 6.8 4.569398  6.75
1900  1 22 17 -73.5 -57.5 11627 6.7 4.569398  6.75
1900  1 18 17 -70.5 -57.5 11630 6.6 4.385753  6.60

and I want to plot a map with the value of moavg for every cell. The problem is that I don't know how to pass the plot function the lon and lat value of the corresponding cell.

Many thanks

user3036416
  • 1,205
  • 2
  • 15
  • 28

1 Answers1

4

The image function will x- and y-values as well as a matrix of values to be plotted. Therefore, you'll need to convert your data into a matrix of plot values (with NA for the missing elements):

# Load the data frame
df <- read.table(text="yr mo dy hr   lon   lat  cell   sst  avg     moavg
1900  1 29 17 -73.5 -59.5 10907 6.0 3.299048  6.00
1900  1 28 17 -72.5 -58.5 11268 6.4 3.928571  6.40
1900  1 25 17 -74.5 -57.5 11626 6.7 4.748500  6.70
1900  1 21 17 -73.5 -57.5 11627 6.8 4.569398  6.75
1900  1 22 17 -73.5 -57.5 11627 6.7 4.569398  6.75
1900  1 18 17 -70.5 -57.5 11630 6.6 4.385753  6.60", header=T)

# Compute the ordered x- and y-values
lon <- sort(unique(df$lon))
lat <- sort(unique(df$lat))

# Build the matrix to be plotted
moavg <- matrix(NA, nrow=length(lon), ncol=length(lat))
moavg[cbind(match(df$lon, lon), match(df$lat, lat))] <- df$moavg

# Plot the image
image(lon, lat, moavg)

Heat map

josliber
  • 43,891
  • 12
  • 98
  • 133