【问题标题】:plotly r choropleth with subplot not working as desired带有子图的 plotly r choropleth 无法按预期工作
【发布时间】:2016-03-29 12:29:04
【问题描述】:

我正在使用提供的示例并将其扩展以生成两个等值线的子图,如下所示。我只得到一个情节并且比例更高:

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)

我误解或遗漏了什么?

【问题讨论】:

    标签: r maps plotly choropleth


    【解决方案1】:

    我认为这是你需要的:

    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)
    

    【讨论】:

    • 谢谢。可以有不同的尺度吗?我有这样的数据。
    • 您应该已将此信息添加到您的原始帖子中。我已经看过,似乎有可能为其他类型的图提供多个图例(通过 legendgroup),但我不确定等值线图是否可能。我尝试了一些方法,但没有成功 :-) 很清楚你想要最终输出什么,但你可以使用 Rmarkdown (html doc) 并有 2 个地图,每个地图都有自己的图例
    • 对不起...另外,它不会在我的计算机上产生所需的结果。我正在使用packageVersion('plotly') [1] ‘3.4.1’
    • 我正在使用plotly_3.4.3(开发版)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 2012-08-30
    • 2012-08-12
    • 2013-12-08
    相关资源
    最近更新 更多