gpt4 book ai didi

how to remove white lines from geom_tile (heat map) using ggplot2(如何使用ggplot2从geom_iles(热图)中删除白色线条)

翻译 作者:bug小助手 更新时间:2023-10-26 22:28:16 25 4
gpt4 key购买 nike



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


更多回答
优秀答案推荐

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:

我不知道这是否是最优雅的解决方案,但如果你在你的AES中添加颜色,然后处理geom_tile中的大小,你可以让它们重叠并删除白色线条:



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.

就像我说的,可能不是最好的解决方案,而且可能有更好、更有效的方法来确定大小值(而不是像我那样反复试验),但总的来说,这似乎解决了您的问题。



So I struggled with this same problem, unable to make my offending axis (a 24 hour period) discrete. In the end I realised that where I had plotted tiles every 2 minutes (1/30 of an hour), my csv data file had rounded the points up to 0.03 intervals, leaving gaps in between tiles, hence the white lines. I simply changed the number of decimal places in the excel sheet from 2 to many, giving 0.03333333 intervals and the white lines are gone. Horray! my heatmap before vs after

因此,我也在努力解决同样的问题,无法使我的冒犯轴(24小时周期)离散。最后我意识到,我每2分钟(1/30小时)绘制一次图块,我的csv数据文件将点四舍五入到0.03的间隔,在图块之间留下了间隙,因此出现了白线。我只是简单地将Excel表格中的小数位数从2位改为多位,给出0.0333333的间隔,白线消失了。霍雷我的热图之前VS之后



If you're plotting a continuous variable on both axes, and you're using geom_tile, you should consider using geom_raster instead. geom_raster removed the lines in my use case, and it's faster!

如果要在两个轴上绘制一个连续变量,并且使用geom_tile,则应考虑改用geom_raster。Geom_raster去掉了我用例中的行,而且速度更快!


geom_raster requires each tile to be the same size, but that has often been sufficient in my experience. Check out the documentation here: https://ggplot2.tidyverse.org/reference/geom_tile.html

Geom_raster要求每个瓷砖的大小相同,但根据我的经验,这通常已经足够了。请查看此处的文档:https://ggplot2.tidyverse.org/reference/geom_tile.html


更多回答

This what exactly what I needed! I had also tried adding geom_raster() at the end of my code. It reduce the number of white lines from every row to 6 white lines throughout the whole plot, but I needed to remove all white lines. Your solution removed it all so thank you!

这正是我所需要的!我还尝试在代码末尾添加geom_raster()。它将白线的数量从每一行减少到整个绘图中的6条白线,但我需要删除所有白线。您的解决方案消除了这一切,所以谢谢您!

Using ggplot2 3.3.2, geom_tile(size= ...) didn't change anything for me. However, I have not dates on the y scale, but integers. What worked for me is geom_tile(height=1.01). The value is something slightly larger than the default, maybe the default is different for a date y scale. When the stripes are vertical geom_tile(width=...) can be used similarly.

使用ggplot2 3.3.2,geom_tile(大小=...)并没有为我改变任何事情。然而,我没有以y为标度的日期,而是整数。对我起作用的是geom_iles(Height=1.01)。该值比缺省值稍大,可能日期y刻度的缺省值有所不同。当条纹是垂直的geom_tile(宽度=...)可以使用类似的方法。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com