Search code examples
rrasterr-rasterspatial-data-frame

Convert a raster to a dataframe and extract values you want in R


I want to make a bathymetry map using ggplot2. I converted a raster file to a data frame, clopped and converted it to a dataframe:

library(raster)

##Import a raster file
Bathymetry_dat <- raster("w001000.adf")

##Clop the raster file
Bathy_clopped <- crop(Bathymetry_dat, extent(84.11236, 108.4594, -4.046979, 24.09534))

##Convert it to a dataframe    
datframe_bathy<-as.data.frame(Bathy_clopped, xy = TRUE)

Then, I checked values (i.e. depth in m) in the dataframe:

> summary(datframe_bathy)
       x                y          w001000_COUNT    
 Min.   : 84.11   Min.   :-4.046   Min.   :   9945  
 1st Qu.: 90.20   1st Qu.: 2.987   1st Qu.:  81618  
 Median : 96.28   Median :10.021   Median : 168447  
 Mean   : 96.28   Mean   :10.021   Mean   : 210212  
 3rd Qu.:102.37   3rd Qu.:17.054   3rd Qu.: 336718  
 Max.   :108.45   Max.   :24.087   Max.   :1205362  
                                   NA's   :3449125 

Depth (m) values are supposed to be negative and should't be this large. Then, I checked the bathymetry file imported in R.

> Bathy_clopped
class       : RasterLayer 
dimensions  : 3377, 2922, 9867594  (nrow, ncol, ncell)
resolution  : 0.008333333, 0.008333333  (x, y)
extent      : 84.10833, 108.4583, -4.05, 24.09167  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : w001000 
values      : -6138, -1  (min, max)
attributes  :
           ID  COUNT
 from: -11584      1
 to  :     -1 676804

There are two attributes: ID and COUNT. I suppose ID is depth (m) and don't know what COUNT is. And it is weird that COUNT ranges from 1 to 676804 and but summary() shows values from 9945 to 1205362.

So my question is how can I convert a raster file to a dataframe that contains values I want?

With the "COUNT" values, I was able to generate a bathymetry map, but the values in the legend are not correct..

Thanks in advance.


Solution

  • There is an attribute table with your raster layer. We can use levels to acces it.

    library(raster)
    
    # See the attribute table
    head(levels(Bathy_clopped)[[1]])
    #       ID COUNT
    # 1 -11584     1
    # 2 -10944     1
    # 3 -10907     1
    # 4 -10900     1
    # 5 -10879     1
    # 6 -10878     1
    

    We can then manipulate this attribute table, replacing COUNT with the depth, which is ID.

    # Get the attribute table
    RAT <- levels(Bathy_clopped)[[1]]
    
    # Replace COUNT with ID
    RAT$Depth <- RAT$ID
    RAT$COUNT <- NULL
    
    # Replace the attribute table
    Bathy_clopped2 <- Bathy_clopped
    levels(Bathy_clopped2)[[1]] <- RAT
    
    # Create a single layer based on the new RAT
    Bathy_clopped2 <- deratify(Bathy_clopped2)
    
    # Create a data frame
    datframe_bathy2 <-as.data.frame(Bathy_clopped2, xy = TRUE)
    

    Now the values in datframe_bathy2 are as expected.

    # Summarize the data frame
    summary(datframe_bathy2)
    #       x                y              COUNT        
    # Min.   : 84.11   Min.   :-4.046   Min.   :-6138    
    # 1st Qu.: 90.20   1st Qu.: 2.987   1st Qu.:-3800    
    # Median : 96.28   Median :10.021   Median :-2428    
    # Mean   : 96.28   Mean   :10.021   Mean   :-2154    
    # 3rd Qu.:102.37   3rd Qu.:17.054   3rd Qu.:  -65    
    # Max.   :108.45   Max.   :24.087   Max.   :   -1    
    #                                   NA's   :3449125