I have NA values in a data set, which I would like to include in my ggplot as well as in the legend. I thought this would be easily done by specifying the na.values="somecolour"
option, as shown e.g. in this post. However, for my example the code runs without plotting any of the NAs, nor including an entry in the legend. Instead rows with missing values are automatically removed. Here's some code for illustration:
set.seed(42)
lat <- rnorm(10, 54, 12)
long <- rnorm(10, 44, 12)
val <- rnorm(6, 10, 3)
val <- c(val,NA,NA,NA,NA)
df <- as.data.frame(cbind(long, lat, val))
library(ggplot2)
library(scales)
ggplot() +
geom_point(data=df, aes(x=lat, y=long, size=val, fill=val),shape=21,alpha=0.6) +
scale_size_continuous(range = c(2, 12), breaks=pretty_breaks(4)) +
scale_fill_distiller(direction = -1, palette="RdYlBu", breaks=pretty_breaks(4),na.value = "black") +
guides(fill = guide_legend(), size = guide_legend()) +
theme_minimal()
What am I doing wrong?