列颜色与自定义比例不匹配

时间:2019-07-02 10:02:17

标签: ggplot2

我整理了一个小图,给出了上颌骨和下颌骨表型的计数。我想要上颌骨是黑色的,下颌骨是灰色的。我为填充选项创建了一个比例尺颜色变量,但运气不好,颜色仍然乱序。

我已经在许多其他条形图上成功使用了此代码,但是这里没有运气。我相信这是因为我使用rbind将3个数据帧合并为一个,但是合并后的数据帧的结构与未合并的数据帧没有区别,后者可以正常工作。

前四个条应为黑色,后四个条应为灰色。

### 3 data sets

    a<-data.frame(
      row.names = c("RI2_MAX_E1","ri2_mand_E1","rc1_mand_E1"),
      count = c(2,2,2),
      labels = c("RI2", "ri2", "rc1")
    )

    b<-data.frame(
      row.names = c("RP3_MAX_E1","RP4_MAX_E1","rp3_mand_E1"),
      count = c(3,3,2),
      labels = c("RP3", "RP4", "rp3")
    )

    c<-data.frame(
      row.names = c("RM3_MAX_E1","rm3_mand_E1"),
      count = c(5,6),
      labels = c("RM3", "rm3")
    )

    ### Bind datasets into 1
    E1.bind<-rbind(a,b,c)

    ### order variables
    E1.bind$labels<-factor(E1.bind$labels, levels =c("RI2","RP3","RP4","RM3","ri2","rc1","rp3","rm3"))

    ### Custom scale color
    E1.color<-c("black","black","black","black","grey","grey","grey","grey")

    ### plot
    ggplot(data= E1.bind, aes(x=E1.bind$labels, y=E1.bind$count,fill=E1.color)) +
      geom_bar(stat="identity") +
      xlab("Teeth") + ylab("Phenotype counts") +
      ggtitle("Teeth With Greatest Number of Phenotypes - Element 1")+
      scale_fill_manual(name="",
                        labels = c("Maxilla","Mandible"),
                        values = c("black","grey"))+
      scale_x_discrete(labels = c("RI2","RP3","RP4","RM3","ri2","rc1","rp3","rm3")) +
      scale_y_continuous(breaks = seq(0,6,1)) +
      theme_classic()+
      theme(legend.position="top")+
      theme(plot.title = element_text(hjust = 0.5))


1 个答案:

答案 0 :(得分:0)

您的E1.bind如下所示:

E1.bind
            count labels
RI2_MAX_E1      2    RI2
ri2_mand_E1     2    ri2
rc1_mand_E1     2    rc1
RP3_MAX_E1      3    RP3
RP4_MAX_E1      3    RP4
rp3_mand_E1     2    rp3
RM3_MAX_E1      5    RM3
rm3_mand_E1     6    rm3

请注意标签的顺序。然后,您将其用作fill

E1.color<-c("black","black","black","black","grey","grey","grey","grey")

一种更好的方法是在用于定义填充颜色的数据框中添加Type。这样一来,它的扩展性也更高:

library(dplyr)
library(ggplot2)

a<-data.frame(
  row.names = c("RI2_MAX_E1","ri2_mand_E1","rc1_mand_E1"),
  count = c(2,2,2),
  labels = c("RI2", "ri2", "rc1")
)

b<-data.frame(
  row.names = c("RP3_MAX_E1","RP4_MAX_E1","rp3_mand_E1"),
  count = c(3,3,2),
  labels = c("RP3", "RP4", "rp3")
)

c<-data.frame(
  row.names = c("RM3_MAX_E1","rm3_mand_E1"),
  count = c(5,6),
  labels = c("RM3", "rm3")
)

### Bind datasets into 1
E1.bind<-rbind(a,b,c)
E1.bind$Type <- ifelse(grepl('R.*', E1.bind$labels), "Maxilla", "Mandible")

### Sort by Type
E1.bind <- arrange(E1.bind, desc(Type))

### plot
ggplot(data= E1.bind, aes(x=labels, y=count, fill=Type)) +
  geom_bar(stat="identity") +
  xlab("Teeth") + ylab("Phenotype counts") +
  ggtitle("Teeth With Greatest Number of Phenotypes - Element 1") +
  scale_y_continuous(breaks = seq(0,6,1)) +
  scale_x_discrete(limits=E1.bind$labels) + 
  scale_fill_manual(values = c("grey", "black")) +
  theme_classic()+
  theme(legend.position="top")+
  theme(plot.title = element_text(hjust = 0.5))

这导致: enter image description here