ggplot geom_tile,删除垂直白线

时间:2018-08-20 15:41:25

标签: r ggplot2

我有以下代码来获取下面的图像,我正在寻找一种方法来消除这些白色条纹,或者至少使其在每一行和每一列中保持一致。有没有办法做到这一点? (也是,造成它们的原因是什么?)

ttfCountCompleted <- tibble(`Production Date` = c(rep(as.Date("2013-09-01"),4), rep(as.Date("2013-10-01"),4), rep(as.Date("2013-11-01"),4) ),
                        `Months in Service` = c(rep(1:4,3)),
                        `nServ` = 1:12)

textcol <- "black"
ggplot(ttfCountCompleted, 
       aes(x = `Production Date`, 
           y = `Months in Service`,
           fill=`nServ`
       )
)+ 
  geom_tile()+
  #remove extra space
  scale_y_discrete(expand=c(0,0))+
  #set base size for all font elements
  theme_grey(base_size=10)+
  theme(
    #remove legend title
    legend.title=element_blank(),
    #remove legend margin
    legend.spacing = grid::unit(0,"cm"),
    #change legend text properties
    legend.text=element_text(colour=textcol,size=7,face="bold"),
    #change legend key height
    legend.key.height=grid::unit(0.8,"cm"),
    #set a slim legend
    legend.key.width=grid::unit(0.2,"cm"),
    #set x axis text size and colour
    axis.text.x=element_text(size=10,colour=textcol),
    #set y axis text colour and adjust vertical justification
    axis.text.y=element_text(vjust = 0.2,colour=textcol),
    #change axis ticks thickness
    axis.ticks=element_line(size=0.4),
    #change title font, size, colour and justification
    #remove plot background
    plot.background=element_blank(),
    #remove plot border
    panel.border=element_blank())

enter image description here

1 个答案:

答案 0 :(得分:1)

我认为这与您的日期间隔和date比例有关。我转换为因子,它们似乎运行良好:

ttfCountCompleted <- ttfCountCompleted %>% mutate(month = factor(months(`Production Date`)))

textcol <- "black"
ggplot(ttfCountCompleted, 
       aes(x = month, 
           y = `Months in Service`,
           fill=`nServ`
       )
)+ 
  geom_tile(color=NA)+
  #remove extra space
  scale_y_discrete(expand=c(0,0))+
  #set base size for all font elements
  theme_grey(base_size=10)+
  theme(
    #remove legend title
    legend.title=element_blank(),
    #remove legend margin
    legend.spacing = grid::unit(0,"cm"),
    #change legend text properties
    legend.text=element_text(colour=textcol,size=7,face="bold"),
    #change legend key height
    legend.key.height=grid::unit(0.8,"cm"),
    #set a slim legend
    legend.key.width=grid::unit(0.2,"cm"),
    #set x axis text size and colour
    axis.text.x=element_text(size=10,colour=textcol),
    #set y axis text colour and adjust vertical justification
    axis.text.y=element_text(vjust = 0.2,colour=textcol),
    #change axis ticks thickness
    axis.ticks=element_line(size=0.4),
    #change title font, size, colour and justification
    #remove plot background
    plot.background=element_blank(),
    #remove plot border
    panel.border=element_blank())