*average, median, variance, standard deviation*and many more are used to get a general overview from data. Example, from 365 daily temperature data for a region within a year, it will be difficult to get a common sense from it. Therefore we use those statistics figures like minimum and maximum temperature, average temperature, variance, etc to get a quick overview.

*raster statistics*tool. But how to get raster statistics parameters based on polygon regions in a vector layer? I will discuss it in this tutorial and also plot the result in a graph as in figure 1.

Figure 1. Average NDVI for multiple local areas |

### Raster Statistics by Vector Polygon Calculation

Now let's see how to calculate raster statistics by vector layer in QGIS.

Firstly add a raster and a vector layer that contains some polygons. In figure 2, can be seen I added a NDVI raster and a local boundary polygon as mentioned before.

Figure 2. Raster and Vector Data |

Open the *Processing Toolbox*. In the *search*, type a keyword
** statistics**. All tools that relate to this keyword will appear. Choose

*v.rast.stats*from GRASS as in figure 3.

Figure 3. v.rast.stats tool |

*v.rast.stats*tool window as in figure 4, select the vector polygon, raster data and specify a prefix for new generated statistics field(s).

Figure 4. v.rast.stats window |

Next in *The methods to use* option. There are 13 statistics parameters
that will be computed. You can check/uncheck the option to calculate only
certain parameters as in figure 5.

Figure 5. Statistics Parameters |

*v.rast.stats*tool window. After running the tool, a new vector layer with all computed statistics parameter fields will be added into the map. Figure 6 is the preview of the attribute table from the new added layer. What can you observe? Is there something wrong?

Figure 6. Statistics parameters in attribute table |

Yes, there is something wrong with it. Now there are 90 rows/polygons. It must be 22 polygons. Where are these new rows coming from? Honestly, I don't know. But fortunately, if you observe the value, they are all the same. To get rid of this, we can dissolve it based on a statistics parameter field. Moreover, I think it does not happen not for all cases, so it's better to check it before you're doing a dissolve operation.

### Dissolve Operation

*Vector > Geoprocessing Tools > Dissolve..*. as shown in figure 7. Then from the

*Dissolve*window select input vector and a field as in figure 8.

Figure 7. Dissolve tool |

Figure 8. Dissolve tool window |

Figure 9. Attribute table after dissolve |

### Plotting The Graph

*Data Plotly*plugin. It's not installed by default. Therefore you have to install it from

*Plugin >*

*Manage and Install Plugins*.. menu and make sure it's checked as in figure 10.

Figure 10. Enabled Data Plotly Plugin |

*Data Plotly*window should appear as in figure 11. From the window select a

*plot type*. In this case I want to create a scatter plot. Then select the vector layer. Next define

*x*and

*y*fields. In the next section you can type a title for the legend. Because I want to make a line graph with points, so in the

*Marker type*option I chose

*Points and Lines*.

Figure 11. Data Plotly window |

Next, in the Layout options you can set a title for the graph and set
*x* and *y* label as seen in figure 12.

Figure 12. Data Plotly layout options |

After this step, you can create the graph by pushing
*Create Plot* button in the bottom of the window and you should get a
graph like figure 1.

That's all the tutorial on how to calculate raster statistics by vector
layer in QGIS. We already discussed how to get raster statistics for each
polygon in a vector layer using the *v.rast.stats* tool from GRASS and
plot a graph to visualize it using the *Data Plotly* plugin. Thanks for
reading!

In another post I discussed how to calculate summary statistics for selected features by location in QGIS. Check it out!