You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this episode, we will extract NDVI values from a
57
-
raster time series dataset and plot them using the
58
-
`ggplot2` package.
59
+
In this episode, we will extract NDVI values from a raster time series dataset
60
+
and plot them using the `ggplot2` package.
59
61
60
62
## Extract Summary Statistics From Raster Data
61
63
62
-
We often want to extract summary values from raster data. For
63
-
example, we might want to understand overall greeness across a field site or at
64
-
each plot within a field site. These values can then be compared between
65
-
different field sites and combined with other
66
-
related metrics to support modeling and further analysis.
64
+
We often want to extract summary values from raster data. For example, we might
65
+
want to understand overall greeness across a field site or at each plot within
66
+
a field site. These values can then be compared between different field sites
67
+
and combined with other related metrics to support modeling and further
68
+
analysis.
67
69
68
70
## Calculate Average NDVI
69
71
70
-
Our goal in this episode is to create a dataframe that contains a single,
71
-
mean NDVI value for each raster in our time series. This value represents the
72
-
mean NDVI value for this area on a given day.
72
+
Our goal in this episode is to create a dataframe that contains a single, mean
73
+
NDVI value for each raster in our time series. This value represents the mean
74
+
NDVI value for this area on a given day.
73
75
74
-
We can calculate the mean for each raster using the `cellStats()` function. The
75
-
`cellStats()` function produces a named numeric vector, where each value
76
-
is associated with the name of raster stack it was derived from.
76
+
We can calculate the mean for each raster using the `global()` function. The
77
+
`global()` function produces a named numeric vector, where each value is
78
+
associated with the name of raster stack it was derived from.
77
79
78
80
```{r}
79
-
avg_NDVI_HARV <- cellStats(NDVI_HARV_stack, mean)
81
+
avg_NDVI_HARV <- global(NDVI_HARV_stack, mean)
80
82
avg_NDVI_HARV
81
83
```
82
84
83
-
We can then convert our output to a data frame using `as.data.frame()`. It's a good
84
-
idea to view the first few rows of our data frame with `head()` to make sure the
85
-
structure is what we expect.
85
+
The output is a data frame (othewise, we could use `as.data.frame()`). It's a
86
+
good idea to view the first few rows of our data frame with `head()` to make
87
+
sure the structure is what we expect.
86
88
87
89
```{r}
88
-
avg_NDVI_HARV <- as.data.frame(avg_NDVI_HARV)
89
90
head(avg_NDVI_HARV)
90
91
```
91
92
92
-
We now have a data frame with row names that are based on the original file name and
93
-
a mean NDVI value for each file. Next, let's clean up the column names in our
94
-
data frame to make it easier for colleagues to work with our code.
93
+
We now have a data frame with row names that are based on the original file
94
+
name and a mean NDVI value for each file. Next, let's clean up the column names
95
+
in our data frame to make it easier for colleagues to work with our code.
95
96
96
-
It is a bit confusing to have duplicate object \& column names (`avg_NDVI_HARV`). Additionally the "avg" does not clearly indicate what the value in that
97
-
particular column is. Let's change the NDVI column name to `meanNDVI`.
97
+
Let's change the NDVI column name to `meanNDVI`.
98
98
99
99
```{r view-dataframe-output}
100
100
names(avg_NDVI_HARV) <- "meanNDVI"
101
101
head(avg_NDVI_HARV)
102
102
```
103
103
104
-
By renaming the column, we lose the "HARV" in the header that reminds us what
105
-
site our data are from. While we are only working with one site now, we
106
-
might want to compare several sites worth of data in the future. Let's add a
107
-
column to our dataframe called "site".
104
+
The new column name doesn't reminds us what site our data are from. While we
105
+
are only working with one site now, we might want to compare several sites
106
+
worth of data in the future. Let's add a column to our dataframe called "site".
108
107
109
108
```{r insert-site-name}
110
109
avg_NDVI_HARV$site <- "HARV"
111
110
```
112
111
113
-
We can populate this column with the
114
-
site name - HARV. Let's also create a year column and populate it with 2011 -
115
-
the year our data were collected.
112
+
We can populate this column with the site name - HARV. Let's also create a year
113
+
column and populate it with 2011 - the year our data were collected.
116
114
117
115
```{r}
118
116
avg_NDVI_HARV$year <- "2011"
@@ -128,14 +126,15 @@ We'd like to produce a plot where Julian days (the numeric day of the year,
128
126
0 - 365/366) are on the x-axis and NDVI is on the y-axis. To create this plot,
129
127
we'll need a column that contains the Julian day value.
130
128
131
-
One way to create a Julian day column is to use `gsub()` on the file name in each
132
-
row. We can replace both the `X` and the `_HARV_NDVI_crop` to extract the Julian
133
-
Day value, just like we did in the [previous episode](13-plot-time-series-rasters-in-r/).
129
+
One way to create a Julian day column is to use `gsub()` on the file name in
130
+
each row. We can replace both the `X` and the `_HARV_NDVI_crop` to extract the
0 commit comments