Plotly 地图不在 R 中渲染地图、Plotly

2023-09-06 14:29:13 作者:然而

我已经成功安装了 R 的 plotly 库.

I have successfully installed the plotly library for R.

首先,我按照他们的 R 入门指南,直接从我的 RStudio 版本 0.99.489 的几个图中复制了代码.

To get started, I followed their Getting Started for R guide and directly copied the code from several plots in my RStudio Version 0.99.489.

散点图、箱线图等的例子效果很好.

The examples for scatterplots, box plots, etc. work well.

气泡图和等值线图的示例未正确绘制.地图根本不渲染.运行代码后只显示标题和图例.

The examples for bubble and choropleth maps do not plot properly. The maps do not render at all. Only title and legend show after running the code.

代码链接在这里:

https://plot.ly/r/bubble-maps/

https://plot.ly/r/choropleth-maps/

谁能帮忙?

提前致谢.

工具:

R 的 Plotly 库 2.016

Plotly Library 2.016 for R

R Studio 版本 0.99.489

R Studio Version 0.99.489

R 版本 3.2.2

在 Windows 7 Home 64 上运行

Running on Windows 7 Home 64

为 Plotly Bubble Map 尝试的一组代码(直接从他们的用户指南/代码示例中复制:

One set of code tried for a Plotly Bubble Map (copied straight from their user guide/code examples:

library(plotly)
df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_us_cities.csv')
df$hover <- paste(df$name, "Population", df$pop/1e6, " million")

df$q <- with(df, cut(pop, quantile(pop)))
levels(df$q) <- paste(c("1st", "2nd", "3rd", "4th", "5th"), "Quantile")
df$q <- as.ordered(df$q)

g <- list(
  scope = 'usa',
  projection = list(type = 'albers usa'),
  showland = TRUE,
  landcolor = toRGB("gray85"),
  subunitwidth = 1,
  countrywidth = 1,
  subunitcolor = toRGB("white"),
  countrycolor = toRGB("white")
)

plot_ly(df, lon = lon, lat = lat, text = hover,
        marker = list(size = sqrt(pop/10000) + 1),
        color = q, type = 'scattergeo', locationmode = 'USA-states') %>%
  layout(title = '2014 US city populations<br>(Click legend to toggle)', geo = g)

编辑#2

我已将问题隔离到这里:

I've isolate the issue to here:

plot_ly(df, lon = lon, lat = lat, text = hover,
        marker = list(size = sqrt(pop/10000) + 1),
        color = q, type = 'scattergeo', locationmode = 'USA-states')

我不确定如何从这里进行故障排除或简化语法以查看可能发生的情况.

I'm not sure how to troubleshoot from here or simplify the syntax to see what might be happening.

推荐答案

我在 RStudio Viewer 中绘制 choropleth 地图时遇到了类似的问题.即使完全按照您在 ployly 网站的链接中提供的代码复制代码,我也只给了我一个标题和图例,但没有地图.

I have had similar troubles with plotly rendering choropleth maps in the RStudio Viewer. Even copying the code exactly as provided in your links to the ployly website gave me just a title and legend, but no map.

但是,当我使用在新窗口中显示"按钮展开查看器时,在完全呈现的网络浏览器中打开了 choropleth.

However, when I expanded the Viewer using the "Show in new window" button, the choropleth opens up in my web brower fully rendered.

这让我相信问题是:

与 plotly 包本身在 RStudio 的环境中无法正常工作有关(怀疑是这个),或者,这只是由于查看器窗口的大小太小而导致绘图无法呈现的情况之一.如果我在具有很多相关性的大型数据帧上运行类似对"的东西,我就会发生这种情况.它不会显示在查看器中,除非我将其扩大到更大的尺寸或在浏览器窗口中呈现.

我想知道 RStudio 或软件包本身中是否有任何设置可以解决此问题.

I would like to know if there are any settings either in RStudio or in the packages themselves that may fix this.

编辑:在我的笔记本电脑上渲染绘图时我没有遇到这个问题 - 只是我的桌面.我相信两者都在运行最新的 R/RStudio 版本,所以我不确定为什么一个渲染而一个不渲染.

EDIT: I do not encounter this issue when rendering the plot on my laptop -- just my desktop. I believe both are running the latest R/RStudio versions so I'm not sure why one renders and one does not.