4

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,

ozgeneral
  • 6,079
  • 2
  • 30
  • 45
  • 1
    When asking for help with plotting, it's much easier if you provide some [sample data](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so we are able to run and test the code. Also, is there a reason you tagged this with "shiny" as well? – MrFlick Aug 20 '18 at 15:42
  • Sure, I will generate example data now. I dont know what is causing the lines so I thought giving the full context might be relevant (I can check it out without shiny and remove the tag if it the problem persists). – ozgeneral Aug 20 '18 at 15:44
  • Possible duplicate: https://stackoverflow.com/questions/49179092/how-to-remove-white-lines-from-geom-tile-heat-map-using-ggplot2 – MrFlick Aug 20 '18 at 15:47
  • Please see the edit. (@MrFlick size=0.6 or similar values did not fix it) – ozgeneral Aug 20 '18 at 15:51

1 Answers1

2

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())
Chris
  • 3,836
  • 1
  • 16
  • 34
  • That works, thanks a lot! Just a side note, example is a smaller version of what I actually have so `month = factor(months(...` wouldnt work after a year, thats why I ended up using only factors and manually choosing the x-axis breaks – ozgeneral Aug 20 '18 at 16:06
  • @ÖzgenEren I suspected that might be the case, glad you figured out a solution. – Chris Aug 20 '18 at 16:09