Search code examples
rggplot2fillaesthetics

ggplot aes fill not working for no discernible reason


I have made ggplot figures hundreds, if not thousands of times, and set the fill to be based on a variable in the df using aes about 99% of the time. For some reason the fill color is not working (points are black every time) and I cannot figure out why, there is nothing clear to me in the code. I've tried pretty much every tweak or adjustment to the code I can think of, removing line or sections one at a time to test what part is throwing things off. Even if I just run the first 2 lines, I get no fill color other than black. Any thoughts?

ggplot(mean_score_by_type, aes(x = Inference, y = Direct, fill=as.factor(Age_Group))) +
  geom_point(alpha=0.7, size=4, stroke=1, position=position_jitter(width=0.05, height=0.05)) +
  scale_fill_discrete(name = "Age (Years)") +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_y_continuous(breaks=seq(0,1,0.25), limits=c(0.25,1.05)) +
  labs(y="Direct", x="Inference", title="Proportion of Correct Responses") +
  geom_vline(xintercept=.56, color="red", linetype=2) + 
  geom_hline(yintercept=.56, color="blue", linetype=2) +
  facet_wrap(~Group)

A few lines of data:

 Participant Age_Group Control-ID Control-MC Direct Inference   Group
        1         6      1.000      1.000     1.00      0.75 Under 7
        2         6      1.000      1.000     1.00      0.00 Under 7
        3         4      1.000      1.000     1.00      0.50 Under 7
        4         4      0.875      0.625     1.00      0.25 Under 7
        5         6      1.000      1.000     1.00      0.25 Under 7
        23        7      1.000      0.750     0.50      1.00  Over 7
        24        8      1.000      1.000     1.00      1.00  Over 7
        26        8      1.000      1.000     1.00      1.00  Over 7
        27        7      1.000      1.000     1.00      1.00  Over 7
        28        7      1.000      1.000     1.00      1.00  Over 7

The especially strange part is that the code below works just fine - the code is identical for the shape specifications. I don't know why the fill color works as it should with the shape added, but comes out black without the shape aesthetic.

ggplot(mean_score_by_type, aes(x = Inference, y = Direct, fill=as.factor(Age_Group), shape=Group)) +
  geom_point(alpha=0.7, size=4, stroke=1, position=position_jitter(width=0.05, height=0.05)) +
  scale_shape_manual(values=c(21, 23)) +
  scale_fill_discrete(name = "Age (Years)") +
  guides(shape = FALSE, fill = guide_legend(override.aes = list(shape=c(21,21,21,23,23,23)))) +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_y_continuous(breaks=seq(0,1,0.25), limits=c(0.25,1.05)) +
  labs(y="Direct", x="Inference", title="Proportion of Correct Responses") +
  geom_vline(xintercept=.56, color="red", linetype=2) + 
  geom_hline(yintercept=.56, color="blue", linetype=2)

Solution

  • You need to specify a different character type. The default character type ggplot chooses does not have a "fill." If you add pch = 21 (or some other type with a fill) to the geom_point() function, this should solve your problem.

    Alternatively, you could keep the default character type, and specify color = as.factor(Age_Group) instead.