在图片的特定区域绘制点

4
我希望您能在图片上绘制点:
我有以下数据框:
x <- c(1,2)
y <- c(10,12)
df <- data.frame(x,y

以下是一张图片 - 网球场

enter image description here

 img <- readJPEG("tennis_court.jpg")

以下代码用于绘制图片。
 ggplot(df, aes(x,y)) + 
 annotation_custom(rasterGrob(img, width=unit(1,"npc"), height=unit(1,"npc")), 
                   -Inf, Inf, -Inf, Inf) +
 stat_bin2d() +
 scale_x_continuous(expand=c(0,0)) +
 scale_y_continuous(expand=c(0,0)) 

这个可以用,但我想把点绘制在特定的区域上。比如说,我想在球场右侧的左侧服务区域绘制它。
有什么想法可以将图片的特定部分分类,以便在该区域绘制点吗?
1个回答

3
以下是一种方法:

这是一种方法:

library(jpeg)
library(ggplot2)
library(grid)
library(scales)

download.file("http://i.stack.imgur.com/BozHb.jpg", tf <- tempfile(fileext = ".jpg"), mode="wb")
img <- readJPEG(tf)
x <- c(1,2)
y <- c(10,12)
df <- data.frame(x, y)

ggplot(transform(df, x=rescale(x, c(0.51, 0.75)), y=rescale(y, c(.2, .5))), 
      aes(x,y)) + 
annotation_custom(rasterGrob(img, width=unit(1,"npc"), height=unit(1,"npc")), -Inf, Inf, -Inf, Inf) +
stat_bin2d() +
scale_x_continuous(expand=c(0,0), limits = c(0, 1)) +
scale_y_continuous(expand=c(0,0), limits = c(0, 1)) 

谢谢!运行得很好!你有什么想法可以改变我的绘图点(颜色和大小)吗? - John Dwyer
不用谢。例如,尝试使用 + geom_point(size=10, shape=15, color="red") 来绘制大的红色正方形。 - lukeA

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接