簡體   English   中英

更改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

最終情節

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM