如何在等值线图上绘制出现点 (lon/lat)?
How do I plot occurrence points (lon/lat) on a choropleth map?
我正在使用 choroplethr 创建美国西半部(州和县)的气候地图。地图已经完成,看起来很棒。现在我需要将发生点 (lon/lat) 绘制到地图上。我尝试了很多功能(lat/lon 和 lon/lat,两者都有)。不幸的是,到目前为止我还没有找到解决方案。这怎么可能?
PS (Mar 18, 2018): 我试过(例如)下面的语句来绘制出现点,似乎可以接受,没有错误,但也没有反应:
NAM_prt = NAM_prt + geom_point(数据=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)
强文本
在 choroplethr 中,所有 <region>_choropleth
函数 return 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 有一个你不需要的美学参数(它按州和县对多边形进行分组,这与你的散点图无关)。
我正在使用 choroplethr 创建美国西半部(州和县)的气候地图。地图已经完成,看起来很棒。现在我需要将发生点 (lon/lat) 绘制到地图上。我尝试了很多功能(lat/lon 和 lon/lat,两者都有)。不幸的是,到目前为止我还没有找到解决方案。这怎么可能?
PS (Mar 18, 2018): 我试过(例如)下面的语句来绘制出现点,似乎可以接受,没有错误,但也没有反应: NAM_prt = NAM_prt + geom_point(数据=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)
强文本
在 choroplethr 中,所有 <region>_choropleth
函数 return 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 有一个你不需要的美学参数(它按州和县对多边形进行分组,这与你的散点图无关)。