【问题标题】:How do I plot occurrence points (lon/lat) on a choropleth map?如何在等值线图上绘制出现点(经度/纬度)?
【发布时间】:2018-08-26 14:34:18
【问题描述】:

我正在使用 choroplethr 为美国西半部(州和县)创建气候地图。地图已完成,看起来很棒。现在我需要在地图上绘制出现点(经度/纬度)。我尝试了许多功能(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)

强文本

【问题讨论】:

    标签: r choropleth choroplethr


    【解决方案1】:

    在 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 有一个您不需要的美学参数(它按州和县对多边形进行分组,这与您的散点图无关)。

    【讨论】:

    • 非常感谢,阿里。为这个错误道歉,我应该注意到我自己将 lon/lat 值作为字符串。亲切的问候,佩特拉
    • 太棒了!如果它回答了您的问题,请您投票并接受它吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多