ggplot2 使 geom_tile 中的缺失值不为空

发布于 2024-11-27 10:17:47 字数 514 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情何以堪。 2024-12-04 10:17:47

这个问题也可以通过下面的scale_fill_continuous编辑中的选项来解决

scale_fill_continuous(na.value = 'salmon')

这仅填充明确的(即NA值)缺失值。 (它在以前版本的 ggplot 中的工作方式可能有所不同,我懒得检查)

请参阅以下代码作为示例:

library(tidyverse)
Data <- expand.grid(x = 1:5,y=1:5) %>%
  mutate(Value = rnorm(25))

Data %>%
  filter(y!=3) %>%
ggplot(aes(x=x,y=y,fill=Value))+
  geom_tile()+
  scale_fill_continuous(na.value = 'salmon')

Data %>%
  mutate(Value=ifelse(1:n() %in% sample(1:n(),22),NA,Value)) %>%
  ggplot(aes(x=x,y=y,fill=Value))+
  geom_tile()+
  scale_fill_continuous(na.value = 'salmon')

对此的一个简单解决方法是使用完整函数使缺失值显式化。

Data %>%
  filter(1:n() %in% sample(1:n(),22)) %>%
  complete(x,y) %>%
  ggplot(aes(x=x,y=y,fill=Value))+
  geom_tile()+
  scale_fill_continuous(na.value = 'salmon')

在某些情况下,展开函数可能比完整函数更有用。

This issue can also be fixed by an option in scale_fill_continuous

scale_fill_continuous(na.value = 'salmon')

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:

library(tidyverse)
Data <- expand.grid(x = 1:5,y=1:5) %>%
  mutate(Value = rnorm(25))

Data %>%
  filter(y!=3) %>%
ggplot(aes(x=x,y=y,fill=Value))+
  geom_tile()+
  scale_fill_continuous(na.value = 'salmon')

Data %>%
  mutate(Value=ifelse(1:n() %in% sample(1:n(),22),NA,Value)) %>%
  ggplot(aes(x=x,y=y,fill=Value))+
  geom_tile()+
  scale_fill_continuous(na.value = 'salmon')

An easy fix for this is to use the complete function to make the missing values explicit.

Data %>%
  filter(1:n() %in% sample(1:n(),22)) %>%
  complete(x,y) %>%
  ggplot(aes(x=x,y=y,fill=Value))+
  geom_tile()+
  scale_fill_continuous(na.value = 'salmon')

In some cases the expand function may be more useful than the complete function.

嘿看小鸭子会跑 2024-12-04 10:17:47

如果您的数据是类似网格的数据,那么通过 subset() 添加另一个用于 NA 的 geom_tile() 怎么样?

# Generate data
pp <- function (n, r = 4) {
  x    <- seq(-r*pi, r*pi, len = n)
  df   <- expand.grid(x = x, y = x)
  df$r <- sqrt(df$x^2 + df$y^2)
  df$z <- cos(df$r^2)*exp(-df$r/6)
  df
}
pp20 <- pp(20)[sample(20*20, size = 200),]

df_grid  <- expand.grid(x = unique(pp20$x), y = unique(pp20$x))
df_merge <- merge(pp20, df_grid, by = c("x", "y"), all = TRUE)

# Missing values
ggplot(df_merge, aes(x = x, y = y)) +
  geom_tile(data = subset(df_merge, !is.na(z)), aes(fill = z)) +
  geom_tile(data = subset(df_merge,  is.na(z)), aes(colour = NA),
    linetype = 0, fill = "pink", alpha = 0.5)

示例

If your data is a grid-like data, how about adding another geom_tile() for NA by subset()?

# Generate data
pp <- function (n, r = 4) {
  x    <- seq(-r*pi, r*pi, len = n)
  df   <- expand.grid(x = x, y = x)
  df$r <- sqrt(df$x^2 + df$y^2)
  df$z <- cos(df$r^2)*exp(-df$r/6)
  df
}
pp20 <- pp(20)[sample(20*20, size = 200),]

df_grid  <- expand.grid(x = unique(pp20$x), y = unique(pp20$x))
df_merge <- merge(pp20, df_grid, by = c("x", "y"), all = TRUE)

# Missing values
ggplot(df_merge, aes(x = x, y = y)) +
  geom_tile(data = subset(df_merge, !is.na(z)), aes(fill = z)) +
  geom_tile(data = subset(df_merge,  is.na(z)), aes(colour = NA),
    linetype = 0, fill = "pink", alpha = 0.5)

an example

那片花海 2024-12-04 10:17:47

对于后代,这里是与 ggplot2 版本 1.9.3 兼容的正确解决方案

+ theme(panel.background=element_rect(fill="blue", colour="blue")
  • 在 joran 的回答中,plot.background 是整个图,包括标题和图例等panel.background 是数据出现的区域。

  • 在最新版本的ggplot2中,opts已替换为themetheme_rect已替换为element_rect >.

  • element_rect时,color是矩形的边界,而fill是矩形的内部。

我最初使用过,

+ geom_rect(aes(xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf), fill="blue")

但是当在背景上添加 geom_raster 而不是 geom_tile 并生成 pdf 输出时,pdf 查看器的渲染非常困难情节,使用更多的CPU周期和内存。

For posterity, here is the right solution compatible with ggplot2 version 1.9.3

+ theme(panel.background=element_rect(fill="blue", colour="blue")
  • In joran's answer, the plot.background is the whole plot including the title and legend etc. The panel.background is the area where the data appears.

  • In the latest version of ggplot2, opts has been replaced with theme and theme_rect has been replaced with element_rect.

  • In specifying element_rect, color is the boundary of the rectangle while fill is the interior of the rectangle.

I had originally used,

+ geom_rect(aes(xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf), fill="blue")

but when adding geom_raster rather than geom_tile over the the background and generate pdf output, pdf viewers had a very hard time rendering the plot, using substantially more cpu cycles and memory.

—━☆沉默づ 2024-12-04 10:17:47

这个答案可能有点太“可爱”,但一种解决方案是否可以简单地将绘图的背景颜色更改为比例中的最小颜色?例如:

+ opts(plot.background = theme_rect(colour = "blue")

如果您的绘图具有更复杂的结构,并且这最终会使您不希望发生这种情况的区域的背景变成蓝色,您可以首先绘制一个 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:

+ opts(plot.background = theme_rect(colour = "blue")

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文