如何用R的par()命令修改绘图调色板?glmtools调色板调整求助
plot_temp() Hey there! I totally get it—default color palettes can feel pretty underwhelming when you’re trying to make your thesis figures look polished. Let’s work through how to swap out that lackluster default in glmtools::plot_temp() since the function doesn’t have a dedicated palette parameter listed in its docs.
First, Let’s Break Down the Problem
The plot_temp() help docs mention extra arguments get passed to par(), but par() doesn’t control the colormap for heatmap-style plots like this. Under the hood, plot_temp() is almost certainly using a hardcoded color palette (like heat.colors() or terrain.colors()) in its underlying plotting function (probably image() or filled.contour()). That’s why modifying par() alone isn’t working—we need to override that hardcoded value.
Solution 1: Try Passing a col Parameter Directly
Even though it’s not listed in the docs, sometimes functions sneak extra arguments through to their internal plotting calls. Give this quick fix a shot first:
# Load a package with professional-looking palettes (e.g., viridis or RColorBrewer) library(viridis) # Call plot_temp with your custom color palette plot_temp(your_model_output, col = viridis(100)) # 100 = number of color levels to use
If that doesn’t work (since the docs specify args go to par()), move on to Solution 2.
Solution 2: Create a Custom Version of plot_temp()
We can copy the original function and tweak just the color part to use our preferred palette. Here’s how:
- View the original function code to find where the color is set:
# Print the full code of plot_temp to identify the color line print(glmtools::plot_temp)
Look for a line that sets the col argument—something like col = heat.colors(n_levels) or similar.
- Copy and modify the function:
# Load your favorite palette package library(viridis) # Make a copy of the original plot_temp function my_plot_temp <- glmtools::plot_temp # Replace the hardcoded palette line (adjust the line number to match what you found) body(my_plot_temp)[[15]] <- quote(col = viridis(100)) # Swap 100 for your desired number of color levels # Now use your custom function instead of the original my_plot_temp(your_model_output)
Solution 3: Set a Global Palette (Last Resort)
If modifying the function feels too involved, you can set a global color palette that all plotting functions will use by default:
library(viridis) # Set the global palette to viridis palette(viridis(10)) # Now call plot_temp—this might work if the function relies on the global palette instead of hardcoding plot_temp(your_model_output)
Note: This changes the palette for all plots in your session, so you can reset it later with palette("default") if needed.
Quick Double-Check: glmtools Installation
Just to ensure you’re running the latest version (in case the function was updated with a palette parameter), confirm your install steps are correct:
# Install devtools if you haven’t already if (!require(devtools)) install.packages("devtools") library(devtools) # Install dependencies and glmtools install_github("GLEON/GLMr") install_github("USGS-R/glmtools") library(glmtools)
Let me know if any of these steps work for you—happy to adjust things further if needed!
内容的提问来源于stack exchange,提问作者Daniel Valencia C.

