3

I'm plotting correlation coefficients r and sample sizes n from a correlation matrix on the same plot (r in the upper triangle, n in the lower triangle). Usually I have no issue with ggplot2 removing my NAs before plotting, but in this situation it's only removing NA values some of the time and I don't know why. I'm using version 3.2.1 of ggplot2.

An example dataframe:

library(ggplot2)
library(dplyr)

ex <- data.frame(x1 = c(rep("2010", 3), rep("2011", 3), rep("2012", 3)),
                 x2 = rep(c("2010", "2011", "2012"), 3),
                 r = c(NA, 0.5, 0.2, NA, NA, 0.1, NA, NA, NA),
                 n = c(NA, NA, NA, 25, NA, NA, 70, 50, NA))

When I run this graph, with size as an aesthetic, NAs are automatically removed from both geoms:

ex %>%
  ggplot(aes(x1, x2))+
  geom_point(aes(size = r))+
  geom_text(aes(label = n))

But, when I run this graph, with color as an aesthetic, NAs are only removed from geom_text, not geom_point:

ex %>%
  ggplot(aes(x1, x2))+
  geom_point(aes(color = r), size = 6)+
  geom_text(aes(label = n))

This stackoverflow question, isn't applicable because I do not want to filter out rows. I also tried changing x1, x2, and r to a character based on this, but it didn't work either

ex %>%
  ggplot(aes(as.character(x1), as.character(x2)))+
  geom_point(aes(color = as.character(r)), size = 6, na.rm = T) +
  geom_text(aes(label = n))

Is there a way to make the color aesthetic ignore NAs?

  • The two geoms don't necessarily have the same aesthetics attached. They inherit whatever's in the initial `ggplot` call, and then they know the ones that you assign in `geom_text` or `geom_point`, but they don't talk between each other. So if r is NA but n isn't, that only affects the point, and vice versa. Some of the issue is that there isn't a clear alternative: for color it makes sense to make a missing value colored gray, but what size would you expect NA to be? – camille Feb 28 '20 at 19:27
  • As an aside, you can use the `each` argument from `rep` as follows: `rep(2010:2012, each = 3)` – markus Feb 28 '20 at 19:27

1 Answers1

3

You can set the na.value value for colour scales. If you set it to NA then the missing points aren't shown:

ex %>%
  ggplot(aes(x1, x2))+
  geom_point(aes(color = r), size = 6)+
  geom_text(aes(label = n)) + 
  scale_color_gradient(na.value = NA)

ggplotwithnacolour

George Savva
  • 4,152
  • 1
  • 7
  • 21