Issues with legend position, in particular since update 4.10
Nobody has claimed this yet.
- Dominant language
- R
- Stars
- 2.7k
- Forks
- 641
- PR merge metrics
- No merged PRs in 30d
Description
Hey,
it's been a while I am struggling with the position of the legend in plotly. First, let me report a small bug, then I'll ask another question.
Reprex: execute the following code with plotly 4.9.4.1 and 4.10, respectively. You'll see that the legend holds on one line in 4.9.4.1 vs. two lines in 4.10 (cf. figures below).
bars <- plot_ly(x = .5, y = "foo", type = 'bar', orientation = 'h', name="First color of legend") %>% # , marker = list(color = 'red', line = list(color = 'white')
layout(barmode = 'stack', margin = list(t = 50), showlegend = T,
legend = list(orientation='h', y=1.2, x=0.2, traceorder='normal', font=list(size=12, color='black'))) %>%
add_trace(x = .5, name="Second color of legend")
bars
orca(bars, width = 415, height = 150, file = "temp.png")
4.9.4.1

4.10

I had optimized hundreds of images with the old version, so I'll just keep the old version running, to avoid having to manually adjust hundreds of width parameters. It may seem an innocuous change for people who start creating their figures with the last version (4.10). But actually it forces to create wider figures if one wants to keep the legend in one line. This in turn widens the relative size of the bar to the detriment of the label (foo), thus rendering the label text smaller (and less readable) when one embeds this figure in a document (with width constrained to text width). Also, notice a related bug: for some widths values close to the switch between one line and two lines legend, the size of the bar reduces for no reason. This offsets the legend from the bar and ends up in an ugly plot.
I could solve my bug by using the old package, so I don't really mind. But as I am here, I'd like to know if there is some way to automatize the positioning of the legend and the width of the figures.
My issue is that I am creating hundreds of figures to plot survey results, variable by variable (see e.g. here). I have created a semi-automatized function that takes to plot a variable in a bar chart like the one above, but I still need to find manually the minimum width such that the legend holds in one line (and doesn't mask the bar), which is quite cumbersome. Another thing that I'd like to automatize is the placement of the legend: I'd like it to be the furthest possible to the left (again, to minimize width), and this depends on the size of the labels.
Are there ways to automatize the placement of the legend's "x" as well as the width with which to save the graph? Or more generally, is there a function to nicely plot a factor variable in a bar chart, by just giving the variable and the function automatically adjusts the parameters to make a nice plot?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the supplied R reproducer with plotly 4.9.4.1 and 4.10 and compare the legend wrapping, bar size, and placement at nearby widths. Done means the regression is explained or corrected, with legend positioning and width behavior verified against the reported examples.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- r
- Domain
- data-visualization
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100