情节r等值,子情节不能按预期工作

时间:2016-03-29 12:29:05

标签: r maps 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')
)

p1 <- plot_ly(df, z = total.exports, text = hover, locations = code, type = 'choropleth', locationmode = 'USA-states', color = total.exports, colors = 'Purples',
marker = list(line = l), colorbar = list(title = "Millions USD"))

p2 <- plot_ly(df, z = total.exports, text = hover, locations = code, type = 'choropleth', locationmode = 'USA-states', color = total.exports, colors = 'Purples',
marker = list(line = l), colorbar = list(title = "Millions USD"))

plotly::subplot(p1, p2, nrows = 2) %>% layout(geo = g)

我误解或遗失了什么?

1 个答案:

答案 0 :(得分:0)

我认为这就是你所需要的:

p1 <- plot_ly(df, z = total.exports, text = hover, locations = code, type = 'choropleth', locationmode = 'USA-states', color = total.exports, colors = 'Purples',
marker = list(line = l), colorbar = list(title = "Millions USD"), geo = 'geo1')

p2 <- plot_ly(df, z = total.exports, text = hover, locations = code, type = 'choropleth', locationmode = 'USA-states', color = total.exports, colors = 'Purples',
marker = list(line = l), colorbar = list(title = "Millions USD"), geo = 'geo2')

plotly::subplot(p1, p2, nrows = 2) %>% layout(geo = g, geo2 = g)

enter image description here