我能够绘制地图和标题的特定点:
library(maps)
map("state")
text(-80.83,35.19,"Charlotte",cex=.6)
我还可以绘制围绕该点为圆心:
symbols(-80.83,35.19,circles=2, add=TRUE)
不过,我想控制圆的大小。我特别想画一个圆圈周围包含在data.frame,矩阵或列表中的多个位置的100个英里的半径。
您可以编写一个函数来定制你怎么想圆一下。例如:
plotCircle <- function(x, y, r) {
angles <- seq(0,2*pi,length.out=360)
lines(r*cos(angles)+x,r*sin(angles)+y)
}
然后,如果你有一组数据帧中的坐标:
coords <- data.frame(x = c(-1,0,1), y = c(-1, 0.5, 1))
你可以用一些初始图(地图,或空情节等)开始
plot(1,type='n',xlim=c(-2,2),ylim=c(-2,2))
然后调用在你的坐标列表中的绘图功能:
apply(coords,1,function(df) plotCircle(df[1],df[2],.3))
加里的命题非常适于平面地图,但不能应用于由“映射”包产生的地图,因为它并不需要用于绘制地图的投影的照顾。严格应用,因为这,它导致elipse(见下文)的图,这是因为圆半径的单位是度,但不千米或英里。但在纬度和经度度不对应相同的物理距离。为了以防万一画一个点一个圆圈,或东西是接近圆形,半径为英里或公里含量的不同距离,你需要计算有关的投影校正坐标。以你的功能和适应它关于对http://www.movable-type.co.uk克里斯俯伏的解释,你的函数变成了:
library(maps)
library(mapdata)#For the worldHires database
library(mapproj)#For the mapproject function
plotElipse <- function(x, y, r) {#Gary's function ;-)
angles <- seq(0,2*pi,length.out=360)
lines(r*cos(angles)+x,r*sin(angles)+y)
}
plotCircle <- function(LonDec, LatDec, Km) {#Corrected function
#LatDec = latitude in decimal degrees of the center of the circle
#LonDec = longitude in decimal degrees
#Km = radius of the circle in kilometers
ER <- 6371 #Mean Earth radius in kilometers. Change this to 3959 and you will have your function working in miles.
AngDeg <- seq(1:360) #angles in degrees
Lat1Rad <- LatDec*(pi/180)#Latitude of the center of the circle in radians
Lon1Rad <- LonDec*(pi/180)#Longitude of the center of the circle in radians
AngRad <- AngDeg*(pi/180)#angles in radians
Lat2Rad <-asin(sin(Lat1Rad)*cos(Km/ER)+cos(Lat1Rad)*sin(Km/ER)*cos(AngRad)) #Latitude of each point of the circle rearding to angle in radians
Lon2Rad <- Lon1Rad+atan2(sin(AngRad)*sin(Km/ER)*cos(Lat1Rad),cos(Km/ER)-sin(Lat1Rad)*sin(Lat2Rad))#Longitude of each point of the circle rearding to angle in radians
Lat2Deg <- Lat2Rad*(180/pi)#Latitude of each point of the circle rearding to angle in degrees (conversion of radians to degrees deg = rad*(180/pi) )
Lon2Deg <- Lon2Rad*(180/pi)#Longitude of each point of the circle rearding to angle in degrees (conversion of radians to degrees deg = rad*(180/pi) )
polygon(Lon2Deg,Lat2Deg,lty=2)
}
map("worldHires", region="belgium")#draw a map of Belgium (yes i am Belgian ;-)
bruxelles <- mapproject(4.330,50.830)#coordinates of Bruxelles
points(bruxelles,pch=20,col='blue',cex=2)#draw a blue dot for Bruxelles
plotCircle(4.330,50.830,50)#Plot a dashed circle of 50 km arround Bruxelles
plotElipse(4.330,50.830,0.5)#Tries to plot a plain circle of 50 km arround Bruxelles, but drawn an ellipse
(对不起我的“名声”不允许我发表图片;-)。编辑:增加了你的形象。
我希望这有帮助。格雷瓜尔