Description
When using geom_dotplot
and specifying a fill
aesthetic, we get a different output depending on whether fill
is specified inside the function aes
or as a separate vector. In the example below, I would expect all points below x~12 to be the same colour in both graphs.
library(tidyverse)
library(patchwork)
# Use 'dose' inside aes
gg1 <- ggplot(ToothGrowth, aes(x = len)) +
geom_dotplot(aes(fill = factor(dose)),
binwidth = .5) +
guides(fill = "none") # Remove legend
# Create separate vector for fill
dot_cols <- as.integer(factor(ToothGrowth$dose))
gg2 <- ggplot(ToothGrowth, aes(x = len)) +
geom_dotplot(fill = dot_cols,
binwidth = .5)
gg1 + gg2
Created on 2022-04-28 by the reprex package (v2.0.1)
As we can see, on the right, there are about 5 points between x=10 and x=12 that should be black but are actually red.
As far as I can tell, this could be a consequence of the data being sorted with respect to x, which means that the rows of the dataset no longer match the elements of the vector dot_cols
. As a proof of concept, I recreated the second graph but I sorted the data first (I also used the group
aesthetic to try to get as close as possible to the original graph):
library(tidyverse)
# Repeat last graph but reorder data first
ToothGrowth2 <- arrange(ToothGrowth, len)
dot_cols2 <- as.integer(factor(ToothGrowth2$dose))
ggplot(ToothGrowth, aes(x = len, group = factor(dose))) +
geom_dotplot(fill = dot_cols2,
binwidth = .5)
Created on 2022-04-28 by the reprex package (v2.0.1)
Now all points below x~12 are black, as expected.