简体   繁体   中英

Plotting regression line and r2 in scatterplot between two rasters in R?

I have two rasters in r

> lpjENLF
class      : RasterLayer 
dimensions : 2803, 5303, 14864309  (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333  (x, y)
extent     : 60.85, 105.0417, 15.95833, 39.31667  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
source     : memory
names      : VegC 
values     : 0, 17.99169  (min, max)

> geocarbon2
class      : RasterLayer 
dimensions : 2803, 5303, 14864309  (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333  (x, y)
extent     : 60.85, 105.0417, 15.95833, 39.31667  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
source     : memory
names      : layer 
values     : 0, 26.53035  (min, max)

I make a scatterplot between them by :

plot(lpjENLF, geocarbon2,maxpixels=900000)

Giving the output above散点图

I find out the corelation by :

> cor(values(geocarbon2), values(lpjENLF), use="complete.obs", method = 'pearson')
[1] 0.6883869

How can I plot regression line and pearson r2 coefficient on the scatterplot?

For plotting regression line and Pearson r2 coefficient on the scatterplot, you can use the following code

#Create 2 rasters
library(raster)

ras1 <- raster(nrow = 10, ncol = 10)
ras2 <- raster(nrow = 10, ncol = 10)
# Assign random cell values
set.seed(42)
values(ras1) <- runif(ncell(ras1))
values(ras2) <- runif(ncell(ras2))

#Create a data frame from the raster values
df <- cbind.data.frame(values(ras1), values(ras2))
names(df) <- c("ras1", "ras2")

#Plotting
library(ggpubr)

ggscatter(data = df, x = "ras1", y = "ras2", 
          add = "reg.line") + # Add regressin line
  stat_cor(aes(label = paste(..rr.label.., ..p.label.., sep = "~`,`~")), 
    label.x = 0, label.y = 1.1)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM