首页 文章

ggplot aes填写不起作用没有明显的原因

提问于
浏览
1

我已经制作了数百个ggplot数字,如果不是数千次,并且使用aes约99%的时间将填充设置为基于df中的变量 . 由于某种原因,填充颜色不起作用(每次点都是黑色的)我无法弄清楚原因,代码中没有任何清楚的东西 . 我已经尝试了几乎每一个调整或调整我能想到的代码,一次删除一行或一个部分来测试哪些部分抛弃了东西 . 即使我只运行前两行,我也没有黑色以外的填充颜色 . 有什么想法吗?

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)

几行数据:

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

特别奇怪的是,下面的代码工作得很好 - 代码对于形状规范是相同的 . 我不知道为什么填充颜色的效果应该与添加的形状一致,但是在没有形状美感的情况下会出现黑色 .

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)

1 回答

  • 3

    您需要指定不同的字符类型 . ggplot选择的默认字符类型没有"fill."如果将 pch = 21 (或带填充的其他类型)添加到geom_point()函数,这应该可以解决您的问题 .

    或者,您可以保留默认字符类型,并指定 color = as.factor(Age_Group) .

相关问题