在geom_tile()/ geom_raster()中标记特定切片

kq0g1dla  于 2023-05-11  发布在  其他
关注(0)|答案(2)|浏览(116)

假设我有一个这样的data.frame:

molten <- data.frame(
  Var1 = factor(
    rep(c("A", "B", "C", "D"), 5),
    levels = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J")
  ),
  Var2 = rep(6:10, each = 4L),
  value = c(
    -0.468920099229389, 0.996105987531978, -0.527496444770932, -0.767851702991822,
    -0.36077954422072, -0.145335912847538, 0.114951323188032, 0.644232124274217,
    0.971443502096584, 0.774515290180507, -0.436252398260595, -0.111174676975868,
    1.16095688943808, 0.44677656465583, -0.708779168274131, 0.460296447139761,
    -0.475304748445917, -0.481548436194392, -1.66560630161765, -2.06055347675196
  ),
  na = rep(c(FALSE, TRUE, FALSE, TRUE, FALSE), c(8L, 1L, 7L, 1L, 3L)),
  row.names = c(
    51L, 52L, 53L, 54L, 61L, 62L, 63L, 64L, 71L, 72L, 73L, 74L,
    81L, 82L, 83L, 84L, 91L, 92L, 93L, 94L
  )
)

head(molten)

  Var1 Var2      value    na
1    A    1 -0.2413015 FALSE
2    B    1  1.5077282 FALSE
3    C    1 -1.0798806 TRUE
4    D    1  2.0723791 FALSE

现在,我想使用ggplot绘制一个图块(或光栅)图,并 * 标记 * 那些具有na=TRUE的图块。目前我将标记绘制为点:

g <- ggplot( molten ) +
  geom_raster( aes( x = Var1, y = Var2, fill = value )  ) + 
  scale_fill_gradient2( low = "blue", high = "red", na.value="black", name = "" ) +
  geom_point( aes( x = Var1, y = Var2, size= as.numeric(na) ) )

但是,我不太喜欢这个情节,原因有两个:
1.即使molten$na = FALSE也仍然有一个点。当然,我可以指定data=molten[ molten$na, ],但实际上这应该是可能的,而不需要指定另一个数据集。
1.我不喜欢的点,但宁愿有框架或条纹通过瓷砖周围。但我不知道如何实现这一点。如果我将geom_segment()用于条带,我将如何指定yendxend

nfg76nw0

nfg76nw01#

以下是两种可能的方法:
在示例1中,我使用ifelsescale_size_manual来控制是否在每个单元格中绘制点。
在示例2中,我创建了一个小的辅助data.frame,并使用geom_rect绘制矩形而不是点。为了方便起见,我将Var 2转换为factor。在ggplot 2中,沿着离散/因子轴的每个步长的长度为1.0。这允许容易地计算geom_rect的值。

# Using ggplot2 version 0.9.2.1
library(ggplot2)

# Test dataset from original post has been assigned to 'molten'.

molten$Var2 = factor(molten$Var2)

# Example 1.
p1 = ggplot(data=molten, aes(x=Var1, y=Var2, fill=value)) +
     geom_raster() +
     scale_fill_gradient2(low="blue", high="red", na.value="black", name="") +
     geom_point(aes(size=ifelse(na, "dot", "no_dot"))) +
     scale_size_manual(values=c(dot=6, no_dot=NA), guide="none") +
     labs(title="Example 1")

ggsave(plot=p1, filename="plot_1.png", height=3, width=3.5)

# Example 2.
# Create auxiliary data.frame.
frames = molten[molten$na, c("Var1", "Var2")]
frames$Var1 = as.integer(frames$Var1)
frames$Var2 = as.integer(frames$Var2)

p2 = ggplot(data=molten) +
     geom_raster(aes(x=Var1, y=Var2, fill=value)) +
     scale_fill_gradient2(low="blue", high="red", na.value="black", name="") +
     geom_rect(data=frames, size=1, fill=NA, colour="black",
       aes(xmin=Var1 - 0.5, xmax=Var1 + 0.5, ymin=Var2 - 0.5, ymax=Var2 + 0.5)) +
     labs(title="Example 2")

ggsave(plot=p2, filename="plot_2.png", height=3, width=3.5)

m1m5dgzv

m1m5dgzv2#

正如@joran在评论中建议的那样,您可以将数据的子集传递给特定的层。
使用您的示例数据

g <- ggplot( molten ) +
  geom_raster( aes( x = Var1, y = Var2, fill = value )  ) + 
  scale_fill_gradient2( low = "blue", high = "red", na.value="black", name = "" ) +
  geom_point(data = molten[molten$na,], aes( x = Var1, y = Var2, size= as.numeric(na) ) )

g

如果你想让图例说明这些点的含义

g <- ggplot( molten ) +
  geom_raster( aes( x = Var1, y = Var2, fill = value )  ) + 
  scale_fill_gradient2( low = "blue", high = "red", na.value="black", name = "" ) +
  geom_point(data = molten[molten$na,], aes( x = Var1, y = Var2, colour = 'black' )) +
  scale_colour_manual(name = 'Ooh look', values = 'black', labels = 'Something cool')

相关问题