在 Shinny Web 应用程序中绘制缺失状态的绘图 [英] Plotly map missing states in Shinny web application

查看:45
本文介绍了在 Shinny Web 应用程序中绘制缺失状态的绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Shinny 中的 Plotly 创建一个交互式绘图,以显示按人口颜色编码的所有美国县.但是,当我将以下代码作为 server.R 函数的一部分运行时,缺少加利福尼亚州、阿拉巴马州、亚利桑那州、科罗拉多州和阿肯色州:

I am trying to create an interactive plot with Plotly in Shinny that shows all American counties color-coded by population. However, the state of California, Alabama, Arizona, Colorado, and Arkansas are missing when I run the following code as a part of the server.R function:

output$countyPolygonMap <- renderPlotly({
        url <- 'https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json'
        countyGeo <- rjson::fromJSON(file=url)
        ## Obtaining the geographical file for all U.S. counties
        url2<- "https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv"
        df <- read.csv(url2, colClasses=c(fips="character"))
        names(df)[2] = "unemployment"
        ## obtaining the unemployment rate data 
        url3 <- "https://storage.googleapis.com/kagglesdsdata/datasets%2F579969%2F1374720%2Fus_county.csv?GoogleAccessId=gcp-kaggle-com@kaggle-161607.iam.gserviceaccount.com&Expires=1596139521&Signature=easqHBFZ757D%2F7LVyDM%2BF%2FIMU6l2OEY6giqVvIC0l0tSSe%2Fohq6NC%2FLFKbsIV6FdFALmPUqG9vATbg0cuRVVwGQMsoUOjlW%2BZLhTVluxbYh1dDE1MTFzWRpzlSH18ejIwqa61F0ARJ%2Bpq6ryIfJuE7wQQ1rOCEpaVB9m%2FP7QaZm2gBJeHYLXJXcvO8w1p0sEnqRsGAesg2Fgj%2Bv8unPGNtDJekEWuNbl1K9k7CAaZWjG2QQ94LB9tAPvfKqykDWDD7w6yN3YFkcfu7kUmjs0CybnMD6IP%2FM5hvJXuUTIie0MOMTWt5bIua4qcTHxIxR5l918y1H17JA2HHrnKLVY%2BA%3D%3D"
        county <- read.csv(url3)

        countyPolygonMap <- plot_ly() %>% add_trace(
            type="choroplethmapbox",
            geojson=countyGeo,
            locations=county$fips,
            z=~county$population,
            colorscale="Viridis",
            zmin=0,
            zmax=12,
            marker=list(line=list(width=0),opacity=0.5)
        ) %>% layout(
            mapbox=list(
                style="carto-positron",
                zoom =2,
                center=list(lon= -95.71, lat=37.09))
        );
        countyPolygonMap;
        ## generating the interactive plotly map
    })

我仔细检查了源数据,没有问题或不适用.R 也没有收到任何警告或错误.你能帮我吗?

I double-checked with the source data and there is no problem or NA. R is not getting any warning or error either. Can you help me with it?

推荐答案

fips 需要 5 位数字.您可以将其转换为 county$nfips <- sprintf("%05d",county$fips).然后在你的情节中使用 nfips ,你会得到以下输出:

fips needs to be 5 digits. You can convert it as county$nfips <- sprintf("%05d",county$fips). Then use nfips in your plotly and you get the following output:

请注意,您也错过了阿拉斯加和康涅狄格州 - 都带有 4 位 fips.

Please note that you were missing Alaska and Connecticut also - all with 4-digit fips.

这篇关于在 Shinny Web 应用程序中绘制缺失状态的绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆