简体   繁体   English

带有地图和点的绘图矩阵

[英]Plotting matrix with map and points

I have a matrix A(lat,lon) that contains the values 0s and 1s. 我有一个矩阵A(lat,lon),其中包含值0和1。 I would like to plot a world map where the 1 have circles of dots overlaid on global map that is white with outlines. 我想绘制一张世界地图,其中1的点圆覆盖在带有白色轮廓的全局地图上。

I've tried some examples in I found online but I keep running into issues of that are beyond me. 我在网上找到了一些例子,但我一直遇到超出我范围的问题。 I've tried "image" but I plotted the data incorrectly and the dots were too small to see. 我尝试使用“图像”,但是我错误地绘制了数据,点太小而看不到。 I'm new to R and would like a simple plot to start learning how to plot maps in R, plus adding overlays. 我是R的新手,并希望通过一个简单的图解开始学习如何在R中绘制地图,以及添加叠加层。

Any help would be greatly appreciated. 任何帮助将不胜感激。 I'm using R version: R version 3.1.2 (2014-10-31) Pumpkin Helmet. 我正在使用R版本:R版本3.1.2(2014-10-31)南瓜头盔。

I've tried this code: 我已经试过这段代码:

 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()

This code works well and produces a nice map. 这段代码运行良好,并生成了一张漂亮的地图。 But I'm not sure how to add the points, as circles, when these points are in the matrix dcn18(lat,lon) as 1's and 0's, I get only a red circle at lat 0 and lon 0. I think I need to map the lon and lat data to the dcn18 where there are only 1, collect these valid points and pass them as a dataframe to points. 但是我不确定如何将这些点添加为圆,当这些点在矩阵dcn18(lat,lon)中分别为1和0时,在lat 0和lon 0处只有一个红色圆圈。我想我需要将lon和lat数据映射到只有1个的dcn18,收集这些有效点并将它们作为数据帧传递给点。 But I have no idea how to do this in R. 但是我不知道如何在R中执行此操作。

Another solution I've playing around with, comes a bit closer, but because my data has NaNs, among other issues, I think "image" is not a good solution. 我正在尝试的另一种解决方案离我们更近了一些,但是由于我的数据存在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

The coordinates need to be binded together in this order Longitude/Latitude: 坐标需要按以下顺序绑定在一起:经度/纬度:

#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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM