-2

I'm using ggplot to graph the bias of the mean temperature in South America.

In order to graph this, I'm using the following code:

ggplot(bias.df2) +  
  geom_tile(aes(x=x, y=y, fill=pr), alpha=0.8) + 
  scale_fill_viridis(na.value="white") +
  coord_equal() +
  theme(legend.position="bottom") +
  theme(legend.key.width=unit(2, "cm"))+
  scale_color_continuous(limits=c(-10,10),breaks=brkbias)

Which generates the following color bar:

ggplot legend

But I need the color bar to look like this:

Grads legend

I've tried modifying the limits of the colourbar but nothing seems to generate the kind of plot that I need. Would really appreciate any help to solve this.

EDIT: The data I'm using can be found in this csv file.

jnava1612
  • 71
  • 8
  • 1
    Please share your data using `dput`? – Quinten Jun 01 '22 at 14:59
  • @Quinten I edited the post and included the data I'm using. – jnava1612 Jun 01 '22 at 15:06
  • 1
    What part of the second colourbar would you like? The discreteness of the colours or the triangle ends? Triangle ends are discussed [here](https://stackoverflow.com/q/68440366/11374827). – teunbrand Jun 01 '22 at 15:09
  • I was trying to get both the discreteness and the triangles. – jnava1612 Jun 01 '22 at 15:29
  • @teunbrand while using your suggestion I was able to get the triangles in the scale, but I can't get the descretization of the scale values and colors, as the colors in the new scale aren't being applied to the graph. The resulting graph looks like [this](https://drive.google.com/file/d/1ad3tRg_kzNJm0KlC-7KsMVtlSqQm-JSg/view?usp=sharing). – jnava1612 Jun 01 '22 at 16:07
  • Yeah I suppose you'd have to do that manually. I'm currently looking into making these types of guides in an easier way, but haven't gotten around to discrete ones yet. – teunbrand Jun 01 '22 at 16:16
  • 1
    you can also [build the guide as a separate plot](https://stackoverflow.com/a/50540633/7941188) – tjebo Jun 01 '22 at 16:23

1 Answers1

5

guide_colorsteps() can be used in combination with the breaks = argument of scale_fill_viridis_c() to print a discrete colorbar with the continuous fill. We can modify the code linked in the comments to add the triangles to that guide. Since you have a horizontal bar, it requires moving and pointing the triangles horizontally instead of vertically.

library(ggplot2)
library(gtable)
library(grid)

bias.df2 <- read.csv("~/Downloads/data.csv")

my_triangle_colorsteps <- function(...) {
  guide <- guide_colorsteps(...)
  class(guide) <- c("my_triangle_colorsteps", class(guide))
  guide
}

guide_gengrob.my_triangle_colorsteps <- function(...) {
  # First draw normal colorsteps
  guide <- NextMethod()
  # Extract bar / colours
  is_bar <- grep("^bar$", guide$layout$name)
  bar <- guide$grobs[[is_bar]]
  extremes <- c(bar$gp$fill[1], bar$gp$fill[length(bar$gp$fill)])
  # Extract size
  width  <- guide$widths[guide$layout$l[is_bar]]
  height <- guide$heights[guide$layout$t[is_bar]]
  short  <- min(convertUnit(width, "cm",  valueOnly = TRUE),
                convertUnit(height, "cm", valueOnly = TRUE))
  # Make space for triangles
  guide <- gtable_add_cols(guide, unit(short, "cm"),
                           guide$layout$t[is_bar] - 1)
  guide <- gtable_add_cols(guide, unit(short, "cm"),
                           guide$layout$t[is_bar])

  left <- polygonGrob(
    x = unit(c(0, 1, 1), "npc"),
    y = unit(c(0.5, 1, 0), "npc"),
    gp = gpar(fill = extremes[1], col = NA)
  )
  right <- polygonGrob(
    x = unit(c(0, 1, 0), "npc"),
    y = unit(c(0, 0.5, 1), "npc"),
    gp = gpar(fill = extremes[2], col = NA)
  )
  # Add triangles to guide
  guide <- gtable_add_grob(
    guide, left, 
    t = guide$layout$t[is_bar],
    l = guide$layout$l[is_bar] - 1
  )
  guide <- gtable_add_grob(
    guide, right,
    t = guide$layout$t[is_bar],
    l = guide$layout$l[is_bar] + 1
  )
  
  return(guide)
}

ggplot(bias.df2) +  
  geom_tile(aes(x=x, y=y, fill=pr), alpha=0.8) + 
  scale_fill_viridis_c(na.value="white", limits = c(-10, 10),
                       breaks = c(-10, -7, -4, -1, -.5, .5, 1, 4, 7, 10), 
                       guide = my_triangle_colorsteps(show.limits = TRUE)) +
  coord_equal() +
  theme(legend.position="bottom") +
  theme(legend.key.width=unit(2, "cm"))

Created on 2022-06-01 by the reprex package (v2.0.1)

lhs
  • 1,018
  • 2
  • 11