{ "cells": [ { "cell_type": "code", "execution_count": 6, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [], "source": [ "# This script is used to create polar plots and pollution roses for the EM27 data\n", "# Load required libraries\n", "library(ggplot2)\n", "library(grid)\n", "library(openair)\n", "library(latticeExtra)\n", "library(dplyr)\n", "library(readr) # safer than base read.csv\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "987b025c", "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] \"ch4_co2_good_group\" \"co_co2_good_group\" \n" ] } ], "source": [ "all_rr_df_path <- '/uufs/chpc.utah.edu/common/home/u0890904/LAIR_1/Projects/Ratio_paper_2025_v5/Data/ha/all_rolling_regr_df_filtered.csv'\n", "\n", "# Use readr::read_csv instead of base read.csv\n", "all_rr_df <- read_csv(all_rr_df_path, show_col_types = FALSE)\n", "\n", "# Clean up column names (remove spaces, special chars)\n", "names(all_rr_df) <- make.names(names(all_rr_df), unique = TRUE)\n", "\n", "# Inspect what columns exist that match\n", "print(grep(\"good_group\", names(all_rr_df), value = TRUE))\n", "\n", "# Parse datetime properly\n", "all_rr_df <- all_rr_df %>%\n", " mutate(\n", " dt = as.POSIXct(dt, format = \"%Y-%m-%d %H:%M:%S\", tz = \"America/Denver\"),\n", " date = as.Date(dt)\n", " )\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "5175bb07", "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "pdf: 2" ], "text/latex": [ "\\textbf{pdf:} 2" ], "text/markdown": [ "**pdf:** 2" ], "text/plain": [ "pdf \n", " 2 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Set the regression label and type\n", "regr_label <- 'co_co2'\n", "x_name = \"xco2.ppm._wmean_ex5q_divak\"\n", "if (regr_label == 'ch4_co2'){\n", " y_name = \"xch4.ppm._wmean_ex5q_divak\"\n", "} else if (regr_label == 'co_co2'){\n", " y_name = \"xco.ppm._wmean_ex5q_divak\"\n", "}\n", "regr_type <- 'york'\n", "permil = TRUE\n", "\n", "save_plots <- TRUE\n", "show_plots <- FALSE\n", "\n", "# Figure out the right \"good group\" column\n", "good_col <- grep(paste0(\"^\", regr_label, \"_good_group$\"), names(all_rr_df), value = TRUE)\n", "\n", "# Filter safely\n", "frr_df <- all_rr_df %>%\n", " filter(!is.na(.data[[good_col]]))\n", "if (permil) {\n", " frr_df <- frr_df %>%\n", " mutate(across(.cols = grep(\"slope\", names(frr_df), value = TRUE), \n", " .fns = ~ . * 1000))\n", "}\n", "stat_type <- 'slope' # the statistic to plot\n", "var_of_interest <- paste(regr_label,regr_type,stat_type,sep='_') # the variable of interest from the regression label and type\n", "\n", "if (save_plots) {\n", "png(\"/uufs/chpc.utah.edu/common/home/u0890904/LAIR_1/Projects/Ratio_paper_2025_v5/Figures/ha/polar_co_co2.png\",width=2000,height=1600,res=300)\n", "polarPlot(frr_df,\n", " pollutant = var_of_interest,\n", " statistic = 'mean',\n", " cols = 'viridis',\n", " limits = c(0, 11),\n", " angle.scale = 45,\n", " par.settings = list(fontsize = list(text = 30)),\n", " units = '',\n", " key.footer = NULL,\n", ")\n", "dev.off()\n", "}\n", "\n", "if (show_plots) {\n", " polarPlot(frr_df,\n", " pollutant = var_of_interest,\n", " statistic = 'mean',\n", " cols = 'viridis',\n", " limits = c(0, 11),\n", " angle.scale = 45,\n", " par.settings = list(fontsize = list(text = 30)),\n", " units = '',\n", " key.footer = NULL,\n", " )\n", "}\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 9, "id": "410c18fb", "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "pdf: 2" ], "text/latex": [ "\\textbf{pdf:} 2" ], "text/markdown": [ "**pdf:** 2" ], "text/plain": [ "pdf \n", " 2 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Set the regression label and type\n", "regr_label <- 'ch4_co2'\n", "x_name = \"xco2.ppm._wmean_ex5q_divak\"\n", "if (regr_label == 'ch4_co2'){\n", " y_name = \"xch4.ppm._wmean_ex5q_divak\"\n", "} else if (regr_label == 'co_co2'){\n", " y_name = \"xco.ppm._wmean_ex5q_divak\"\n", "}\n", "regr_type <- 'york'\n", "permil = TRUE\n", "\n", "save_plots <- TRUE\n", "show_plots <- FALSE\n", "\n", "# Figure out the right \"good group\" column\n", "good_col <- grep(paste0(\"^\", regr_label, \"_good_group$\"), names(all_rr_df), value = TRUE)\n", "\n", "# Filter safely\n", "frr_df <- all_rr_df %>%\n", " filter(!is.na(.data[[good_col]]))\n", "if (permil) {\n", " frr_df <- frr_df %>%\n", " mutate(across(.cols = grep(\"slope\", names(frr_df), value = TRUE), \n", " .fns = ~ . * 1000))\n", "}\n", "stat_type <- 'slope' # the statistic to plot\n", "var_of_interest <- paste(regr_label,regr_type,stat_type,sep='_') # the variable of interest from the regression label and type\n", "\n", "if (save_plots) {\n", "png(\"/uufs/chpc.utah.edu/common/home/u0890904/LAIR_1/Projects/Ratio_paper_2025_v5/Figures/ha/polar_ch4_co2.png\",width=2000,height=1600,res=300)\n", "polarPlot(frr_df,\n", " pollutant = var_of_interest,\n", " statistic = 'mean',\n", " cols = 'viridis',\n", " limits = c(0, 8.8),\n", " angle.scale = 45,\n", " par.settings = list(fontsize = list(text = 30)),\n", " units = '',\n", " key.footer = NULL,\n", "\n", ")\n", "dev.off()\n", "}\n", "\n", "if (show_plots) {\n", " polarPlot(frr_df,\n", " pollutant = var_of_interest,\n", " statistic = 'mean',\n", " cols = 'viridis',\n", " limits = c(0, 8.8),\n", " angle.scale = 45,\n", " par.settings = list(fontsize = list(text = 30)),\n", " units = '',\n", " key.footer = NULL,\n", " )\n", "}" ] } ], "metadata": { "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "4.4.2" } }, "nbformat": 4, "nbformat_minor": 5 }