2

I've searched but not find anyone w this issue and thus no solution. I've come to understand that you move the legend around with the legend attribute:

  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 

This works generally in other plots, I can move the legend all the way outside the plot, but with this facet_wrap plot it doesn't move all the way to the right as can be seen below:

enter image description here

It doesn't mather if I put x = 300, or increase margin space for r - no results. I guess it's beacuse the legend already is x to the right 100% - but apparently it's not? Changing y - is moving the legend, so the argument works as such.

-> Any ideas?

Let me know if you need to see more of the code.

Thanks in advance!

Christian
  • 117
  • 1
  • 3
  • 10

1 Answers1

1

You can add space as if there's a third plot and give that non-plot very little space.

First I'll demo with one row of plots; then I'll demo what to do with multiple rows.

One Row of Subplots

Here are some generic plots.

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")

Here's the subplot with no intervention.

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

enter image description here

Now as if there's a third plot in that row, between the plots and the legends. I'll give the actual plots 45% and 45% of the space respectively, and give the remaining space to the non-plot.

This requires you to assign widths and 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"))

enter image description here

More than One Row of Subplots

You didn't make your question reproducible, but I can see that you have two rows. You're going to want to add the content in specs for that.

Here's an example with a second row and code for a subplot with no interventions.

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))

Now to make these rows each span three, you have to create a subplot for each row, and then combine them.

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))

enter image description here

enter image description here

Kat
  • 15,669
  • 3
  • 18
  • 51
  • 1
    Thanks this seems to be what I'm looking for, I'll try it out asap and thereafter accept solution! :) – Christian Feb 23 '23 at 09:26