Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Make legend for scale_fill_continuous continuous scale instead of discrete

I’m using the function of scale_fill_gradientn() which normally should produce a continuous scale as the legend but somehow if I use the following code attached below the outcome produces a legend with boxes of colours as discrete values

  ggplot(aes(x=grouping,y=person,fill=score)) + 
  geom_tile(stat = "identity",position = "identity",colour="black",size=1) + 
  facet_grid(family~functional,switch = "y") +
  scale_fill_gradientn(colours = c("white","orange","red"),
    limits=c(0,100),breaks = c(0,50,100),labels=c("min","mid","max"),
    guide = guide_legend(direction = "horizontal",title.position = "bottom")) +
    labs(fill = "Average Percentage")

enter image description here

I notice that as long as I remove the guide argument from the scale_fill_gradientn() function it would give me back a continuous scale i.e.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

scale_fill_gradientn(colours = c("white","orange","red"),
    limits=c(0,100),breaks = c(0,50,100),labels=c("min","mid","max"))

enter image description here

But then my question would be why does this occur and if I have to refrain from using the guide argument how can I add a black border around the scale (like the picture above with the discrete colour boxes) as well as adjust the positioning of the title and labels e.g. put the legend title below the colour bar and labels above the colour bar? Thanks a lot!

>Solution :

Here is a potential solution using the palmer penguins dataset as a minimal, reproducible example:

library(tidyverse)
library(palmerpenguins)

penguins %>% 
  na.omit() %>%
  ggplot(aes(x = body_mass_g, y = bill_length_mm, fill = flipper_length_mm)) +
  geom_point(shape = 21, size = 3) +
  scale_fill_gradientn(colours = c("white", "orange", "red"),
                       limits = c(170, 240), breaks = c(180, 205, 235),
                       labels = c("min", "mid", "max"),
                       guide = guide_legend(direction = "horizontal",
                                            title.position = "bottom"))


penguins %>% 
  na.omit() %>%
  ggplot(aes(x = body_mass_g, y = bill_length_mm, fill = flipper_length_mm)) +
  geom_point(shape = 21, size = 3) +
  scale_fill_gradientn(colours = c("white", "orange", "red"),
                       limits = c(170, 240), breaks = c(180, 205, 235),
                       labels = c("min", "mid", "max"),
                       guide = guide_colourbar(direction = "horizontal",
                                               title.position = "bottom"))

Created on 2022-07-25 by the reprex package (v2.0.1)

Does this solve your problem?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading