在 R 中使用 plotly 包再现等值线图

Reproducing a Choropleth Map by using plotly package in R

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 website,制作出来的图应该是这样的:

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

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


我也收到了。如果您打开 javascript 控制台,您会看到一个错误:

   Failed to load resource: Unable to init SSL Context: 

正在尝试打开此文件时:

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

这是一个屏幕截图:

原因:

我认为这是因为 R-Studio 的非专业版在设计上不支持 https,因此除了将其包装为 markdown 并查看外,可能没有其他解决方法它在浏览器中,如下所述。

解决方法:

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

```{r} 

 (your code here)

```

然后另存为.Rmd文件)编织。然后它仍然无法在 R-Studio 预览浏览器中工作,但如果您使用 "Open in Browser" 函数并在 Chrome 中打开它(例如),它就会工作。

或者购买专业版:).