我想用lat,lon绘制一些数据,其中点的颜色为值,三个指定的轮廓水平叠加在顶部。我似乎无法使stat_contour或geom_contour工作,因为我的数据不是完美的网格格式,我似乎无法使stat_density_2d工作,因为1)我感兴趣的是值而不是点密度,2 )即使我修改我的数据使其看起来像一个密度数据集,我似乎无法弄清楚如何只绘制我感兴趣的特定轮廓线。我还尝试用contourLines()获得单独的轮廓线但这也让我失败了。
以下是我正在处理的数据示例。
Lon Lat Data
-76.2834 39.7619 26.0
-76.2823 39.7696 125.6
-76.2811 39.7774 134.2
-76.2800 39.7852 105.7
-76.2789 39.7930 61.6
-76.2778 39.8008 9.9
-76.2530 39.7544 313.4
-76.2519 39.7622 500.5
-76.2508 39.7700 554.2
-76.2497 39.7778 542.5
-76.2485 39.7856 505.2
-76.2474 39.7934 456.8
-76.2463 39.8011 402.8
-76.2452 39.8089 345.5
-76.2441 39.8167 286.3
-76.2429 39.8245 225.8
-76.2418 39.8323 164.4
-76.2407 39.8401 102.3
-76.2396 39.8479 39.8
-76.2215 39.7547 68.7
-76.2204 39.7625 229.8
-76.2193 39.7703 341.0
-76.2182 39.7781 409.9
-76.2170 39.7859 445.9
-76.2159 39.7937 457.8
-76.2148 39.8015 452.2
-76.2137 39.8093 433.9
-76.2125 39.8171 406.3
-76.2114 39.8248 371.7
-76.2103 39.8326 331.9
-76.2092 39.8404 288.1
-76.2080 39.8482 241.1
-76.2069 39.8560 191.6
-76.2058 39.8638 140.0
-76.2047 39.8716 86.8
-76.2035 39.8794 32.2
-76.1878 39.7706 82.0
-76.1867 39.7784 179.4
-76.1855 39.7862 251.9
-76.1844 39.7940 303.1
-76.1833 39.8018 336.3
-76.1822 39.8096 354.6
-76.1810 39.8174 360.6
-76.1799 39.8252 356.5
-76.1788 39.8330 344.0
-76.1777 39.8407 324.7
-76.1765 39.8485 299.6
-76.1754 39.8563 269.8
-76.1743 39.8641 235.9
-76.1731 39.8719 198.6
-76.1720 39.8797 158.4
-76.1709 39.8875 115.7
-76.1698 39.8953 70.9
-76.1686 39.9031 24.2
-76.1540 39.7865 17.5
-76.1529 39.7943 91.7
-76.1518 39.8021 150.1
-76.1507 39.8099 194.5
-76.1495 39.8177 226.5
-76.1484 39.8255 247.8
-76.1473 39.8333 259.7
-76.1461 39.8411 263.3
-76.1450 39.8488 259.9
-76.1439 39.8566 250.3
-76.1427 39.8644 235.2
-76.1416 39.8722 215.5
-76.1405 39.8800 191.5
-76.1393 39.8878 164.0
-76.1382 39.8956 133.2
-76.1371 39.9034 99.7
-76.1359 39.9112 63.5
-76.1348 39.9189 25.2
-76.1180 39.8180 44.5
-76.1169 39.8258 85.3
-76.1157 39.8336 116.7
-76.1146 39.8414 139.7
-76.1135 39.8492 155.0
-76.1123 39.8569 163.6
-76.1112 39.8647 166.0
-76.1101 39.8725 163.0
-76.1089 39.8803 155.0
-76.1078 39.8881 142.6
-76.1067 39.8959 126.2
-76.1055 39.9037 106.2
-76.1044 39.9115 82.9
-76.1032 39.9192 56.8
-76.1021 39.9270 27.9
-76.0819 39.8495 6.8
-76.0808 39.8572 30.9
-76.0797 39.8650 48.6
-76.0785 39.8728 60.5
-76.0774 39.8806 67.0
-76.0762 39.8884 68.7
-76.0751 39.8962 65.9
-76.0740 39.9040 59.0
-76.0728 39.9118 48.3
-76.0717 39.9195 34.3
-76.0705 39.9273 17.0
这是我现在的代码。我想在同一个图上看到p1和p2的效果,但是ggplot不会让我在同一个图上实现两种不同的颜色方案:
library(ggplot2)
## Read Data from CSV file ##
data <- read.table("stack.csv",header=TRUE,sep=",")
## Rework the data to look like a density plot
n <- length(data[,1])
for (i in 1:n)
{
v <- as.integer(data[i,4]/50)
lon <- rep(data[i,2],v)
lat <- rep(data[i,3],v)
z <- rep(data[i,4],v)
if (i == 1) {d <- data.frame(lon,lat,z)}
else
{
dd <- data.frame(lon,lat,z)
d <- rbind(d,dd)
}
}
# Plot data
p1 <- ggplot(data,aes(x=Lon,y=Lat,z=Data,colour=Data)) + geom_point() +
scale_color_gradientn(colors=c("grey95","yellow2","firebrick"),na.value="white",values=c(0.0,0.85,0.95,1.0)) +
xlim(-76.31,-76.05) +
ylim(39.74,39.94) +
theme(panel.background=element_blank(), axis.ticks=element_blank())
# Get contour
p2 <- ggplot(d,aes(x=lon,y=lat)) +
stat_density_2d(aes(color=..level..),lwd=0.3) +
scale_color_gradientn(colors=c("white","white","grey60","grey95","white"),
na.value="white",values=c(0.0,0.85,0.90,0.95,1.0)) +
xlim(-76.31,-76.05) +
ylim(39.74,39.94) +
theme(panel.background=element_blank(), axis.ticks=element_blank())
非常感谢任何帮助。哦,请放轻松,因为这是我的第一篇文章...
这是dput的输出:
structure(list(X = c(401L, 601L, 801L, 1001L, 1201L, 1401L, 5001L,
5201L, 5401L, 5601L, 5801L, 6001L, 6201L, 6401L, 6601L, 6801L,
7001L, 7201L, 7401L, 9801L, 10001L, 10201L, 10401L, 10601L, 10801L,
11001L, 11201L, 11401L, 11601L, 11801L, 12001L, 12201L, 12401L,
12601L, 12801L, 13001L, 15001L, 15201L, 15401L, 15601L, 15801L,
16001L, 16201L, 16401L, 16601L, 16801L, 17001L, 17201L, 17401L,
17601L, 17801L, 18001L, 18201L, 18401L, 20201L, 20401L, 20601L,
20801L, 21001L, 21201L, 21401L, 21601L, 21801L, 22001L, 22201L,
22401L, 22601L, 22801L, 23001L, 23201L, 23401L, 23601L, 25801L,
26001L, 26201L, 26401L, 26601L, 26801L, 27001L, 27201L, 27401L,
27601L, 27801L, 28001L, 28201L, 28401L, 28601L, 31401L, 31601L,
31801L, 32001L, 32201L, 32401L, 32601L, 32801L, 33001L, 33201L,
33401L), Lon = c(-76.2833766152044, -76.2822614845223, -76.2811461257035,
-76.2800305386072, -76.2789147230922, -76.2777986790175, -76.2530102918317,
-76.2518918288511, -76.2507731365048, -76.2496542146515, -76.2485350631496,
-76.2474156818575, -76.2462960706334, -76.2451762293357, -76.2440561578225,
-76.2429358559517, -76.2418153235816, -76.2406945605699, -76.2395735667744,
-76.2215285318079, -76.220406508839, -76.2192842551349, -76.2181617705536,
-76.217039054953, -76.215916108191, -76.2147929301251, -76.2136695206132,
-76.2125458795127, -76.2114220066812, -76.210297901976, -76.2091735652545,
-76.2080489963739, -76.2069241951914, -76.2057991615641, -76.2046738953489,
-76.2035483964028, -76.1877948538244, -76.1866688068276, -76.1855425274412,
-76.1844160155223, -76.1832892709281, -76.1821622935157, -76.1810350831421,
-76.1799076396641, -76.1787799629386, -76.1776520528224, -76.176523909172,
-76.1753955318441, -76.1742669206952, -76.1731380755816, -76.1720089963598,
-76.170879682886, -76.1697501350163, -76.1686203526068, -76.1540454940392,
-76.1529154172824, -76.1517851064791, -76.1506545614859, -76.149523782159,
-76.1483927683548, -76.1472615199296, -76.1461300367394, -76.1449983186405,
-76.1438663654888, -76.1427341771401, -76.1416017534504, -76.1404690942754,
-76.1393361994707, -76.138203068892, -76.1370697023948, -76.1359360998344,
-76.1348022610662, -76.1180119900133, -76.116877406209, -76.1157425864104,
-76.1146075304732, -76.1134722382528, -76.1123367096047, -76.1112009443841,
-76.1100649424463, -76.1089287036465, -76.1077922278398, -76.1066555148811,
-76.1055185646255, -76.1043813769276, -76.1032439516424, -76.1021062886244,
-76.0819456814837, -76.0808065776724, -76.0796672359135, -76.0785276560616,
-76.0773878379713, -76.0762477814972, -76.0751074864935, -76.0739669528148,
-76.0728261803152, -76.0716851688488, -76.0705439182699), Lat = c(39.7618502813269,
39.7696398264863, 39.7774293547284, 39.7852188660436, 39.7930083604226,
39.8007978378559, 39.7544048721348, 39.7621941870797, 39.7699834849497,
39.7777727657353, 39.7855620294269, 39.7933512760152, 39.8011405054906,
39.8089297178436, 39.8167189130646, 39.8245080911442, 39.8322972520729,
39.8400863958411, 39.8478755224393, 39.7547404892594, 39.7625295547245,
39.7703186029472, 39.7781076339179, 39.7858966476271, 39.7936856440653,
39.8014746232228, 39.8092635850901, 39.8170525296577, 39.824841456916,
39.8326303668554, 39.8404192594663, 39.8482081347393, 39.8559969926646,
39.8637858332328, 39.8715746564343, 39.8793634622594, 39.770645180057,
39.7784339588547, 39.7862227202227, 39.7940114641515, 39.8018001906315,
39.8095888996529, 39.8173775912063, 39.825166265282, 39.8329549218704,
39.8407435609618, 39.8485321825466, 39.8563207866153, 39.8641093731582,
39.8718979421656, 39.8796864936279, 39.8874750275354, 39.8952635438786,
39.9030520426478, 39.786540246803, 39.7943287358635, 39.8021172073064,
39.809905661122, 39.8176940973007, 39.8254825158328, 39.8332709167086,
39.8410592999185, 39.8488476654527, 39.8566360133016, 39.8644243434555,
39.8722126559047, 39.8800009506396, 39.8877892276504, 39.8955774869274,
39.903365728461, 39.9111539522414, 39.9189421582589, 39.8180020475419,
39.8257902081697, 39.8335783509717, 39.8413664759382, 39.8491545830594,
39.8569426723257, 39.8647307437274, 39.8725187972546, 39.8803068328976,
39.8880948506467, 39.8958828504922, 39.9036708324242, 39.911458796433,
39.919246742509, 39.9270346706422, 39.8494529349797, 39.8572407633009,
39.8650285735872, 39.8728163658288, 39.8806041400158, 39.8883918961386,
39.8961796341873, 39.9039673541522, 39.9117550560233, 39.9195427397909,
39.9273304054451), Data = c(26.01872, 125.5967, 134.2395, 105.6832,
61.55046, 9.894023, 313.3874, 500.5094, 554.2007, 542.473, 505.2249,
456.8239, 402.7803, 345.5363, 286.3161, 225.7946, 164.3729, 102.3032,
39.75208, 68.7181, 229.8339, 340.999, 409.853, 445.9147, 457.7944,
452.1698, 433.8613, 406.2562, 371.7264, 331.9475, 288.1201, 241.1184,
191.59, 140.0223, 86.78784, 32.17551, 82.0317, 179.4106, 251.9416,
303.1165, 336.325, 354.6191, 360.617, 356.498, 344.0441, 324.6988,
299.6271, 269.7693, 235.8863, 198.5966, 158.4054, 115.7276, 70.90639,
24.22691, 17.54184, 91.71716, 150.1082, 194.4878, 226.5373, 247.7999,
259.6597, 263.3375, 259.8975, 250.2603, 235.2186, 215.4528, 191.5466,
164.001, 133.2463, 99.6526, 63.5395, 25.18322, 44.46111, 85.26085,
116.6825, 139.6546, 155.0307, 163.5872, 166.0246, 162.9709, 154.9872,
142.5729, 126.172, 106.1784, 82.94203, 56.77329, 27.94799, 6.796898,
30.87994, 48.59637, 60.4822, 67.03131, 68.69669, 65.89231, 58.99547,
48.34937, 34.26572, 17.02743)), .Names = c("X", "Lon", "Lat",
"Data"), class = "data.frame", row.names = c(NA, -98L))
答案 0 :(得分:0)
好的,我明白了。我使用contoureR包从不规则网格中获取单个轮廓线,然后在那里绘制:
p1 <- ggplot(dat,aes(x=Lon,y=Lat,z=Data,colour=Data)) + geom_point() +
scale_color_gradientn(colors=c("grey95","yellow2","firebrick"),na.value="white",values=c(0,0.25,0.75,1.0)) +
geom_path(data=df,mapping=aes(x=df$Lon,y=df$Lat),
linetype=1,color="black",lwd=0.25,alpha=0.5) +
theme(panel.background=element_blank(), axis.ticks=element_blank())
在最后一行中,我创建了一个数据框,其列名与数据中的列名相同,因为我无法使ggplot部分工作:
import numpy as np
a = np.array([1, 4, 5, 7, 9, 14, 15, 17, 18, 19, 21, 22, 23, 25, 26])
b = np.array([8, 28, 12, 3, 24, 16, 23, 19, 14, 2, 11, 29, 27, 6, 13])
print(np.count_nonzero(a==b))
当我尝试使用getContourLines的输出时,geom_path()部分给了我各种错误,因此我将相关列复制到数据框,这只有在我给出与dat中的列名相同的列名时才有效。然后它奏效了!