R - 图例部分超过情节图(小平面环绕)

问题描述 投票:0回答:1

我已经搜索过但没有找到任何人解决这个问题,因此没有解决方案。 我已经了解到您使用

legend
属性移动图例:

  layout(title = list(text = paste0('Arbetskraftsdeltagande och sysselsättningsgrad',
                                    '<br>',
                                    '<sup>',
                                    '20-64 år','</sup>')), 
         margin = list(l = 50, r = 130, b = 50, t = 90),
         #   annotations = list(text = "Källa: Konjunkturinstitutet", xref='paper', yref='paper', 
         #                     x=1, y=-0.2, showarrow = F,
         #                    font = list(size = 12)),
         legend = list(x = 100, y = 0.5))

df_plotly 

这在其他情节中通常有效,我可以将图例一直移动到情节之外,但是对于这个 facet_wrap 情节,它不会一直向右移动,如下所示:

如果我将 x = 300 或增加 r 的边距空间都没有关系 - 没有结果。 我想这是因为传说已经是 x 到右边的 100% - 但显然不是? 改变 y - 正在移动图例,所以这个论点是这样的。

->有什么想法吗?

如果您需要查看更多代码,请告诉我。

提前致谢!

r ggplot2 plotly facet-wrap legend-properties
1个回答
0
投票

您可以添加空间就好像有第三个情节并给非情节非常小的空间。

首先,我将用一排图进行演示;然后我将演示如何处理多行。

一排子图

这里有一些通用的情节。

plt <- plot_ly(x = 1:3, y = 1:3, name = "A", type = "scatter", mode = "markers")

plt2 <- plot_ly(x = 1:3, y = 1:3, name = "B", type = "scatter", mode = "markers")

这是没有干预的子图。

subplot(plt, plt2) %>% 
  layout(legend = list(bgcolor = "lightgray"))

现在好像在那一行中有第三个情节,在情节和传说之间。我会分别给实际地块45%和45%的空间,剩下的空间给非地块

这需要你分配

widths
specs.rowspan

subplot(plt, plt2, widths = c(.45, .45, .1),  # assign three widths
        specs = list(rowspan = 3)) %>%        # tell plot there's 3
  layout(legend = list(bgcolor = "lightgray"))

不止一排子图

你没有让你的问题重现,但我可以看到你有两行。为此,您将要在

specs
中添加内容。

这是一个带有第二行的示例和没有干预的子图的代码。

plt <- plot_ly(x = 1:3, y = 1:3, name = "A", type = "scatter", mode = "markers")
plt2 <- plot_ly(x = 1:3, y = 1:3, name = "B", type = "scatter", mode = "markers")
plt3 <- plot_ly(x = 1:3, y = 1:3, name = "C", type = "scatter", mode = "markers")
plt4 <- plot_ly(x = 1:3, y = 1:3, name = "D", type = "scatter", mode = "markers")

# no intervention to move legend in this subplot
subplot(plt, plt2, plt3, plt4, nrows = 2, margin = .05) %>% 
  layout(legend = list(bgcolor = "lightgray", y = .5))

现在要使这些行每行跨越三行,您必须为每一行创建一个子图,然后将它们组合起来。

subplot(
  subplot(plt, plt2, widths = c(.45, .45, .1),
          specs = list(rowspan = 3)),
  subplot(plt3, plt4, widths = c(.45, .45, .1),
          specs = list(rowspan = 3)),
  nrows = 2, margin = .05
) %>% layout(legend = list(bgcolor = "lightgray", y = .5))

© www.soinside.com 2019 - 2024. All rights reserved.