geom_text()在因子排序后删除数据

时间:2018-06-29 11:14:20

标签: r ggplot2

我正在使用geom_segment(R新手)创建甘特图/时间轴。

此代码:

timeline <- ggplot(db, aes(x=Discovery.date, y=Compound.name, label=Compound.name, colour=Compound.name))+
  geom_segment(aes(xend= End.Date, y=Compound.name, yend= Compound.name), size=5)+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
  geom_text(hjust = 'middle', nudge_x = 6, nudge_y= 0.11, colour="black")+
  labs(x= "Discovery Date",
       y= "Compound Name")+
  theme(axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        legend.position="none")+


timeline

返回此图: wrong order

当我使用以下代码更改顺序以反映发现日期时:

db$Compound.name <- factor(db$Compound.name, levels = c("sarkomycin",
                                     "carzinophilin",
                                     "mitomycin C",
                                     "streptozocin",
                                     "chromomycin A3",
                                     "mithramycin",
                                     "bleomycin",
                                     "actinomycin D",
                                     "doxorubicin",
                                     "daunorubicin",
                                     "zinostatin",
                                     "aclarubicin",
                                     "peplomycin",
                                     "epirubicin HCl",
                                     "pirarubicin",
                                     "idarubicin HCl",
                                     "pentostatin",
                                     "zinostatin stimalamer",
                                     "valrubicin",
                                     "amrubicin HCl",
                                     "temsirolimus"))

db $ Compound.name <-factor(db $ Compound.name,level = rev(levels(db $ Compound.name)))

结果: correct order 这会更改顺序,但是会丢失Compound.name = pirarubicin(从顶部起y = 7),并且在顶部变成灰色部分。给出的原因:

  

删除了1行包含缺失值的行(geom_text)

仅数据顺序已更改,并且完全在xlim之内。

任何帮助或建议将不胜感激。

示例代码:

Compound.name <- c("sarkomycin",
                   "carzinophilin",
                   "mitomycin C",
                   "streptozocin",
                   "chromomycin A3",
                   "mithramycin",
                   "bleomycin",
                   "actinomycin D",
                   "doxorubicin",
                   "daunorubicin",
                   "zinostatin",
                   "aclarubicin",
                   "peplomycin",
                   "epirubicin HCl",
                   "pirarubicin",
                   "idarubicin HCl",
                   "pentostatin",
                   "zinostatin stimalamer",
                   "valrubicin",
                   "amrubicin HCl",
                   "temsirolimus")
Discovery.date <- c("1954",
                 "1956",
                 "1958",
                 "1960",
                 "1961",
                 "1961",
                 "1962",
                 "1964",
                 "1972",
                 "1975",
                 "1976",
                 "1981",
                 "1981",
                 "1984",
                 "1988",
                 "1990",
                 "1992",
                 "1994",
                 "1999",
                 "2002",
                 "2007")

End.Date <- c("1965",
              "1970",
              "2018",
              "2018",
              "1960",
              "2000",
              "2018",
              "2018",
              "2018",
              "2018",
              "1985",
              "2018",
              "2018",
              "2018",
              "2018",
              "2018",
              "2018",
              "2018",
              "2018",
              "2018",
              "2018")

df<- data.frame(Compound.name, Discovery.date, End.Date)

1 个答案:

答案 0 :(得分:0)

label参数移出ggplot美学(aes)并移入geom_text美学。 (我也将所有主题参数都移到了一个块中,以使其更易于阅读。)

ggplot(df, aes(x=Discovery.date, y=Compound.name, colour=Compound.name))+  # OUT OF HERE
  geom_segment(aes(xend= End.Date, y=Compound.name, yend= Compound.name), size=5)+
  geom_text(aes(label=Compound.name), # INTO HERE
                hjust = 'middle', nudge_x = 6, nudge_y= 0.11, colour="black")+  
    labs(x= "Discovery Date", y= "Compound Name")+
    theme(axis.text.y=element_blank(),
          axis.ticks.y=element_blank(),
          legend.position="none",
          panel.grid.major = element_blank(), 
          panel.grid.minor = element_blank())