13

Using the guide_legend argument, even without specifiying any further arguments, changes my legend from a continuous legend to a discrete one. I need to correct this (e.g. to use this: Add a box for the NA values to the ggplot legend for a continous map and then order the legends.)

df <- expand.grid(X1 = 1:10, X2 = 1:10)
df$value <- df$X1 * df$X2

ggplot(df, aes(X1, X2)) + 
  geom_tile(aes(fill = value))

The Legend on the right side is continuous

ggplot(df, aes(X1, X2)) + 
  geom_tile(aes(fill = value))+
  scale_fill_continuous(guide = guide_legend())

The legend now is discrete

The same happens, if I specify the argument when adding it as a + guides(fill = guide_legend())

Any ideas how to make sure the legend stays unchanged, so that I can use e.g. the order argument.

Thanks!

3
  • 5
    try guide_colourbar() instead of guide_legend()
    – Ilkyun Im
    Commented Apr 22, 2020 at 0:12
  • 1
    "Working as intended" here: guide_legend() is used for discrete scales, whereas guide_colorbar() is the version to use for continuous scales. They should both all accept the same/similar list of arguments, such as order=.... Commented Apr 22, 2020 at 15:57
  • Fantastic, many thanks to both of you! This does the trick. The naming seems odd to me though - the guide_legend sounds like it would be a general command. Oh well :) Commented Apr 23, 2020 at 8:31

1 Answer 1

13

Thanks to Ilkyun Im and chemdork123 for providing me with the answers.

The right command here would be guide_colorbar().

So it would be:

ggplot(df, aes(X1, X2)) + 
  geom_tile(aes(fill = value))+
  scale_fill_continuous(guide = guide_colorbar())

I still find it odd that the guide_legend() is not a general command, but specific to discrete legends. Oh well :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.