为分类值更改 R 中的默认绘图颜色

Changing default plot colors in R for categorical values

我刚刚编写了下面的情节,我想修改颜色:

library(ggplot2)
library(arm)

df <- tibble::tribble(~Proportion, ~Lower,~Upper, ~Area,~Time,
                      invlogit(-0.2486022), invlogit(-0.654304025), invlogit(0.157099625), "SNP", "Day",
                      0.6878081, ( 0.6878081-0.0961473),(0.6878081+ 0.0961473), "SNP", "Night",
                      invlogit(-0.9417583), invlogit(-1.394725916), invlogit(-0.488790684),"LGCA", "Day",
                      invlogit(-0.1771685), invlogit(-0.630136116),invlogit(0.275799116), "LGCA","Night")
df


dfnew <- df %>% 
  mutate(ymin = Proportion - Lower,
         ymax = Proportion + Upper)

p <-   ggplot(data = dfnew, aes(x = Time, y = Proportion, color=Area)) +

  geom_point(size = 6, stroke = 0, shape = 16) + 
  geom_errorbar(aes(y=Proportion, ymin = Lower, ymax = Upper),width=0.1,size=1)


p<-p+theme(axis.text=element_text(size=15),
             axis.title=element_text(size=20))

p

的确,我希望 SNP 出现在颜色名称 "coral" 中,LGCA 出现在颜色名称 "darkgoldenrod2".

此外,由于误差线相互重叠,我也想稍微移动点和误差线,这样就不会重叠。

我是 R 的新手,所以如果至少有人能指出我正确的方向,那将不胜感激!

提前致谢。

我相信你想要的是以下内容。

scale_color_manual 调用中,您必须手动为每个因子水平分配一个值,如下所示:

p <-   ggplot(data = dfnew, aes(x = Time, y = Proportion, color=Area)) +
  geom_point(size = 6, stroke = 0, shape = 16) + 
  geom_errorbar(aes(y=Proportion, ymin = Lower, ymax = Upper),width=0.1,size=1) + 
  theme(axis.text=element_text(size=15),
           axis.title=element_text(size=20)) +
  scale_color_manual(values = c("SNP" = "coral", 
                                "LGCA" = "darkgoldenrod2"))
p

编辑:我错过了你问题的第二部分,可以使用 geom_pointgeom_errorbar 中的每个 position_dodge 定位误差线和点,使其不重叠,如下所示:

  geom_point(size = 6, stroke = 0, shape = 16, 
             position = position_dodge(width = 0.1)) + 
  geom_errorbar(aes(y=Proportion, ymin = Lower, ymax = Upper),width=0.1,size=1,
                position = position_dodge(width = 0.1)) + 
  theme(axis.text=element_text(size=15),
           axis.title=element_text(size=20)) +
  scale_color_manual(values = c("SNP" = "coral", 
                                "LGCA" = "darkgoldenrod2"))
p