在geom_tile()/ geom_raster()中标记特定的图块 [英] Marking specific tiles in geom_tile() / geom_raster()

查看:165
本文介绍了在geom_tile()/ geom_raster()中标记特定的图块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个像这样的数据框:
$ b $ $ p $ df < - matrix(rnorm(100),nrow = 10)
rownames(df)< - 字母[1:10]
熔化< - 熔化(df)
熔化$ na < - 错误
熔化[圆形(runif(10,0,100)),na] < - T
头(熔化)

Var1 Var2值不详
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和 mark 绘制具有 na = TRUE 的图块(或栅格) 。目前我将这些商标绘制为点:$ b​​
$ b pre $ g $ p $ ggplot熔化
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,] ,但实际上这应该是可能的,而不指定其他数据集。 我不喜欢这些要点,但宁愿在墙上穿过墙或边框。但我不知道如何做到这一点。如果我使用 geom_segment()作为条带,我将如何指定 yend xend

任何帮助表示感谢。

编辑1
下面是 dput 的重现性:

 结构(列表(Var1 =结构(c(1L,2L,3L,4L,1L,2L,3L,
4L,1L,2L,3L,4L,1L,2L,3L)标记= c(A,
B,C,D,E,F,G,4L, (6L,6L,6L,6L,7L,7L,7L,7L,8L,8L,8L,8L,8L,8L,8L) ,
9L,9L,9L,9L,10L,10L,10L,10L),值= C(-0.468920099229389,
0.996105987531978,-0.527496444770932,-0.767851702991822,
-0.36077954422072,-0.145335912847538 ,0.114951323188032,
0.644232124274217,0.9971443502096584,0.7774515290180507,
-0.436252398260595,-0.111174676975868,1.116095688943808,
0.44677656465583,-0.708779168274131,0.460296447139761,0.460296447139761,0
-0.4 75304748445917,-0.481548436194392,-1.66560630161765,
-2.06055347675196),NA = C(FALSE,FALSE,FALSE,FALSE,FALSE,
FALSE,FALSE,FALSE,TRUE,FALSE,FALSE,FALSE,FALSE, FALSE,
FALSE,FALSE,TRUE,FALSE,FALSE,FALSE)),.Names = c(Var1,
Var2,value,na),row.names = C(51L,52L,53L,54L,61L,
62L,63L,64L,71L,72L,73L,74L,81L,82L,83L,84L,91L,92L,
93L,94L) ,class =data.frame)


解决方案

两种可能的方法:在示例1中,我使用了 ifelse scale_size_manual 来控制点是否绘制在每个单元格中。在示例2中,我创建了一个小的辅助数据框,并使用 geom_rect 来绘制一个矩形而不是一个点。为了方便,我将Var2转换为因子。在ggplot2中,沿离散/因子轴的每个步长都是1.0。这可以方便地计算 geom_rect 的值。

 #使用ggplot2版本0.9.2.1 
库(ggplot2)

#原始帖子的测试数据集已被分配到'熔化'。

melt $ Var2 = factor(熔化的$ Var2)

#例子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)

 #示例2. 
#创建辅助数据框。
帧=熔化[熔化$ na,c(Var1,Var2)]
帧$ Var1 = as.integer(帧$ Var1)
帧$ Var2 = as.integer (帧$ VAR2)

P2 = ggplot(数据=熔融)+
geom_raster(AES(X = VAR1,Y = VAR2,填充=值))+
scale_fill_gradient2( low =blue,high =red,na.value =black,name =)+
geom_rect(data = frames,size = 1,fill = NA,color =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)


Let's say I have a data.frame like this:

df <- matrix( rnorm(100), nrow = 10)
rownames(df) <- LETTERS[1:10]
molten <- melt(df)
molten$na <- FALSE
molten[ round(runif(10,  0, 100 )), "na" ] <- T
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

Now, I want to plot a tile (or raster) plot using ggplot and mark those tiles which have na=TRUE. Currently I plot the marks as points:

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) ) )

However, I don't like this plot very much for two reasons:

  1. There is still a point drawn even if molten$na = FALSE. Sure I could specify data=molten[ molten$na, ], but actually this should be possible without specifying another data set.
  2. I don't like the points, but would rather like to have frames around or stripes through the tiles. But I have no idea how to achieve this. If I would use geom_segment() for stripes, how would I specify yendand xend?

Any help is appreciated.

Edit 1 Here is the dputfor reproducibility:

structure(list(Var1 = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("A", 
"B", "C", "D", "E", "F", "G", "H", "I", "J"), class = "factor"), 
    Var2 = c(6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 
    9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L), 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 = c(FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, TRUE, FALSE, FALSE, FALSE)), .Names = c("Var1", 
"Var2", "value", "na"), row.names = c(51L, 52L, 53L, 54L, 61L, 
62L, 63L, 64L, 71L, 72L, 73L, 74L, 81L, 82L, 83L, 84L, 91L, 92L, 
93L, 94L), class = "data.frame")

解决方案

Here are two possible approaches:

In Example 1, I used ifelse and scale_size_manual to control whether a point is plotted in each cell.

In Example 2, I created a small auxiliary data.frame and used geom_rect to plot a rectangle instead of a dot. For convenience, I converted Var2 to factor. In ggplot2, each step along a discrete/factor axis is length 1.0. This allows easy computation of the values for 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) 

这篇关于在geom_tile()/ geom_raster()中标记特定的图块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆