我有一个使用 R 绘图的问题:我有基于像素的 x-y 坐标(658px x 402px jpg 图片)。我想以图片( map )为背景绘制 x-y 坐标。
我尝试了这个,但事实证明有问题;这些点应该在北海(全部)
d <- read.csv("~/Dropbox/IMITATION3/d.csv", sep=";")
library(ggplot2)
library(jpeg)
library(grid)
ima <- readJPEG("~/Dropbox/IMITATION3/image.jpg")
ggplot(d, aes(IM02_cx, IM02_cy)) +
annotation_custom(rasterGrob(ima, width=unit(1,"npc"), height=unit(1,"npc")),
-Inf, Inf, -Inf, Inf) +
stat_bin2d(bins = 55) +
scale_x_continuous(expand=c(0,0)) +
scale_y_reverse(expand=c(0,0))
这是图片:
CSV:
CASE;IM02_cx;IM02_cy
412;191;75
127;222;74
459;220;73
80;138;72
86;225;72
458;156;71
71;86;69
289;219;69
291;219;68
77;221;68
338;199;67
495;200;67
371;138;66
197;227;66
64;134;65
105;170;65
124;196;65
237;171;64
350;213;64
63;92;63
305;106;63
286;139;63
99;199;63
353;201;63
362;198;62
452;168;60
479;204;60
65;209;60
330;214;60
132;101;59
162;184;59
93;182;58
249;209;58
49;117;57
106;172;57
83;150;56
282;168;56
234;190;56
492;165;55
40;181;55
448;195;55
262;184;54
199;165;53
47;92;52
109;143;52
107;147;52
97;208;51
467;145;50
98;169;50
451;169;49
480;117;48
414;96;47
424;122;47
466;143;47
514;170;47
465;133;45
176;172;45
69;183;45
417;109;44
220;140;44
160;96;43
172;128;43
48;132;43
344;156;43
490;163;43
396;182;43
374;163;41
327;172;41
128;173;41
408;82;40
53;147;40
409;158;40
225;71;38
402;70;37
469;149;34
284;99;32
455;163;32
149;174;21
PS:我不知道为什么,但我必须反转我的 y 尺度才能(基本上)以正确的角度获得点......–
最佳答案
您正在将坐标引用系统设为图像,因此您必须手动处理“投影”。您还可以使用 ggmap
包中的 ggimage
。以下假设您的数据位于 dat
中:
library(ggplot2)
library(ggmap)
library(jpeg)
library(grid)
ima <- readJPEG("image.jpg")
gg <- ggimage(ima)
gg <- gg + geom_point(data=dat, aes(x=IM02_cx, y=nrow(ima)-IM02_cy))
gg
关于r - 如何使用图片作为坐标系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32506950/