ggplot2 使 geom_tile 中的缺失值不为空
在 R 的 ggplot2 数据可视化包的 geom_tile() 层中,当单元格不包含数据时,不会绘制该单元格。例如 http://docs.ggplot2.org/current/geom_tile.html 并搜索“缺失值”。
我想更改此行为以显示所有图块的最小值。这可能吗?如果可能的话怎么办?
附加背景:当我使用时,
stat_density2d(aes(x=x,y=y, fill=..density..), geom="tile", contour=FALSE)
我希望没有密度的区域看起来与密度很小的区域非常相似。就像现在一样,如果说色谱是从蓝色到红色并且背景是白色,那么当图块中没有数据时,它是白色的,而当图块中存在单个数据点时,它是蓝色的。
向数据添加伪计数似乎是可能的,但我如何提前知道如何分配伪计数?在有刻面的情况下?
In the geom_tile()
layer in the ggplot2 data visualization package for R, when a cell contains no data it is not drawn. E.g. http://docs.ggplot2.org/current/geom_tile.html and search for "missing value".
I would like to change this behavior to show the minimum value over all the tiles. Is this possible and if so how?
Additional context: when I use
stat_density2d(aes(x=x,y=y, fill=..density..), geom="tile", contour=FALSE)
I would like the regions with no density to look very similar to the regions with very little density. As it is now, if say the color spectrum is from blue to red and the background is white, then there when there is no data in a tile it is white and when there is a single data point in a tile is blue.
Adding a pseudo count to the data seems possible, but how do I know in advance how to distribute the pseudo-counts? and in the case when there are faceting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这个问题也可以通过下面的scale_fill_continuous编辑中的选项来解决
:
这仅填充明确的(即NA值)缺失值。 (它在以前版本的 ggplot 中的工作方式可能有所不同,我懒得检查)
请参阅以下代码作为示例:
对此的一个简单解决方法是使用完整函数使缺失值显式化。
在某些情况下,展开函数可能比完整函数更有用。
This issue can also be fixed by an option in scale_fill_continuous
Edit below:
This only fills in the explicitly (i.e. values which are NA) missing values. (It may have worked differently in previous versions of ggplot, I'm too lazy to check)
See the following code for an example:
An easy fix for this is to use the complete function to make the missing values explicit.
In some cases the expand function may be more useful than the complete function.
如果您的数据是类似网格的数据,那么通过
subset()
添加另一个用于 NA 的geom_tile()
怎么样?If your data is a grid-like data, how about adding another
geom_tile()
for NA bysubset()
?对于后代,这里是与 ggplot2 版本 1.9.3 兼容的正确解决方案
在 joran 的回答中,plot.background 是整个图,包括标题和图例等
panel.background
是数据出现的区域。在最新版本的ggplot2中,
opts
已替换为theme
,theme_rect
已替换为element_rect
>.element_rect
时,color
是矩形的边界,而fill
是矩形的内部。我最初使用过,
但是当在背景上添加
geom_raster
而不是geom_tile
并生成pdf
输出时,pdf 查看器的渲染非常困难情节,使用更多的CPU周期和内存。For posterity, here is the right solution compatible with
ggplot2
version 1.9.3In joran's answer, the
plot.background
is the whole plot including the title and legend etc. Thepanel.background
is the area where the data appears.In the latest version of ggplot2,
opts
has been replaced withtheme
andtheme_rect
has been replaced withelement_rect
.In specifying
element_rect
,color
is the boundary of the rectangle whilefill
is the interior of the rectangle.I had originally used,
but when adding
geom_raster
rather thangeom_tile
over the the background and generatepdf
output, pdf viewers had a very hard time rendering the plot, using substantially more cpu cycles and memory.这个答案可能有点太“可爱”,但一种解决方案是否可以简单地将绘图的背景颜色更改为比例中的最小颜色?例如:
如果您的绘图具有更复杂的结构,并且这最终会使您不希望发生这种情况的区域的背景变成蓝色,您可以首先绘制一个
geom_rect
图层,该图层延伸到仅您的数据范围。This answer may perhaps be a bit too 'cute', but could one solution be to simply change the background color of your plot to be the minimum color in your scale? For instance:
If your plot has a more complex structure and this ends up making the background blue in areas where you don't want that to happen, you could plot a
geom_rect
layer first that extends to through the ranges of your data only.