问题
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
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?
回答1:
I got this too. If you open the javascript console you can see an error:
Failed to load resource: Unable to init SSL Context:
while it is trying to open this file:
"https://cdn.plot.ly/world_110m.json"
Here is a screen shot:
Cause:
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.
Workaround:
If you package it in R-markdown (put your code between the following lines):
```{r}
(your code here)
```
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).
Or buy the professional version :).
来源:https://stackoverflow.com/questions/43458527/reproducing-a-choropleth-map-by-using-plotly-package-in-r