---
title: "K4GGWA Spatial Data for GGW Restoration Decision-Making"
subtitle: "Mini-Project: Downloading and analysing your own data"
author: "K4GGWA Platform"
format:
  html:
    toc: false
    code-fold: true
    theme: cosmo
execute:
  warning: false
  message: false
---

# Mini-project

In this mini-project, you will use the [**GGW dashboard sampling tool**](https://dashboards.cifor-icraf.org/app/k4ggwa) to download and analyse data about a landscape you know.

You will:

1.  Choose an area of interest from the GGW dashboards and sample 100-500 points.
2.  Download the sampled indicators data as a CSV.
3.  Explore the distribution of environmental indicators.
4.  Create a density plot as your main visual output.
5.  Optionally investigate a relationship between two indicators.
6.  Write a short report explaining what you found and how that might inform decision-making in the GGW.

::: callout-note
**Time:** \~60-90 minutes\
**Output:** 1 PNG and a short summary report
:::

# Part 1 - Get your data from the dashboard

## 1. Choose your sampling area

Open the [**GGW dashboards**](https://dashboards.cifor-icraf.org/app/k4ggwa) and go to **Sample data**.

![](images/ggwdash.png)

You can define your sampling area in either of two ways:

-   **Country and district:** select a country, optionally select a district, and choose the number of random points.
-   **Custom polygon:** use the polygon tool on the map to draw your own area, then choose the number of points.

For the mini-project, use at least 100 points. If possible, try 200-500 points so that the distribution is easier to interpret.

Choose an area that is meaningful to your work. For example, a restoration landscape, project area, district, or an area you are familiar with.

## 2. Choose your indicators

Select the indicators you want to sample.

For the mini-project, **choose one indicator** you would like to understand. You may also select a second indicator if you want to try the optional relationship challenge later.

Indicator choices could include:

| Indicator      | Example question                                          |
|-----------------|------------------------------------------------------|
| Tree cover     | How variable is tree cover across my area?                |
| SOC            | Is soil carbon fairly uniform across my area of interest? |
| EVI trend      | Are most sampled pixels becoming greener or less green?   |
| Severe erosion | How widespread are high erosion values?                   |
| Rainfall       | How variable is annual rainfall across the area?          |
| Fire frequency | Are most places rarely or frequently burned?              |
| Cropland       | How much of the sampled landscape is cropland?            |

## 3. Generate and download the CSV

Click **Generate CSV** and save the file somewhere easy to find (suggestion to save it in the data folder we created on Day 2).

The sample dataset contains variables like:

-   `lon`, `lat` - sample point coordinates
-   `evi_trend` - greenness trend
-   `tree_cover_pct` - tree cover (%)
-   `erosion_pct` - severe erosion (%)
-   `rainfall_mm` - mean annual rainfall (mm)
-   `temperature_c` - mean annual temperature (°C)
-   `soc_g_c_per_kg` - soil organic carbon (g/kg)
-   `tn_g_per_kg` - total nitrogen (g/kg)
-   `cropland_pct` - cropland (%)
-   `fire_freq_pct_years` - fire frequency (% of years burned)
-   soil texture and pH variables
-   temperature and precipitation trend variables

::: callout-note
The column names in your csv file may be different to the ones above, depending on which you selected.
:::

# Part 2 - Bring the data into R

## 4. Load CSV

Run the code below and select the csv you downloaded from the dashboard.

```{r}
# Install and load libraries
```

```{r}
# Read in data. Remember the function we use to read in a dataset? Make sure to store your dataset in an object.
# Make sure your object name makes sense. For example, 'Afar_LDSF_data_points' or 'Djibouti_LDSF_data_points'.
```

```{r}
# Quick checks - remember our dim(), head(), names() and summary() functions?
```

Each row should represent one sampled spatial point.

# Part 3 - Describe your landscape

Choose one indicator and create a simple plot or chart that helps you understand what the variable looks like across your sampled landscape. For example:

-   Is tree cover concentrated around one range of values?
-   Is SOC highly variable?
-   Are EVI trends mostly positive or negative?
-   Does erosion have a long tail of unusually high values?

### Step 1. Choose your indicator

Select one of the indicators you sampled. Our data checks above should show you the names of each of the indicators as column names.

### Step 2. Explore the distribution

Start with a **histogram.** This shows how many sampled points fall into different value ranges.

```{r}
# Remember how to build plots with ggplot2? 
# We already used geom_boxplot(), geom_point(), and geom_sf(), amoungst others. To create a histogram, will be using geom_histogram(). 
# Try creating a histogram below. Save your histogram to an object name so we can easily recall and save it later. 
# Hint: make sure your indicator is plotted on the x-axis. 
```

Did you give your plot a title or axis labels? **Try adding labels if not, and changing the colour and number of the histogram bins.** The arguments for changing the number of bins and the colour are `bins =`and `fill =`, in the geom_histogram function.

What do you notice about your data? Are there any obvious patterns or shapes? Things to look for:

-   the centre of the distribution;
-   how widely values vary;
-   skewness or long tails;
-   unusually high or low observations;
-   possible groups or clusters.

### Step 3. Make a density plot

Now turn the same distribution into a smoother **density plot**. Think of this as a smoothed description of where observations are concentrated

```{r}
# Hint: the ggplot function for creating density plots is geom_density().
# Try adjusting the fill colour and transparency (alpha) as you did with your histogram.
```

A density plot is useful for seeing where and how observations are concentrated, and whether a distribution is narrow, broad, skewed, or potentially multimodal.

::: callout-tip
## Improve the figure

Before saving, make sure your figure contains all the necessary information so that somebody who is unfamiliar with the data can understand what is being shown.

-   Is there a title? Does it appropriately describe the figure?
-   Are the units clear? I.e., what unit of measurement is the indicator in.
-   Is there a figure caption? Does it identify the data source, date, author?
:::

### Step 4. Save your figure

```{r}
# We will use our ggsave function to save our figures. You can use the template code below. 
ggsave(
  filename = # Name of the figure you want to save to file,
  plot = # Name of object your stored your density or histogram in,
  width = 8, 
  height = 5.5, 
  dpi = 300)
```

::: callout-tip
You now have your main deliverable: a **300-dpi png** that can be used in a report, presentation, project update or social media post!
:::

# Part 4 - Make your final visual

Now that you've explored what your sampled data looks like across the landscape, you are going to generate a final mapped visual, using one of the mapping methods we learned during yesterday's practical sessions. We looked at things like:

-   Static map (`ggplot2`) where point data is coloured by its value (e.g., map shows points across your geographic extent with different colours for different values)

-   Interactive map (`leaflet`) with colour-coded markers

-   Interactive map (`leaflet`) with colour-coded markers and pop-ups

-   Interactive map (`leaflet`) with two different variables plotted, encoding one variable by marker size and the other by colour

**Using the blank code cell below, create your final visual.**

```{r}
# Mini-project: Create your final visual here

```

# Part 5 - Write up your summary report

Write a short summary of your data exploration and visualisation using the prompts below.

::: callout-warning
1.  **What did you investigate?** What variable did you explore and in what region/country?

    I sampled \_\_\_\_\_\_ in \_\_\_\_\_\_ and examined the distribution of \_\_\_\_\_\_.

2.  **What did you observe?** Describe the centre, spread, shape or any interesting features of the distribution.

    Most sampled points were \_\_\_\_\_\_, while \_\_\_\_\_\_. The distribution appears \_\_\_\_\_\_.

3.  **What might explain this pattern?** Think about climate, soils, vegetation, land use, management or other processes that could plausibly explain what you see.

    A possible explanation is \_\_\_\_\_\_.

4.  **How can this be useful to what you do?** Connect the pattern to a real restoration/monitoring/land-management question or problem.

    This could be useful for \_\_\_\_\_\_ because \_\_\_\_\_\_.

5.  **Limitations?** Are their constraints or limitations to this analysis? What other data or evidence could be used to support this work?

    This analysis doesn't show \_\_\_\_\_\_ because \_\_\_\_\_\_.
:::

# Part 6 - Turn it into something useful

**Finished? Your analysis doesn't have to stay inside R! Export your visualisation as a png or standalone html file.**

```{r}
# Export your visual to your computer as a standalone file so it can be used later, in reports, briefs, presentations, or other communications for your organisation
```

Now, you can combine your map or chart with the summary you wrote above, and use it in **reports, briefs, as a presentation figure, or even a LinkedIn post!**

If you share it publicly, **clearly state the data source** (sample of spatial pixels from LDSF predictive maps) and **link to GGW dashboards**, and **don't forget to tag Landscape Alliance, K4GGWA, and your colleagues!**

# Final checklist

-   [ ] Selected a meaningful sampling area in the dashboard

-   [ ] Downloaded sample points

-   [ ] Loaded the CSV into R

-   [ ] Explored the data with `summary()`

-   [ ] Made a histogram and/or density plot

-   [ ] Interpreted the distribution

-   [ ] Saved a PNG at 300 dpi

-   [ ] Written a short summary report

-   [ ] Connected the result to a real-world restoration or land-management question

-   [ ] Used the exported visual in current work (reports, presentations, communications)
