pacman::p_load(pacman, ggplot2, dplyr, readr)

# Read the data
data <- read_csv("Productivity_Gradient_Germany.csv")
## Rows: 420 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Species
## dbl (3): Indicator_Value_Nitrogen, Individual, FA_Index
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Check the structure of the data
str(data)
## spc_tbl_ [420 × 4] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ Species                 : chr [1:420] "Centaurea jacea" "Centaurea jacea" "Centaurea jacea" "Centaurea jacea" ...
##  $ Indicator_Value_Nitrogen: num [1:420] 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 ...
##  $ Individual              : num [1:420] 1 2 3 4 5 6 7 8 9 10 ...
##  $ FA_Index                : num [1:420] 0.011 0.1 0.003 0.007 0.014 0.063 0.036 0.013 0.046 0.038 ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   Species = col_character(),
##   ..   Indicator_Value_Nitrogen = col_double(),
##   ..   Individual = col_double(),
##   ..   FA_Index = col_double()
##   .. )
##  - attr(*, "problems")=<externalptr>
# Summary statistics
summary_stats <- data %>%
  group_by(Species) %>%
  summarize(
    mean_FA = mean(FA_Index, na.rm = TRUE),
    sd_FA = sd(FA_Index, na.rm = TRUE),
    mean_IVN = mean(Indicator_Value_Nitrogen, na.rm = TRUE),
    sd_IVN = sd(Indicator_Value_Nitrogen, na.rm = TRUE)
  )

# Display the complete summary statistics
options(tibble.print_max = Inf, tibble.width = Inf)
print(summary_stats)
## # A tibble: 9 × 5
##   Species             mean_FA  sd_FA mean_IVN sd_IVN
##   <chr>                 <dbl>  <dbl>    <dbl>  <dbl>
## 1 Centaurea jacea      0.0304 0.0247     3.72  0.688
## 2 Lathyrus pratensis   0.0300 0.0402     3.77  0.583
## 3 Lotus corniculatus   0.0399 0.0363     3.63  0.593
## 4 Plantago lanceolata  0.0300 0.0284     3.63  0.593
## 5 Potentilla erecta    0.0297 0.0173     3.6   0.586
## 6 Prunella vulgaris    0.0353 0.0308     3.76  0.570
## 7 Ranunculus acris     0.0455 0.0343     3.74  0.595
## 8 Rumex acetosa        0.0253 0.0193     3.8   0.545
## 9 Trifolium pratense   0.0410 0.0346     3.63  0.593
# Boxplot of FA_Index by Species
ggplot(data, aes(x = Species, y = FA_Index)) +
  geom_boxplot() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(title = "Boxplot of FA_Index by Species", x = "Species", y = "FA Index")

# Scatter plot of FA_Index vs Indicator_Value_Nitrogen
ggplot(data, aes(x = Indicator_Value_Nitrogen, y = FA_Index, color = Species)) +
  geom_point(alpha = 0.6) +
  geom_smooth(method = "lm", se = FALSE) +
  labs(title = "Scatter plot of FA_Index vs Indicator_Value_Nitrogen", x = "Indicator Value Nitrogen", y = "FA Index")
## `geom_smooth()` using formula = 'y ~ x'

# Faceted scatter plots for each species
ggplot(data, aes(x = Indicator_Value_Nitrogen, y = FA_Index)) +
  geom_point(alpha = 0.6) +
  geom_smooth(method = "lm", se = FALSE) +
  facet_wrap(~ Species, scales = "free") +
  labs(title = "Scatter plot of FA_Index vs Indicator_Value_Nitrogen by Species", x = "Indicator Value Nitrogen", y = "FA Index")
## `geom_smooth()` using formula = 'y ~ x'

# Summary statistics for each combination of Species and Indicator_Value_Nitrogen
summary_by_species_ivn <- data %>%
  group_by(Species, Indicator_Value_Nitrogen) %>%
  summarize(
    mean_FA = mean(FA_Index, na.rm = TRUE),
    sd_FA = sd(FA_Index, na.rm = TRUE),
    count = n()
  ) %>%
  arrange(Species, Indicator_Value_Nitrogen)
## `summarise()` has grouped output by 'Species'. You can override using the
## `.groups` argument.
# Display the complete summary statistics
options(tibble.print_max = Inf, tibble.width = Inf)
print(summary_by_species_ivn)
## # A tibble: 42 × 5
## # Groups:   Species [9]
##    Species             Indicator_Value_Nitrogen mean_FA   sd_FA count
##    <chr>                                  <dbl>   <dbl>   <dbl> <int>
##  1 Centaurea jacea                          3    0.0352 0.0249     10
##  2 Centaurea jacea                          3.1  0.0159 0.00866    10
##  3 Centaurea jacea                          4.3  0.0373 0.0262     10
##  4 Centaurea jacea                          4.5  0.0331 0.0306     10
##  5 Lathyrus pratensis                       3.1  0.0214 0.0174     10
##  6 Lathyrus pratensis                       3.7  0.0437 0.0639     10
##  7 Lathyrus pratensis                       4.5  0.0248 0.0221     10
##  8 Lotus corniculatus                       3    0.0279 0.0276     10
##  9 Lotus corniculatus                       3.1  0.0384 0.0301     10
## 10 Lotus corniculatus                       3.2  0.0468 0.0528     10
## 11 Lotus corniculatus                       3.7  0.0411 0.0258     10
## 12 Lotus corniculatus                       4.3  0.036  0.0217     10
## 13 Lotus corniculatus                       4.5  0.0491 0.0521     10
## 14 Plantago lanceolata                      3    0.0484 0.0417     10
## 15 Plantago lanceolata                      3.1  0.0271 0.0178     10
## 16 Plantago lanceolata                      3.2  0.0144 0.0141     10
## 17 Plantago lanceolata                      3.7  0.0327 0.0306     10
## 18 Plantago lanceolata                      4.3  0.0342 0.0240     10
## 19 Plantago lanceolata                      4.5  0.023  0.0278     10
## 20 Potentilla erecta                        3    0.0301 0.0190     10
## 21 Potentilla erecta                        3.2  0.0343 0.0196     10
## 22 Potentilla erecta                        3.7  0.0302 0.0145     10
## 23 Potentilla erecta                        4.5  0.0242 0.0170     10
## 24 Prunella vulgaris                        3.1  0.0235 0.0266     10
## 25 Prunella vulgaris                        3.2  0.0396 0.0392     10
## 26 Prunella vulgaris                        3.7  0.0192 0.0163     10
## 27 Prunella vulgaris                        4.3  0.0567 0.0308     10
## 28 Prunella vulgaris                        4.5  0.0377 0.0270     10
## 29 Ranunculus acris                         3    0.0505 0.0456     10
## 30 Ranunculus acris                         3.2  0.0362 0.0217     10
## 31 Ranunculus acris                         3.7  0.0529 0.0418     10
## 32 Ranunculus acris                         4.3  0.0417 0.0370     10
## 33 Ranunculus acris                         4.5  0.0462 0.0229     10
## 34 Rumex acetosa                            3.2  0.0258 0.0254     10
## 35 Rumex acetosa                            3.7  0.0224 0.0189     10
## 36 Rumex acetosa                            4.5  0.0277 0.0134     10
## 37 Trifolium pratense                       3    0.0528 0.0447     10
## 38 Trifolium pratense                       3.1  0.0288 0.0383     10
## 39 Trifolium pratense                       3.2  0.0483 0.0326     10
## 40 Trifolium pratense                       3.7  0.0406 0.0427     10
## 41 Trifolium pratense                       4.3  0.0356 0.0215     10
## 42 Trifolium pratense                       4.5  0.0398 0.0250     10
# Boxplot of FA_Index by Indicator_Value_Nitrogen
ggplot(data, aes(x = as.factor(Indicator_Value_Nitrogen), y = FA_Index)) +
  geom_boxplot() +
  facet_wrap(~ Species, scales = "free") +
  labs(title = "Boxplot of FA_Index by Indicator_Value_Nitrogen and Species", x = "Indicator Value Nitrogen", y = "FA Index")