ozgeneral
ozgeneral

Reputation: 6779

ggplot geom_tile, remove vertical white lines

I have the following code to get attached image below, and I am looking for a way to get rid of those white stripes, or at least make it consistent across each row and column. Is there a way to achieve this? (also, what is causing them?)

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,

Upvotes: 4

Views: 2598

Answers (1)

Chris
Chris

Reputation: 3986

I believe it's to do with your date breaks and the date scale. I converted to factors and they seemed to work fine:

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())

Upvotes: 2

Related Questions