Skip to content

Line breaks in y-tick labels on horizontal bar chart #1079

@e-mitchell

Description

@e-mitchell

On a horizontal barplot, trying to shorten y-tick labels using <br> line breaks leaves too much room for the label.

Here's an example of a long label (no line breaks):

library(plotly)

long_label = "Dinner (or, super long label to check out some line breaks in plotly)"

dat1 <- data.frame(
  sex = factor(c("Female","Female","Male","Male")),
  time = factor(c("Lunch",long_label,"Lunch",long_label), levels=c("Lunch",long_label)),
  total_bill = c(13.53, 16.81, 16.24, 17.42)
)

p <- ggplot(data=dat1, aes(x=time, y=total_bill, fill=sex)) +
  geom_bar(stat="identity", position=position_dodge()) + 
  coord_flip() + theme_bw()

p <- ggplotly(p)
ggplotly(p)

image

After adding line breaks in the labels, the plot stays squished to the right:

library(plotly)

long_label = "Dinner (or, super long label<br>to check out some<br>line breaks in plotly)"

dat1 <- data.frame(
  sex = factor(c("Female","Female","Male","Male")),
  time = factor(c("Lunch",long_label,"Lunch",long_label), levels=c("Lunch",long_label)),
  total_bill = c(13.53, 16.81, 16.24, 17.42)
)

p <- ggplot(data=dat1, aes(x=time, y=total_bill, fill=sex)) +
  geom_bar(stat="identity", position=position_dodge()) + 
  coord_flip() + theme_bw()

p <- ggplotly(p)
ggplotly(p)

image

When it should look more like this (but with the rest of the label):

image

I think the issue might have something to do with the way that 'nchar' in the ggplotly function is counting characters of each tick label, without splitting by <br> first.

Thanks for looking into this!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions