用地图和点绘制矩阵

时间:2015-03-07 23:10:15

标签: r

我有一个包含值0和1的矩阵A(lat,lon)。 我想绘制一张世界地图,其中1有圆圈点重叠 带有轮廓的白色全球地图。

我在网上找到了一些例子但是我一直在遇到问题 超出我的。我试过"图像"但是我错误地绘制了数据并且点太小而无法看到。我是R的新手,想要一个简单的情节来开始学习如何在R中绘制地图,以及添加叠加。

非常感谢任何帮助。我正在使用R版本:R版本3.1.2(2014-10-31)南瓜头盔。

我已尝试过此代码:

 library(maps)
 library(mapdata)
 library(maptools)
 map(database = 'world',
 xlim = c(-180, 180),
 ylim = c(-90, 90),
 fill = T,
 col = 'white',
 resolution = 0,
 bg = 'white',
 mar = c(1,1,2,1))
 points(dcn18,pch = 21,cex = 2,col = 'red')
 title(xlab = 'Longitude',ylab = 'Latitude',main = '')
 axis(1, labels = T) 
 axis(2, labels = T)
 grid()
 box()

此代码效果很好,可以生成一个漂亮的地图。但我不确定如何将这些点添加为圆圈,当这些点在矩阵dcn18(lat,lon)中为1和0时,我只得到一个红色的圆圈lat 0和lon 0.我想我需要将lon和lat数据映射到只有1的dcn18,收集这些有效点并将它们作为数据帧传递给点。但我不知道如何在R中做到这一点。

我正在玩的另一个解决方案,更接近,但因为我的数据有NaN,其他问题,我认为"图像"不是一个好的解决方案。

map(database = 'world',
    xlim = c(-180, 180),
    ylim = c(-90, 90),
    fill = T,
    col = 'white',
    resolution = 0,
    bg = 'white',
    mar = c(1,1,2,1))
image(seq(-180,180),seq(-90,90),dcn18.l180, xlim=c(-180,180), 
      ylim=c(-90,90), col = heat.colors(12),fill=FALSE,
      add =TRUE)

title(xlab = 'Longitude',ylab = 'Latitude',main = '')
axis(1, labels = T) 
axis(2, labels = T)
grid()
box()

K2pjP.png

1 个答案:

答案 0 :(得分:1)

坐标需要按照经度/纬度顺序绑定在一起:

#Data Preparation: 
Lat <- c("-80", "50", "-20", "0", "20", "-50", "80" ) 
Lon <- c("-150", "-100", "-50", "0", "50", "100", "150" )

#Bind your two columns
LonLat <- cbind(Lon, Lat)

#Draw your world map here

#Draw your points on the map
points(LonLat,pch = 21,cex = 2,col = 'red')
相关问题