通过在R中使用plotly包来复制Choropleth映射 [英] Reproducing a Choropleth Map by using plotly package in R

查看:132
本文介绍了通过在R中使用plotly包来复制Choropleth映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

library(plotly)
df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv")
df$hover <- with(df, paste(state, '<br>', "Beef", beef, "Dairy", dairy, "<br>",
                           "Fruits", total.fruits, "Veggies", total.veggies,
                           "<br>", "Wheat", wheat, "Corn", corn))
# give state boundaries a white border
l <- list(color = toRGB("white"), width = 2)
# specify some map projection/options
g <- list(
  scope = 'usa',
  projection = list(type = 'albers usa'),
  showlakes = TRUE,
  lakecolor = toRGB('white')
)

p <- plot_geo(df, locationmode = 'USA-states') %>%
  add_trace(
    z = ~total.exports, text = ~hover, locations = ~code,
    color = ~total.exports, colors = 'Purples'
  ) %>%
  colorbar(title = "Millions USD") %>%
  layout(
    title = '2011 US Agriculture Exports by State<br>(Hover for breakdown)',
    geo = g
  )

p 


上面的代码来自 plotly网站,生成的图应如下:


The above code is from plotly website and the plot produced should be as follows:

但是,我通过使用代码生成的图如下:

However, the plot I generated by using the code is as follows:

会发生什么?我是否需要安装一些其他软件包才能重现正确的图?

What happens? Do I need to install some other packages to reproduce the correct plot?


推荐答案

我也知道了.如果您打开JavaScript控制台,则会看到错误:

I got this too. If you open the javascript console you can see an error:

   Failed to load resource: Unable to init SSL Context: 

在尝试打开此文件时:

 "https://cdn.plot.ly/world_110m.json"

以下是屏幕截图:

我相信这是因为非专业版R-Studio在设计上不支持 https ,因此除了将其包装为markdown并在浏览器中将其包装为我在下面描述.

I believe this is because the non-professional Version of R-Studio does not support https by design, so there is probably no work around except wrapping it as markdown and viewing it in a browser as I describe below.

如果将其打包在R-markdown中(将代码放在以下几行之间):

If you package it in R-markdown (put your code between the following lines):

```{r} 

 (your code here)

```

,然后将其另存为.Rmd文件)并进行编织.然后,它仍然不能在R-Studio预览浏览器中使用,但是如果您使用在浏览器中打开"功能并在Chrome中打开它(例如),它将起作用.

and then save it as an .Rmd file) and knit it. Then it will still not work in the R-Studio preview-browser, and but it does if you use the "Open in Browser" function and open it in Chrome (for example).

或购买专业版:).

这篇关于通过在R中使用plotly包来复制Choropleth映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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