17

有时,使用向图像添加灰色半透明图层然后在该图层中切割钥匙孔以突出显示下方图像的特定部分的技术会很有帮助。这是来自youtube 视频的示例:

在此处输入图像描述

我有时会使用绘图来执行此操作,但使用 Inkscape 添加半透明图层,然后使用橡皮擦在该图层上切一个洞。这 (a) 看起来不够专业 (b) 需要额外的时间和单独的程序,以及 (c) 可能会降低质量。

我想在 R 中做到这一点。我在询问 ggplot2 因为这是我选择的工具,但我认为任何网格答案都会很好(我知道 base 可能有一个非常不同的方法)。

所以这是一个 MWE,我在其中添加了一个geom_rect来显示我们想要切割钥匙孔/窗户的位置:

ggplot(mtcars, aes(mpg, wt)) + 
    geom_point(size=3) +    
    geom_rect(mapping=aes(xmin=20, xmax=25, 
        ymin=3, ymax=3.3), color="black", alpha=.01)

如何使用 R 制作一个看起来像这样的图:

在此处输入图像描述

4

3 回答 3

19

原来你可以grid.path(...)grid包中做到这一点。阅读文档以了解如何创建带有孔的路径。

library(gridExtra)
library(ggplot2)

ggp <- ggplot(mtcars, aes(mpg, wt)) + geom_point(size=3)
grid.newpage()
grid.draw(arrangeGrob(ggp))
grid.path(c(0,0,1,1,.48,.48,.62,.62),
                    c(0,1,1,0,.43,.50,.50,.43),
                    id=rep(1:2, each=4),
                    rule="evenodd",gp=gpar(fill="black", alpha=0.6))

注意:grid.draw(...)并且grid.path(...)grid包装中;arrangeGrob(...)gridExtra包装中。加载gridExtra导致grid加载。感谢@MartinBel 建议编辑。

回应@BrandonBertelsen 评论:grid.path(...)对形状不可知论;你只需要提供坐标。

center <- c(x=0.55,y=0.48)
r      <- 0.1
circle <- do.call(rbind,lapply(seq(0,2*pi,length=36),
                               function(th)c(x=r*cos(th),y=r*sin(th))))
circle <- data.frame(circle)
circle$x <- circle$x + center["x"]
circle$y <- circle$y + center["y"]

ggp <- ggplot(mtcars, aes(mpg, wt)) + geom_point(size=3)
grid.newpage()
grid.draw(arrangeGrob(ggp))
grid.path(c(0,0,1,1,circle[,1]),
          c(0,1,1,0,circle[,2]),
          id=c(1,1,1,1,rep(2,nrow(circle))),
          rule="evenodd",gp=gpar(fill="black", alpha=0.6))

由于绘图窗口的纵横比,“圆”是一个椭圆。


进一步阅读:这不是你画的,而是你不画的,作者 Paul Murrell 在 R 杂志上

于 2014-01-26T23:33:59.697 回答
7

下面的呢?

P <- ggplot(mtcars, aes(mpg, wt)) + geom_point(size=3) 

## Set the limits for x & y
xlims <- c(20, 25)
ylims <- c(3, 3.3)


# Where P is the original plot
P + geom_rect(mapping=aes(xmin=-Inf, xmax=min(xlims), ymin=-Inf, ymax=+Inf), fill="black", alpha=.01) +
    geom_rect(mapping=aes(xmin=min(xlims), xmax=+Inf, ymin=max(ylims), ymax=+Inf), fill="black", alpha=.01) +
    geom_rect(mapping=aes(xmin=min(xlims), xmax=+Inf, ymin=-Inf, ymax=min(ylims)), fill="black", alpha=.01) +
    geom_rect(mapping=aes(xmin=max(xlims), xmax=+Inf, ymin=min(ylims), ymax=max(ylims)), fill="black", alpha=.01) 

在此处输入图像描述

于 2014-01-26T21:51:44.320 回答
5
require(ggplot2)

#represent some tiles based on your axes (10 x 10, by 1) deoending on resolution you want
alpha_tiles<-expand.grid(x=0:10,y=0:10,a=0.6)

#set alpha to 0 for the coordinate
alpha_tiles[alpha_tiles$x %in% 7:9 & alpha_tiles$y==7,]$a<-0

qplot(0:10,0:10, size=10, color="red") + theme_bw() +
  geom_raster(data=alpha_tiles,aes(x=x,y=y), alpha=alpha_tiles$a, fill="grey")

在此处输入图像描述

或者这是一个更完整的答案,使整个情节空白:

require(ggplot2)

#represent some tiles based on your axes (here 100 x 100, by 1) depending on resolution you want
resolution<-100
alpha_tiles<-expand.grid(x=0:resolution,y=0:resolution,a=0.6)

#set alpha to 0 for the coordinates to highlight
alpha_tiles[alpha_tiles$x %in% 40:70 & alpha_tiles$y %in% 70:80,]$a<-0
alpha_tiles[alpha_tiles$x %in% 10:30 & alpha_tiles$y %in% 10:25,]$a<-0

p<-qplot(0:10,0:10, size=10, color="red") + theme_bw() # background plot


qplot(0:resolution,0:resolution,geom="blank") + theme(axis.line=element_blank(),
                                     axis.text.x=element_blank(),
                                     axis.text.y=element_blank(),
                                     axis.ticks=element_blank(),
                                     axis.title.x=element_blank(),
                                     axis.title.y=element_blank(),
                                     legend.position="none",
                                     panel.background=element_blank(),
                                     panel.border=element_blank(),
                                     panel.grid.major=element_blank(),
                                     panel.grid.minor=element_blank(),
                                     plot.background=element_blank()) +
  annotation_custom(ggplotGrob(p),0,resolution,0,resolution) +
geom_raster(data=alpha_tiles,aes(x=x,y=y), alpha=alpha_tiles$a, fill="grey")

在此处输入图像描述

于 2014-01-26T21:53:44.820 回答