有时,使用向图像添加灰色半透明层然后将键孔切割到该层以突出显示下面图像的某个部分的技术是有帮助的.以下是来自youtube vide的示例:

我有时使用绘图,但使用Inkscape添加半透明层,然后使用橡皮擦在该层中切出一个洞.这(a)看起来不够专业(b)需要额外的时间和单独的程序和(c)可能的质量损失.
我想在R中做.我问的是ggplot2,因为这是我选择的工具,但我认为任何网格答案都会很好(我知道基础可能有一个非常不同的方法).
所以这是一个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)
Run Code Online (Sandbox Code Playgroud)
我如何使用R来制作看起来像这样的图:

jlh*_*ard 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))
Run Code Online (Sandbox Code Playgroud)

NB: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))
Run Code Online (Sandbox Code Playgroud)

由于绘图窗口的纵横比,"圆"是椭圆.
进一步阅读:这不是你画的,这是你在R日记中没有被Paul Murrell所描绘的
以下怎么样?
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)
Run Code Online (Sandbox Code Playgroud)

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")
Run Code Online (Sandbox Code Playgroud)

或者这是一个更完整的答案,使整个情节空白:
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")
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
516 次 |
| 最近记录: |