如何在照片上绘制二维密度图

问题描述

我有一个二维密度图,需要将其叠加到照片上。我可以用rtiff包读入图片:

install.packages("rtiff")
library(rtiff)
x <- readTiff('F01_screenshot.tiff')
plot(x)

密度图的数据是一个非常大的数据框,称为eet2(请参见下面的可再现数据);密度图的代码是这样的:

library(ggplot2)
ggplot(eet2,aes(x=X,y= Y)) +
  geom_point() + stat_density2d()

我可以轻松地分别生成两个图,但是不知道如何将密度图叠加到照片上,从而使密度图按比例缩小以适合图片。任何帮助深表感谢!

enter image description here

可复制的数据

eet2 <- dput(eet1[1:1000,2:3])
structure(list(X = c(0,177.378,27.289,852.719,852.813,71.068,144.875,140.385,21.598,170.325,136.746,21.038,146.279,141.86,11.822,146.078,137.681,21.182,148.867,132.886,20.444,146.129,80.251,6.688,141.08,149.789,23.044,74.097,141.182,21.72,83.075,81.192,6.766,849.784,153.96,23.686,78.374,142.782,21.967,72.929,142.922,11.91,83.639,143.912,22.14,134.809,142.826,21.973,132.7,85.876,7.156,138.935,80.951,12.454,144.385,141.716,21.802,76.768,74.406,6.2,134.444,155.341,23.899,189.336,224.517,34.541,207.46,216.122,18.01,204.552,208.524,32.081,207.513,203.162,31.256,197.578,204.362,17.03,195.223,956.396,147.138,201.969,224.989,34.614,214.064,140.374,11.698,140.235,958.501,147.462,141.898,143.337,22.052,955.901,954.965,79.58,131.323,223.214,34.341,952.203,143.102,22.016,935.525,918.238,76.52,123.337,127.93,19.682,915.755,128.336,19.744,913.158,120.076,10.006,911.196,124.387,19.136,911.631,911.389,140.214,910.433,119.375,9.948,115.898,910.073,140.011,915.461,965.025,148.465,1125.565,1074.389,165.291,1061.611,1051.23,161.728,1044.3,269.733,22.478,245.869,206.934,234.525,245.609,250.874,251.576,260.164,262.974,263.161,269.25,262.693,261.818,265.634,266.479,266.592,266.39,262.751,262.508,265.093,262.666,262.343,1059.856,1075.848,1077.98,1068.255,1065.442,1065.255,1071.821,1072.292,1072.355,1073.788,1072.572,1072.41,1074.295,1074.487,1074.499,1078.434,1080.074,1080.293,1081.104,1080.014,1079.869,1080.261,1080.181,1080.175,1078.568,1079.422,1079.536,1078.84,1077.459,1077.275,1077.805,1076.787,1076.719,1077.199,1076.045,1075.891,1077.362,1076.663,1076.569,1077.942,1078.842,1078.902,1078.713,1080.023,1080.198,1080.764,1081.553,1081.659,1081.946,1082.445,1082.479,1083.987,1084.113,1084.13,1084.741,1085.809,1085.952,1085.031,1084.708,1084.686,1085.901,1085.266,1085.181,1085.513,1085.159,1085.112,1088.024,1088.29,1088.308,1088.889,1088.327,1088.252,1069.543,1065.069,1064.472,1067.409,1068.075,1068.119,1068.377,1070.019,1070.238,1072.384,1071.454,1071.33,1072.31,1069.704,1069.53,1068.856,1073.986,1074.67,1075.824,1077.42,1077.633,1080.095,1082.62,1082.788,1087.112,1090.026,1090.414,1094.654,1098.678,1099.215,1103.801,1097.674,1097.266,1110.129,1105.4,1104.769,1115.638,1115.922,1115.96,1115.612,1115.278,1115.256,1114.682,1112.96,1112.73,1063.573,948.249,932.873,844.512,834.23,833.545,865.203,867.402,867.695,838.499,864.3,867.74,837.215,864.187,865.985,760.653,862.073,861.199,860.757,860.699,861.681,71.807,851.287,855.415,855.965,859.554,864.523,865.185,868.109,873.22,873.561,845.881,882.877,887.81,893.391,899.782,900.634,908.492,56.781,319.09,696.905,704.008,58.667,677.276,673.676,673.195,673.45,671.209,671.06,672.474,674.07,674.283,674.896,656.33,653.855,678.654,681.884,682.099,684.933,715.507,719.584,735.659,1290.244,1364.188,1362.632,1366.322,1366.568,1371.973,1364.1,1363.051,1361.383,812.325,739.117,734.716,1322.199,1361.365,730.422,731.132,731.227,729.084,724.164,723.507,726.199,725.939,725.922,725.437,1278.917,1352.714,726.791,1281.431,1355.383,1366.558,764.435,724.294,725.46,724.544,724.422,724.178,1288.932,1364.232,723.902,680.495,677.601,724.172,722.856,722.681,735.236,743.706,744.835,554.779,735.387,747.427,724.914,720.25,719.628,721.122,742.971,745.884,742.171,737.947,737.665,729.207,808.032,818.542,905.216,106.496,1169.534,1195.852,99.654,1236.396,1190.473,183.15,1143.686,1132.754,1132.025,1123.856,1103.6,1100.9,1083.716,1079.191,1078.588,1083.785,1070.395,1069.503,1066.885,1065.121,1064.885,1064.401,1065.285,1065.403,1075.009,1076.036,1076.105,1086.308,1075.821,1074.423,1073.282,126.268,1092.281,1019.684,1087.663,286.389,33.693,1117.064,1114.254,171.424,1111.518,1110.347,92.529,1077.293,1107.754,170.424,1107.136,1076.571,165.626,1067.475,1110.71,92.559,274.852,280.654,43.177,280.278,271.773,270.639,1060.434,88.369,1054.964,1047.876,1046.931,1052.977,1064.93,1066.524,1061.181,1069.339,1069.883,275.888,970.662,1063.299,268.308,974.232,1068.355,1044.757,1065.286,1066.655,1076.68,1085.54,1086.721,1088.216,1083.409,1082.768,1087.272,1087.283,1086.689,1087.19,1087.257,1089.988,1087.837,1087.55,272.953,1040.007,1091.144,1076.712,1089.387,1091.077,1103.104,-360.56,-555.715,1072.081,1073.131,1073.201,1073.04,1089.257,1091.419,1072.888,1073.111,1073.141,1104.182,1075.399,1073.48,1103.711,1076.133,1072.456,1104.247,1105.181,1105.305,1072.744,1104.02,1106.105,1074.673,1102.759,1106.503,1076.082,1104.401,1108.177,1078.025,1109.772,1111.888,288.949,292.278,292.722,1112.981,1123.139,1124.493,1127.996,1134.943,1135.406,1136.071,1141.749,1142.506,1148.754,1150.764,1151.032,1156.093,1154.597,1154.497,1150.481,1124.218,1120.716,1108.646,1095.181,1093.385,1113.175,1089.783,1088.224,1719.795,-286.164,-553.625,1112.49,1099.064,1097.274,1106.601,1105.705,1105.645,1100.75,1110.143,1111.395,1114.044,1108.662,1107.944,1115.615,1114.492,1114.417,296.606,1011.559,1106.886,213.15,735.037,804.622,643.816,590.868,587.338,588.443,591.022,591.366,595.437,598.575,598.994,-70.23,562.977,605.191,608.988,14.755,-64.476,-86.326,490.189,567.057,573.015,572.841,572.83,576.607,578.243,578.461,579.714,1343.595,1445.446,585.277,588.153,588.345,588.809,588.843,588.848,592.898,591.447,591.254,592.538,589.623,589.429,590.233,587.901,587.59,589.276,603.526,605.426,691.889,810.649,818.566,889.948,866.466,863.335,164.367,164.895,164.966,158.056,851.73,897.975,164.51,806.786,892.423,894.069,892.465,892.251,153.408,842.909,888.876,151.935,802.889,889.683,154.817,146.446,145.329,888.428,193.529,147.202,885.088,224.817,136.78,147.255,802.581,889.958,888.64,889.975,890.064,1575.971,968.922,887.982,889.232,860.991,857.226,872.007,893.987,895.452,859.373,892.292,896.681,859.84,859.942,859.955,898.274,860.41,857.885,879.17,862.76,860.572,856.434,854.683,854.45,851.949,174.027,128.832,848.35,847.791,847.716,847.847,849.008,849.163,133.198,806.744,851.647,146.291,773.622,857.267,860.622,834.937,831.512,623.517,631.712,97.187,631.526,641.912,98.756,648.161,654.673,54.556,659.733,603.775,684.278,696.548,689.181,688.198,687.25,682.216,681.881,711.089,713.482,713.802,713.969,715.124,715.278,711.369,709.299,709.162,679.428,675.016,674.428,675.948,673.724,673.427,675.195,678.8,679.041,681.486,686.341,686.988,696.445,694.867,694.657,703.31,705.003,705.116,711.882,109.52,1269.549,1438.822,720.784,60.065,721.678,713.535,109.775,709.244,681.534,104.851,693.092,681.394,56.783,679.538,680.866,104.749,675.661,668.345,102.822,-69.364,-10.671,677.983,0),Y = c(0,-560.914,-86.294,12.57,10.336,0.861,-569.851,-582.641,-89.637,-513.461,-582.556,-89.624,-567.846,-574.869,-47.906,-567.25,-571.902,-87.985,-554.095,-574.651,-88.408,-557.127,-365.662,-30.472,-565.123,-564.378,-86.827,-380.282,-587.338,-90.36,-373.494,-394.186,-32.849,-15.305,-586.833,-90.282,-391.571,-603.611,-92.863,-392.659,-595.738,-49.645,-369.805,-585.56,-90.086,-607.653,-598.77,-92.118,-603.821,-362.726,-30.227,-590.569,-365.806,-56.278,-584.274,-594.203,-91.416,-378.246,-385.426,-32.119,-585.678,-569.332,-87.589,-572.327,-566.971,-87.226,-586.06,-584.482,-48.707,-593.944,-583.298,-89.738,-580.776,-590.48,-90.843,-600.616,-598.535,-49.878,-602.826,-29.44,-4.529,-618.062,-592.995,-91.23,-609.041,-411.878,-34.323,-413.262,-52.608,-8.094,-411.569,-415.928,-63.989,-48.63,-47.12,-3.927,-415.299,-599.916,-92.295,-44.314,-411.139,-63.252,-46.124,-48.509,-4.042,-407.485,-413.499,-63.615,-55.391,-415.922,-63.988,-57.45,-422.827,-35.236,-54.607,-414.86,-63.825,-51.45,-48.986,-7.536,-45.942,-413.839,-34.487,-411.075,-38.956,-5.993,-14.525,35.454,5.455,279.172,315.145,48.484,313.523,314.854,48.439,314.166,-89.369,-7.447,-125.57,-138.883,-157.401,-162.487,-158.14,-157.561,-171.662,-160.906,-160.189,-166.883,-171.154,-171.724,-187.379,-175.901,-174.37,-188.734,-185.215,-184.98,-189.818,-189.088,-188.991,283.22,302.83,305.444,270.066,289.663,290.969,275.119,276.953,277.198,260.347,261.677,261.855,246.632,250.916,251.202,238.808,246.008,246.968,240.285,253.356,255.099,251.122,262.086,262.817,258.677,268.433,269.734,264.812,273.771,274.966,268.696,276.112,276.606,268.26,275.135,276.051,265.545,272.265,273.161,264.077,268.116,268.385,261.015,266.217,266.911,259.724,265.892,266.714,259.602,265.93,266.352,258.209,265.537,266.514,259.411,265.687,266.523,259.761,266.978,267.459,260.97,268.01,268.949,262.4,271.867,273.13,264.803,273.569,274.153,266.979,275.25,276.353,268.073,274.926,275.84,269.625,279.424,280.077,271.785,281.741,283.068,276.083,284.162,285.239,279.865,291.741,292.533,291.52,302.124,303.538,300.685,313.141,314.802,313.594,327.048,327.945,325.593,339.796,341.69,341.58,355.702,357.585,356.602,381.738,383.413,370.869,394.752,397.936,391.659,409.376,411.739,415.584,433.281,434.461,437.478,454.368,456.62,445.173,427.865,425.558,383.004,370.373,369.531,382.343,399.016,401.239,413.831,422.157,423.267,432.015,448.845,449.967,425.956,482.75,487.1,501.178,503.055,508.065,42.339,492.242,494.125,494.376,484.667,484.368,484.328,467.742,464.267,464.035,468.697,433.854,429.208,400.85,393.727,392.777,376.735,23.546,885.955,500.323,481.941,40.162,530.014,516.432,514.621,494.618,478.341,477.256,456.114,458.922,459.297,456.514,425.5,421.365,462.707,472.408,473.055,467.768,454.51,452.742,441.741,201.307,169.249,167.631,163.795,163.539,160.509,161.775,161.943,161.298,397.997,429.557,419.546,177.979,161.875,418.022,423.34,424.049,413.55,419.645,420.457,407.472,415.496,416.031,407.006,148.509,114.042,409.442,164.128,131.419,109.849,407.293,427.122,418.844,426.465,427.481,417.779,161.317,127.123,409.12,367.918,365.171,395.673,396.298,396.381,388.306,392.859,393.466,473.545,400.213,395.325,366.771,370.934,371.489,339.469,387.501,393.905,386.022,390.883,391.207,378.721,394.352,396.436,384.581,45.245,380.042,375.485,31.29,335.187,323.048,49.7,359.169,361.068,361.195,348.111,362.176,364.051,383.932,392.518,393.663,349.699,392.668,395.532,391.36,396.741,397.458,387.906,391.383,391.846,371.849,378.048,378.461,318.908,357.694,362.865,342.973,40.35,288.149,259.291,276.577,-102.466,-12.055,241.977,234.288,36.044,247.849,244.967,20.414,290.17,258.996,39.846,269.854,259.002,39.847,297.18,266.569,22.214,-87.029,-89.617,-13.787,-95.348,-79.043,-76.869,20.581,272.257,289.812,292.152,271.933,287.652,289.748,276.443,284.481,285.017,-116.323,230.711,276.983,-141.084,229.411,278.81,314.961,280.304,277.994,277.686,282.62,283.278,287.306,282.107,281.414,290.288,291.936,292.045,288.296,302.772,304.702,292.15,302.242,303.588,-136.669,270.271,297.4,274.154,301.517,305.165,290.369,-426.184,-521.724,321.366,329.046,329.558,321.7,306.849,304.869,321.009,327.981,328.91,290.339,328.378,330.914,289.022,325.172,329.992,288.447,295.19,296.089,322.57,298.091,296.459,321.394,300.861,298.123,320.881,300.679,297.985,319.966,296.464,294.897,-164.893,-160.079,-159.437,278.924,281.357,281.682,269.259,274.752,275.118,267.165,265.143,264.873,258.509,264.614,265.429,256.744,266.259,266.893,263.361,301.473,306.555,262.416,313.245,320.022,281.712,320.535,323.123,-194.846,-521.034,-564.526,274.367,278.267,278.787,264.133,268.555,268.85,256.818,266.194,267.444,260.084,265.714,266.464,262.669,272.063,272.689,-175.62,225.568,279.06,-159.431,216.327,266.428,245.597,254.043,254.606,244.851,255.798,257.258,248.152,256.508,257.622,-319.948,219.787,255.769,243.947,-235.689,-299.64,-334.288,164.071,230.518,224.188,233.409,234.024,228.544,236.363,237.405,235.462,-116.121,-162.999,240.889,251.796,252.524,246.8,258.72,260.309,257.726,267.828,269.175,265.087,277.995,278.856,273.876,287.09,288.852,280.858,292.608,294.174,246.387,213.675,211.494,201.469,209.375,210.429,-217.38,-218.456,-218.6,-232.09,196.612,225.193,-248.699,166.3,221.633,213.54,219.095,219.836,-261.438,192.469,222.729,-265.48,164.234,221.529,-254.99,-254.199,-254.094,209.197,-218.172,-246.663,207.52,-196.823,-250.735,-261.477,163.4,220.05,210.838,222.305,223.069,-196.463,176.753,226.515,217.024,249.012,253.277,203.798,226.753,228.283,241.225,228.683,227.011,235.665,243.606,244.664,212.621,240.812,242.692,207.285,207.473,207.498,194.745,202.346,203.359,191.408,-221.759,-249.304,186.022,192.304,193.141,182.101,191.751,193.037,-260.225,162.901,191.109,-252.884,141.317,193.877,186.754,220.72,225.249,304.742,316.744,48.73,334.833,352.419,54.218,368.749,378.178,31.515,378.548,339.912,385.233,406.951,373.432,368.963,411.64,362.786,359.529,358.897,353.738,353.051,336.054,340.344,340.915,335.944,355.461,356.762,345.061,372.321,375.955,376.513,404.13,407.812,416.338,426.28,426.943,432.67,429.483,429.058,426.368,425.226,425.074,416.298,411.677,411.369,345.327,53.127,183.69,208.182,261.561,21.797,298.1,281.941,43.375,314.226,304.413,46.833,374.171,368.525,30.71,359.118,367.905,56.601,287.494,285.213,43.879,735.244,113.115,304.619,0)),row.names = 31537:32536,class = "data.frame")

解决方法

这应该为您提供一种获取所需图像的方法。但是,我没有您的原始TIFF,因此这里的对齐方式不正确,因为我必须在问题中使用png的剪切粘贴版本。

无论如何,我将使用的方法是:

  1. 将图像转换为栅格
  2. 将栅格转换为grid::rasterGrob
  3. 使用annotation_custom
  4. 将rasterGrob绘制为ggplot中的第一层
  5. 正常绘制其他图层。

这是一个例子:

library(ggplot2)
library(rtiff)
library(grid)

x <- readTiff('F01_screenshot.tiff')
pic <- as.raster(array(c(x@red,x@green,x@blue),c(x@size,3)))
picgrob <- rasterGrob(pic)

ggplot(eet2,aes(x=X,y= Y)) +
  annotation_custom(picgrob) +
  geom_point() + 
  stat_density2d() +
  coord_equal()

enter image description here

您可能需要缩放Y轴数据以使其与图片的纵横比匹配。

作为示例,如果我们假设max(eet2$Y)是图像的顶部边缘,而min(eet2$Y)是图像的底部边缘,并且还假设min(eet2$X)是图像的顶部边缘和{{1 }}的右边缘(正如您所建议的那样,您可以在注释中使用这种情况),我们可以将图片与以下数据结合起来:

max(eet2$X)

enter image description here

如果对齐方式不正确,则我们需要数据中不存在的额外校准信息(即pic_ratio <- dim(pic)[2]/dim(pic)[1] data_ratio <- diff(range(eet2$X)) / diff(range(eet2$Y)) eet2$Y <- eet2$Y * data_ratio / pic_ratio ggplot(eet2,y= Y)) + annotation_custom(picgrob) + geom_point() + stat_density2d() + coord_equal(xlim = range(eet2$X),ylim = range(eet2$Y)) 的值代表图像的顶部和底部,以及什么?值eet2$Y代表左右边缘。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...