我正在使用choroplethr为美国西部半岛(州和县)制作气候图。地图已经完成并且看起来很棒。现在我需要将出现点(lon / lat)绘制到地图上。我尝试了很多功能(lat / lon和lon / lat,两者都有)。不幸的是,到目前为止我还没有找到解决方案。怎么可以这样做?
PS(2018年3月18日):我已尝试(例如)以下陈述来绘制出现点,它似乎被接受,没有错误,但也没有反应: NAM_prt = NAM_prt + geom_point(data = spec_US_df,aes(x = lon,y = lat),color =“red”,size = 30,alpha = 0.5)
library(choroplethr)
# Preparing the input data.frame containing "region" and "value"
state = c('ARI')
county = c('Apache', 'Cochise', 'Coconino', 'Gila', 'Graham', 'Greenlee', 'La Paz', 'Maricopa', 'Mohave', 'Navajo', 'Pima', 'Pinal', 'Santa Cruz', 'Yavapai', 'Yuma')
region = c(4001, 4003, 4005, 4007, 4009, 4011, 4012, 4013, 4015, 4017, 4019, 4021, 4023, 4025, 4027)
value = c(40, 88, 19, 10, 10, 0, 0, 302, 47, 0,222, 9, 0, 34, 40)
SWNAMcounties = data.frame(state = state, county = county, region = region, value = value)
# Preparing the choropleth map
NAM_prt = county_choropleth(SWNAMcounties,
title = "County Climate Map (Data from 1990 to 1995)",
legend = "Impact",
num_colors = 1,
state_zoom = c("arizona"))
NAM_prt
# part 2:
# Creating occurrence points (lat/lon)
lat = c('32.22528', '32.36194', '32.66156', '32.38121', '32.69486', '32.36842', '32.51652')
lon = c('-111.1206', '-109.6742', '-110.1742', '-109.6278', '-109.4554', '-109.5601', '-110.0397')
spec_US_df = data.frame(lat, lon)
强文
答案 0 :(得分:0)
在choroplethr中,所有<region>_choropleth
函数都返回ggplot2对象。这意味着您可以使用+
运算符添加另一个图层(例如,在您的情况下为点)。
首先,我看到了如何定义spec_US_df
的问题。也就是说,纬度和经度值应该是数字,但是你将它们作为字符串。首先,我首先重写您的数据创建代码,如此
# part 2:
# Creating occurrence points (lat/lon)
lat = c(32.22528, 32.36194, 32.66156, 32.38121, 32.69486, 32.36842, 32.51652)
lon = c(-111.1206, -109.6742, -110.1742, -109.6278, -109.4554, -109.5601, -110.0397)
spec_US_df = data.frame(lat, lon)
接下来,在将其与choroplethr组合之前,实际生成所需的图层会有所帮助。这就是我所拥有的:
library(ggplot2)
ggplot(spec_US_df) +
geom_point(mapping = aes(lon, lat), color="red", size = 30, alpha=0.5)
最后,结合两者:
NAM_prt +
geom_point(data = spec_US_df, mapping = aes(lon, lat), color="red", size = 30, alpha=0.5, inherit.aes = FALSE)
关键区别在于,在将原始ggplot2代码与choroplethr组合时,还需要添加参数inherit.aes = FALSE
。这是因为choroplethr具有您不需要的美学参数(它按州和县分组多边形,这与您的散点图无关)。