I am looking to enhance my plot in R by adding arrows and labels to the colorbar legend, similar to what is depicted in the provided image. I am curious about the specific steps or functions I can utilize in R to achieve this customization. Could you please guide me through the process or provide a sample code snippet that demonstrates how to incorporate arrows and labels within the colorbar legend in R?
Here's my code, I can't draw the colorbar and arrows in the same picture, I would like to know how to change my code.
ggpolar(pole = "N", max.lat = 90, min.lat = 30, 
    land.fill.colour = "transparent", land.outline.colour = "#343434", 
    plt.lat.labels = FALSE,size = 0.8) +
ggtitle("C") +
theme(plot.title = element_text(hjust = 0, family = "Arial", size = 18,face = "bold")) +
geom_tile(data = ae_raster_S3_df, aes(x = lon, y = lat, fill = change_ratio),alpha = 0.8) + 
geom_point(data = result_df_s3, aes(x = lon, y = lat), color = "#343434", size = 0.5) +
scale_fill_stepsn(colors = custom_colors,
                breaks = break_points, 
                labels = c("-5", "-4", "-3", "-2", "-1", "0", "1", "2", "3", "4", "5"),
                limits = c(-6, 6),
                name = expression(AE~('%'~ decade^{-1})),
                na.value = "transparent",
                guide = guide_colorbar(barwidth = 1, barheight = 25, direction = "vertical",
                                       title.position = "right", title.hjust=0.5, label.position = "right", title.theme = element_text(family = "Arial", size = 16, angle = 90),label.theme = element_text(family = "Arial", size = 12))) +
theme(
plot.background = element_rect(fill = NA, color = NA),
panel.background = element_rect(fill = NA, color = NA),
legend.position = "right",
legend.key.width = unit(2, "line"),
legend.key.height = unit(0.5, "line"),
legend.title = element_text(family = "Arial", size = 12),
legend.text = element_text(size = 12),
axis.title = element_blank(),
axis.text = element_text(family = "Arial", size = 12),
axis.ticks = element_blank()
)
ggdraw() +
draw_text("Decreasing", x = 0.65, y = 0.66, hjust = 0, family = "Arial",color = "#D15547", size = 16, angle=90) +
draw_text("Increasing", x = 0.65, y = 0.14, hjust = 0, family = "Arial",color = "#3C87BD", size = 16, angle=90) +
draw_text("Seasonality", x = 0.8, y = 0.4, hjust = 0, family = "Arial",color = "black", size = 16, angle=90) +
draw_line(x = c(0.5, 0.5), y = c(0.4, 0.1), 
        arrow = arrow(length = unit(0.1, "inches")), color = "#3C87BD", size = 1) +
draw_line(x = c(0.5, 0.5), y = c(0.6, 0.9), 
        arrow = arrow(length = unit(0.1, "inches")), color = "#D15547", size = 1)
                One option to achieve your desired result would be to use guide_custom which was introduced in ggplot2 >= 3.5.0 and allows to add a manual or custom legend to a ggplot where the custom legend has to be provided as a grob.
To create the custom legend I use grid and gtable for the layout.
Using a minimal reproducible example based on ggplot2::mpg:
library(ggplot2)
library(gtable)
library(grid)
column1 <- polylineGrob(
  x = rep(0, 4),
  y = c(.4, .05, 0.6, 0.95),
  id = c(1, 1, 2, 2),
  gp = gpar(
    col = c("#D15547", "#3C87BD"),
    lwd = 2
  ),
  arrow = arrow(length = unit(0.1, "inches"))
)
column2 <- textGrob(
  label = c(
    "Decreasing", "Increasing", "Seasonality"
  ),
  y = unit(c(0.225, .775, .5), "npc"),
  x = unit(c(0, 0, 1), "npc"),
  gp = gpar(
    col = c("#D15547", "#3C87BD", "black"),
    fontsize = 16
  ),
  hjust = c(.5, .5, .5),
  vjust = c(1, 1, .75),
  rot = -90
)
# Setup the gtable layout
legend <- gtable(
  # Widths of columns.
  widths = unit(c(.8, .8), c("cm", "cm")),
  heights = unit(10, "cm")
)
# Add the grobs
legend <- gtable_add_grob(
  legend,
  grobs = list(column1, column2),
  t = c(1, 1),
  l = c(1, 2),
  clip = "off"
)
guide <- guide_colorbar(
  barwidth = 1,
  barheight = unit(10, "cm"),
  direction = "vertical",
  title.position = "right", title.hjust = 0.5, label.position = "right",
  title.theme = element_text(family = "Arial", size = 16, angle = 90),
  label.theme = element_text(family = "Arial", size = 12)
)
base <- ggplot(mpg, aes(displ, hwy, colour = cty)) +
  geom_point() +
  labs(
    x = "Engine displacement",
    y = "Highway miles per gallon",
    col = "City miles per gallon"
  ) +
  theme(axis.line = element_line())
base +
  scale_colour_viridis_c(
    guide = guide
  ) +
  guides(
    custom = guide_custom(
      legend
    )
  ) +
  theme(
    legend.box = "horizontal"
  )

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With