Reputation: 33
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))
Upvotes: 3
Views: 1912
Reputation: 5336
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)
Upvotes: 3