davidcarslaw / openair

Tools for air quality data analysis
https://davidcarslaw.github.io/openair/
GNU General Public License v2.0
305 stars 113 forks source link

[Question]: How to plot contributions of different pollutants to different clustered trajectories #364

Open Anuragsahucuraj opened 1 year ago

Anuragsahucuraj commented 1 year ago

I want to plot 4clusters of each pollutant like BLACK CARBON, OZONE etc separately I plotted a 4 cluster of all air mass trajectories. now from that How can i plot 4 clusters of a pollutant? means what is the contribution of BC in that Particular cluster

Anuragsahucuraj commented 1 year ago

I want to plot 4clusters of each pollutant like BLACK CARBON, OZONE etc separately I plotted a 4 cluster of all air mass trajectories. now from that How can i plot 4 clusters of a pollutant? means what is the contribution of BC in that Particular cluster

jack-davison commented 1 year ago

Hi Anurag,

You can use a bit of dplyr to calculate any statistic per cluster using the openair trajCluster() output:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

# get some traj data
traj_data <- openairmaps::traj_data

# cluster
clustered <- openair::trajCluster(traj_data, cols = "Dark2")


# calculate avg (or any stat...) per cluster
avg <-
  clustered$data$traj %>%
  group_by(cluster) %>%
  summarise(
    nox = mean(nox, na.rm = TRUE),
    o3 = mean(o3, na.rm = TRUE),
    pm10 = mean(pm10, na.rm = TRUE),
    pm2.5 = mean(pm2.5, na.rm = TRUE)
  )

# plot?
library(ggplot2)

ggplot(avg, aes(x = cluster, y = nox)) +
  geom_col(aes(fill = cluster), show.legend = FALSE) +
  scale_fill_brewer(palette = "Dark2") +
  theme_bw() +
  labs(y = openair::quickText("Average NOx in cluster (ug/m3)"))

Created on 2023-09-18 with reprex v2.0.2

If you want to put it back on a traj map, you can use the other element of trajCluster() output, join it to your summary stats, and use trajPlot().

clustered$data$results %>%
  left_join(avg) %>%
  openair::trajPlot(pollutant = "nox")

image

Anuragsahucuraj commented 1 year ago

How can i get to know the contribution of each cluster of each pollutant in %

This is a common plot for all trajectories Like this IGPCARE_LT