Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly Maps Not Rendering in R

Tags:

r

plotly

I have successfully installed the plotly library for R.

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.

Links to the code are here:

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

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

Can anyone help?

Thanks in advance.

EDIT:

Tools:

Plotly Library 2.016 for R

R Studio Version 0.99.489

R Version 3.2.2

Running on Windows 7 Home 64

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)

EDIT #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.

like image 503
Windstorm1981 Avatar asked Dec 25 '15 19:12

Windstorm1981


1 Answers

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.

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

This leads me to believe that the issue is either:

  1. Something to do with the plotly package itself not correctly working within RStudio's environment (doubt it's this) or,
  2. It's just one of those instances where the plot doesn't render because the size of the Viewer window is too small. I have that happen if I run something like "pairs" on a large data frame with a lot of correlations. It doesn't show in the viewer unless I expand it to a larger size or render it in a browser window.

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

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.

like image 140
Jsukup Avatar answered Oct 24 '22 17:10

Jsukup