[英]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:
根据您要查找的内容:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.