简体   繁体   English

r plotly 组合气泡和等值线 map 使用 lat lon 而不是 fips 代码

[英]r plotly combined bubble and choropleth map using lat lon instead of fips code

I have a dataset with columns "state_name", "county_name", "value1", "value2", "lat", and "lon".我有一个包含“state_name”、“county_name”、“value1”、“value2”、“lat”和“lon”列的数据集。 I've tried to apply the examples from the Plotly website and other resources with my data but I'm not getting any luck.我尝试将 Plotly 网站和其他资源中的示例与我的数据一起应用,但我没有任何运气。 They only seem to have an example with the fipscode, and I'm not sure how to use "lat" and "lon" information on my code.他们似乎只有 fipscode 的示例,我不确定如何在我的代码中使用“lat”和“lon”信息。

This is the example code I found from the plotly website.这是我从 plotly 网站上找到的示例代码。 Is there a way to plot the map with only the "lat" and "lon" information without having to use counties JSON and the fips code?有没有办法使用 plot map 仅具有“纬度”和“经度”信息而不必使用县 JSON 和 fips 代码?

library(rjson)
library(plotly)

url <- 'https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json'
counties <- rjson::fromJSON(file=url)
url2<- "https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv"
df <- read.csv(url2, colClasses=c(fips="character"))
fig <- plot_ly() 
fig <- fig %>% add_trace(
    type="choroplethmapbox",
    geojson=counties,
    locations=df$fips,
    z=df$unemp,
    colorscale="Viridis",
    zmin=0,
    zmax=12,
    marker=list(line=list(
      width=0),
      opacity=0.5
    )
  )
fig <- fig %>% layout(
    mapbox=list(
      style="carto-positron",
      zoom =2,
      center=list(lon= -95.71, lat=37.09))
  )
fig

Given the example you provided, I assume you want to color the counties by either value1 or value2.鉴于您提供的示例,我假设您想按 value1 或 value2 为县着色。 In this case, you believe you need the geojson as these draw the county's boundaries so that the color gradient can be applied.在这种情况下,您认为您需要 geojson,因为它们绘制了县的边界,以便可以应用颜色渐变。 Depending on what you are looking for:根据您要查找的内容:

  • If you are looking for the fill counties style (strictly choropleth map), you can potentially convert the fips code from the state_name and county_name using this dataset ( Counties and FIPS Dataset );如果您正在寻找填充县样式(严格的等值线图),您可以使用此数据集( 县和 FIPS 数据集)潜在地从 state_name 和 County_name 转换 fips 代码; you can merge by the county and state to obtain the FIPS codes.您可以按县和 state 合并以获得 FIPS 代码。
  • If you are not looking for a choropleth map, you might not need the geojson object;如果您不是在寻找等值线 map,您可能不需要 geojson object; a bubble map might be more suitable ( Plotly bubble Maps ) as you only need the lat and long to plot this.气泡 map 可能更合适( Plotly 气泡图),因为您只需要 plot 的纬度和经度。

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

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