Search code examples
rggplot2heatmap

how to remove white lines from geom_tile (heat map) using ggplot2


I am having trouble removing the white lines between tiles in my heat map. Below is my code and picture. Has anyone encountered this before?

t <- ggplot(Drug_heatmap_df_final, 
   aes(x=reorder(Drug,Total_Deaths), y=Start_Date, fill=Total_Deaths)) + 
   geom_tile() + 
   labs(title="Heatmap of Total Deaths per month by Drug", x="Drug", y="Month") + 
   theme(plot.title = element_text(hjust=.5)) +
   scale_y_date(date_breaks="1 year" , labels = date_format("%b-%Y")) +
   theme(axis.text.x = element_text(size=13)) 

plot(t)

enter image description here


Solution

  • I don't know if this is the most elegant solution but if you add color in your aes and then play with the size in geom_tile you can get them to overlap and remove the white lines:

    First is how my data looks with the white lines:

    ggplot(mydf, aes(x=grp, y=date, fill=n)) + 
      geom_tile()
    

    enter image description here

    Now I set my color to the same object as my fill and mess with the size:

    ggplot(mydf, aes(x=grp, y=date, fill=n,color=n)) + 
      geom_tile(size=0.6) 
    

    enter image description here

    Like I said, probably not the most elegant solution, and there is probably a better, more efficient way to determine the size value (instead of trial and error like I did) but in general this seems to solve your issue.