2

I want to plot a spatial map in which each pixel is of 0.25 degree spatial resolution in R. For this I have fill values at 0.25 degree grid spacing.

Below is the data

    -----------------------    
     Lat     Lon     Value
    -----------------------
    19.25   82.75   3.6846
    19.50   82.50   3.6871
    19.50   82.75   3.6769
    19.50   83.00   3.7537
    19.50   83.25   3.9128
    19.50   85.00   4.7792
    19.75   82.00   3.6602
    19.75   82.25   3.7096
    19.75   82.50   3.6010
    19.75   82.75   3.5799
    19.75   83.00   3.6477
    19.75   83.25   3.8002
    19.75   83.50   3.8280
    19.75   83.75   3.9472
    19.75   84.00   4.1493
    19.75   85.00   4.7894
    19.75   85.25   5.1068
    19.75   85.50   5.5290
    20.00   81.50   3.1184
    20.00   81.75   3.2302
    20.00   82.00   3.3910
    20.00   82.25   3.4322
    20.00   82.50   3.3703
    20.00   82.75   3.3752

The map which I want to generate in R is shown below that I plotted in MATLAB.

enter image description here

  • 1
    You should tell us more about your data - have you read it in to R yet? Or is it in a file that you need to read? – Spacedman Aug 28 '17 at 11:51
  • I have rainfall values at different latitude, longitude in a excel file. These latitude and longitudes are at regular spacing i.e., 0.25 degree. I can easily read the data, but do not know how to plot it as a spatial grid map. @Spacedman – Mario Aug 28 '17 at 12:54
  • I have edited the question for clarification. – Mario Aug 29 '17 at 4:22
  • 3
    @Mario Have you tried rasterFromXYZ from the raster library? It should work since the cells are regular. You can then plot the raster. I can post this as an answer when the question is re-opened. – Janina Aug 29 '17 at 6:42
1

In the comments I recommended rasterFromXYZ which would be suitable for simple plotting of the data. However, having examined the question more carefully, I suggest the following solution using ggplot2:

library(ggplot2)
library(viridis)

test_data = read.csv("test.csv", sep = "")

ggplot() + geom_raster(data = test_data, aes(x=Lon, y = Lat, fill=Value)) + 
  coord_fixed(ratio = 1) +
  scale_fill_viridis(direction = -1) +
  theme_bw()

I used the data sample from the question. The viridis package is just for the colours. coord_fixed(ratio = 1) ensures that the spacing on the x and y axis will be equal, you can modify this by changing the ratio. theme_bw() is just to make the background white instead of the default grey theme.

enter image description here

  • I am getting a blank map with no colors. – Mario Aug 29 '17 at 12:41
  • @Mario huh. I am not sure where this problem comes from. So it just shows the grid? And is this happening with the original data as well as the sample? Did you change anything in the code or data (column names..)? – Janina Aug 29 '17 at 12:57
  • Only colors are missing, rest of the things are same. Apparently, same thng is happening when I am using rasterXYZ method. – Mario Aug 29 '17 at 13:00
  • 1
    @Mario oh. I guess that's beyond my knowledge and the extent of gis.stackexchange. I found the same issue (I suppose) on stackoverflow but without any solution. Maybe try a different computer? Update R? – Janina Aug 29 '17 at 13:07
  • 1
    Raster output from Windows servers via RDP (Windows' "Remote Desktop Protocol") can be badly broken. If you can fiddle with the RDP parameters (compression, bitmaps etc) you can make this work. First saw this about 10 years ago, not fixed yet, I see... – Spacedman Aug 29 '17 at 14:45
2

Given your data as a data frame:

> head(xyz)
    Lat   Lon  Value
1 19.25 82.75 3.6846
2 19.50 82.50 3.6871
3 19.50 82.75 3.6769
4 19.50 83.00 3.7537
5 19.50 83.25 3.9128
6 19.50 85.00 4.7792

convert to a raster - note the reordering of lat-lon:

> library(raster)
> r = rasterFromXYZ(xyz[,c(2,1,3)])
> plot(r)

enter image description here

Note this is the bottom bit of your figure. Empty cells contain NA values.

You probably also should give it a coordinate system - if its in WGS84 lat-long then you do:

> projection(r)="+init=epsg:4326"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.