如何使用ggplot2从geom_tile(热图)中删除白线

时间:2018-03-08 17:30:12

标签: r ggplot2 heatmap

我在热图中删除瓷砖之间的白线时遇到问题。下面是我的代码和图片。有没有人遇到过这个?

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

2 个答案:

答案 0 :(得分:1)

因此,我为同样的问题而苦苦挣扎,无法使犯罪轴(24小时周期)离散。最后,我意识到我每2分钟(一小时的1/30)绘制图块的位置,我的csv数据文件将这些点四舍五入至0.03的间隔,在图块之间留有空隙,因此出现了白线。我只是将excel工作表中的小数位数从2更改为许多,给出了0.03333333的间隔,白线消失了。霍雷! my heatmap before vs after

答案 1 :(得分:0)

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.