Many functions in the dendroTools R package provide built-in plotting methods for quick inspection and interpretation of results. These plots are created with ggplot2, therefore the returned plot is a ggplot object. This is very convenient because users can directly modify the default dendroTools plot by adding any ggplot2 layers (themes, scales, labels, annotations, etc.) with the + operator.
In this vignette I demonstrate a basic workflow:
1) calculate a daily_response() example,
2) create a default plot with plot(),
3) polish the plot using ggplot2,
4) build a similar heatmap from scratch by extracting calculated values from the returned object.
All data used below is included in the dendroTools package.
# Load packages library(dendroTools) library(ggplot2) # Load example data data(data_MVA) data(LJ_daily_temperatures) # Run daily_response() example_basic <- daily_response(response = data_MVA, env_data = LJ_daily_temperatures, row_names_subset = TRUE, lower_limit = 35, upper_limit = 45, remove_insignificant = FALSE, previous_year = FALSE, reference_window = "end")
The simplest way to visualize the results is to use the generic plot() method.
plot(example_basic)
Because plot(example_basic) returns a ggplot object, it can be modified directly. In this example I:
- set a diverging colour scale and fix the limits to -1 and 1,
- apply a minimal theme,
- move the legend to the bottom.
plot(example_basic) + scale_fill_gradient2( name = "cor", low = "blue", mid = "white", high = "red", na.value = "white", limits = c(-1, 1) # select min-max here ) + theme_minimal() + theme(panel.background = element_blank(), plot.background = element_blank(), plot.title = element_blank(), legend.position = "bottom" )
Here is another example with renamed axis labels and rotated x-axis labels.
plot(example_basic) + scale_fill_gradient2( name = "Correlation", low = "blue", mid = "white", high = "red", na.value = "white", limits = c(-1, 1) ) + labs(x = "Season end (DOY)", y = "Season length (days)") + theme_bw() + theme(legend.position = "bottom", axis.text.x = element_text(angle = 45, hjust = 1))
Sometimes you may want complete control over the plot (e.g., different geometries, custom annotations, combining multiple plots, etc.). In such cases you can extract the computed values from the returned dendroTools object and create your own plot.
For daily_response() outputs, the calculated values are stored in object$calculations. The code below converts this matrix-like object to a long format suitable for geom_tile().
# Extract calculations (correlation table) from the dmrs object cor_mat <- example_basic$calculations # Convert matrix-like object to long format using base R melted <- as.data.frame(as.table(as.matrix(cor_mat))) colnames(melted) <- c("season_length", "season_end", "value") # Convert labels such as "X35" into numeric values (if present) melted$season_end <- as.numeric(gsub("X", "", melted$season_end)) melted$season_length <- as.numeric(gsub("X", "", melted$season_length)) # Remove NA values (if any) melted <- melted[!is.na(melted$value), ] summary(melted)
ggplot(melted, aes(x = season_end, y = season_length, fill = value)) + geom_tile() + scale_y_continuous(expand = c(0, 0)) + scale_x_continuous(expand = c(0, 0)) + scale_fill_gradient2( name = "cor", low = "blue", mid = "white", high = "red", na.value = "white", limits = c(-1, 1) ) + xlab("Season end") + ylab("Season Length") + theme_bw() + theme(legend.position = "bottom")
+. plot(object) and then polish it with ggplot2 scales and themes. object$calculations and plotted from scratch using ggplot() and geom_tile().monthly_response(), monthly_response_seascor(), daily_response_seascor(). Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.