水平并排分区统计子图不起作用

Horizontal side by side choropleth subplots do not work

提问人:Luitpold Wienerle 提问时间:10/12/2023 最后编辑:Konrad RudolphLuitpold Wienerle 更新时间:10/19/2023 访问量:35

问:

我想在一个子图中水平(并排)有两个 r 绘图分区统计图,但 nrow=1 不起作用,而是它们垂直显示(彼此上方):

library(plotly)
df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv")

g <- list(
  scope = 'usa',
  projection = list(type = 'albers usa'),
  showlakes = TRUE,
  lakecolor = toRGB('white')
)

p1 <- plot_ly(z = df$beef, text = df$hover, locations = df$code, type = 'choropleth', locationmode = 'USA-states', color = df$beef, colors = 'Purples',
              colorbar = list(title = "Millions USD")) %>% layout(geo = g)

p2 <- plot_ly(z = df$pork, text = df$hover, locations = df$code, type = 'choropleth', locationmode = 'USA-states', color = df$pork, colors = 'Purples',
              colorbar = list(title = "Millions USD"), geo = 'geo2') %>% layout(geo2 = g)


plotly::subplot(p1, p2, nrows = 1)

enter image description here

我该如何修复(将它们放在彼此之上而不是并排)? 我怎样才能有一个共同的图例/颜色条?

r plotly 子绘图 等值统计

评论


答:

1赞 Luitpold Wienerle 10/19/2023 #1

只需使用“domain”即可让 nrows 工作。

g <- list(
  scope = 'usa',
  projection = list(type = 'albers usa'),
  showlakes = TRUE,
  lakecolor = toRGB('white'),
  domain = list(x = c(0, 1), y = c(0, 1))
)

p1 <- plot_ly(z = df$beef, locations = df$code, type = 'choropleth', locationmode = 'USA-states', 
              color = df$beef, colors = 'Purples',
              colorbar = list(title = "Millions USD"), showscale = F) %>% layout(geo = g)
p2 <- plot_ly(z = df$pork, locations = df$code, type = 'choropleth', locationmode = 'USA-states', 
              color = df$pork, colors = 'Greens',
              colorbar = list(title = "Millions USD"), showscale = F, geo = 'geo2') %>% layout(geo2 = g)
p3 <- plot_ly(z = df$corn, locations = df$code, type = 'choropleth', locationmode = 'USA-states', 
              color = df$corn, colors = 'Blues',
              colorbar = list(title = "Millions USD"), showscale = F, geo = 'geo3') %>% layout(geo3 = g)
p4 <- plot_ly(z = df$cotton, locations = df$code, type = 'choropleth', locationmode = 'USA-states', 
              color = df$cotton, colors = 'Oranges',
              colorbar = list(title = "Millions USD"), showscale = F, geo = 'geo4') %>% layout(geo4 = g)

subplot(p1, p2, p3, p4, nrows = 2)

这将呈现:enter image description here

仍然没有找到共享子图颜色条的方法,所以我通过“showscale = F”隐藏它们并使用不同的颜色来代替。