0

There is a very similar question here: Add NA value to ggplot legend for continuous data map.

I tried to understand it, but I didn't manage to make it work for my data. So I created a super simple example. I have this data:

set.seed(1)
df = data.frame(a=rnorm(50), b=rnorm(50), c=rep(1:5, 10))
df[sample(1:50, 10), ]$c = NA

where all columns are numeric. Now I'd like to make a ggplot with a legend entry for the NA-values. When I do the following:

ggplot(df) +
  geom_point(
    aes(x = a, y =b, col=c)
  )

This is the result

enter image description here

What I want is something like this (when c is a a factor it gets automatically an entry):

ggplot(df) +
  geom_point(
    aes(x = a, y =b, col=factor(c))
  )

enter image description here

Could I achieve more or less easy similar results and keep my values in class numeric?

Carl
  • 4,232
  • 2
  • 12
  • 24
Robin Kohrs
  • 655
  • 7
  • 17
  • I don't think so; you're trying to have a color scale that is both continuous (c) and discrete (is.na(c)) at the same time. Could you use shape to indicate NA values instead? – CFD Mar 31 '21 at 14:23

1 Answers1

0

Defining a color for NA is easy by adding scale_color_continuous(na.value="red"), but it is not explicitly labeled in the legend.

To achieve that you could add a second color scale just for the NA value using ggnewscale:

library(ggplot2)
library(ggnewscale)
set.seed(1)
df = data.frame(a=rnorm(50), b=rnorm(50), c=rep(1:5, 10))
df[sample(1:50, 10), ]$c = NA
na.value.forplot <- 'red'
ggplot(df) +
  geom_point(aes(x = a, y =b, col=c)) +
  scale_color_continuous(guide = guide_colorbar(order = 2)) +
  new_scale_color() +
  geom_point(data=subset(df, is.na(c)),
    aes(x=a, y=b, col="red")) +
  scale_color_manual(name=NULL, labels="NA", values="red")

Created on 2021-03-31 by the reprex package (v1.0.0)

user12728748
  • 8,106
  • 2
  • 9
  • 14