RPubs will retire in June 2027. Your existing documents will stay accessible through December 31, 2031
and Connect Cloud is the recommended home for new publishing. Read the blog post
gravatar

jstremblay

Jack Shelley-Tremblay

Recently Published

Plot
ARC2TEST
ELP Data for Predicting RT
New metric Count as compared to other variables in the ELP.
Cows are cool
Psycholinguistic plot
Plot
# ========================================================= # 13. Relabel axes with indexed variable names # ========================================================= # --------------------------------------------------------- # 1. Create ordered variable index # --------------------------------------------------------- var_names <- colnames(cor_mat) var_index_df <- data.frame( variable = var_names, index = seq_along(var_names), y_label = paste0(var_names, " (", seq_along(var_names), ")"), x_label = paste0("(", seq_along(var_names), ")"), stringsAsFactors = FALSE ) # --------------------------------------------------------- # 2. Merge labels into plotting data # --------------------------------------------------------- heat_sig_df_labeled <- heat_sig_df %>% dplyr::left_join(var_index_df, by = c("Var1" = "variable")) %>% dplyr::rename( Var1_index = index, Var1_label = y_label ) %>% dplyr::left_join(var_index_df, by = c("Var2" = "variable")) %>% dplyr::rename( Var2_index = index, Var2_label = x_label ) # --------------------------------------------------------- # 3. Convert to ordered factors using index # --------------------------------------------------------- heat_sig_df_labeled <- heat_sig_df_labeled %>% dplyr::mutate( Var1_label = factor(Var1_label, levels = var_index_df$y_label), Var2_label = factor(Var2_label, levels = var_index_df$x_label) ) # --------------------------------------------------------- # 4. Plot with new labels # --------------------------------------------------------- p_sig_labeled <- ggplot(heat_sig_df_labeled, aes(x = Var2_label, y = Var1_label, fill = r)) + geom_tile(color = "white") + geom_text(aes(label = sprintf("%.2f", r)), size = 3) + scale_fill_gradient2( low = "blue", mid = "white", high = "red", midpoint = 0, limits = c(-1, 1), name = "r" ) + theme_minimal(base_size = 12) + theme( axis.text.x = element_text(angle = 0, hjust = 0.5), axis.text.y = element_text(size = 9), panel.grid = element_blank() ) + labs( title = "Significant Correlations Only (p < .05)", x = "Variable Index", y = "Variables" ) + coord_fixed() # --------------------------------------------------------- # 5. Save # --------------------------------------------------------- ggsave( filename = "Derived_Measures_Correlation_Heatmap_SIGNIFICANT_ONLY_LABELED.png", plot = p_sig_labeled, width = 14, height = 12, dpi = 300 ) # Display print(p_sig_labeled)