Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ggplotly - axis titles still overlapping with labels when converting faceted plot #1224

Closed
hms1 opened this issue Mar 29, 2018 · 11 comments
Closed
Labels

Comments

@hms1
Copy link

hms1 commented Mar 29, 2018

The issue with axis titles overlapping with axis labels, fixed in the latest version of plotly, appears to still be present when using ggplotly to convert a faceted ggplot.

When converting an unfaceted ggplot, the new automargin=TRUE attribute works perfectly:

mpg_plot <- mpg %>%
  ggplot(aes(x = cyl, fill = trans)) +
  geom_bar() 

ggplotly(mpg_plot) %>% 
  layout( 
  xaxis = list(automargin=TRUE), 
  yaxis = list(automargin=TRUE)
  )

image
However, when adding a facet, the old issue of overlap between axis titles and labels persists:

mpg_plot_faceted <- mpg_plot + facet_wrap(. ~ fl)

ggplotly(mpg_plot_faceted) %>% 
 layout( 
 xaxis = list(automargin=TRUE), 
 yaxis = list(automargin=TRUE)
 )

image

Thanks,

Henry

For reference, the original plotly issue and related fix:

plotly/plotly.js#1504
plotly/plotly.js#2243

@cpsievert
Copy link
Collaborator

I believe automargins only account for axis titles/labels, not annotations, which is currently the only way to implement axis titles for facetted plots

@Nova-Scotia
Copy link

+1. I tried to fix this issue based on this post but wasn't successful.

@MeganBeckett
Copy link

I'm also having issues with faceted plots when converting from ggplot to ggplotly. Does anyone have any solutions?

@Nova-Scotia
Copy link

Correction: that post https://stackoverflow.com/questions/42763280/r-ggplot-and-plotly-axis-margin-wont-change I mentioned does provide a solution, you may just have to play around a bit with the numbers, and determine which label you want to move.

@cpsievert
Copy link
Collaborator

Note that plotly_json() can be helpful for identifying which annotation you want to modify

@jfernandez903
Copy link

jfernandez903 commented May 2, 2019

Based on the post that @Nova-Scotia mentioned, I created a simple function that can make it less of a hassle if you have multiple plots that you are creating:

layout_ggplotly <- function(gg, x = -0.02, y = -0.08){
  # The 1 and 2 goes into the list that contains the options for the x and y axis labels respectively
  gg[['x']][['layout']][['annotations']][[1]][['y']] <- x
  gg[['x']][['layout']][['annotations']][[2]][['x']] <- y
  gg
}

you can pipe your ggplotly output into this function so you don't have to name each plot you make in order to adjust the axis labels

@kojisposts
Copy link

still relatively new to coding so I might be completely misunderstanding something, but it seems to me that ggplots are now constructed differently and that the gg[['x']][['layout']][['annotations']][[1]][['y']] hack no longer works...

Seems to me that p[["theme"]][["text"]] controls the x-axis text, but none of them adjust the y=positioning.

スクリーンショット 2019-08-02 午後0 40 06
スクリーンショット 2019-08-02 午後0 38 33

@ghost
Copy link

ghost commented Aug 23, 2019

The layout_ggplotly annotations method doesn't really work for all screen sizes, which is what I'd like to do so that we can integrate ggplotly with my shiny apps. It'd be amazing if this was able to be fixed

@DmitriiIshutin
Copy link

Based on the post that @Nova-Scotia mentioned, I created a simple function that can make it less of a hassle if you have multiple plots that you are creating:

layout_ggplotly <- function(gg, x = -0.02, y = -0.08){
  # The 1 and 2 goes into the list that contains the options for the x and y axis labels respectively
  gg[['x']][['layout']][['annotations']][[1]][['y']] <- x
  gg[['x']][['layout']][['annotations']][[2]][['x']] <- y
  gg
}

you can pipe your ggplotly output into this function so you don't have to name each plot you make in order to adjust the axis labels

I noticed this function works correctly only when x and y axis titles exist. If either of those is missing -- for example, as a result of theme(axis.title.x = element_blank()) -- then positional reference using ...[[1]]... and ...[[2]]... refers to wrong titles / annotations.

@pat-alt
Copy link

pat-alt commented Nov 14, 2020

Props to @cpsievert @Nova-Scotia @jfernandez903 - all very helpful! When using facet_grid the legend overlaps with the plot. If you run into this issue, you can adapt @jfernandez903`s function as follows:

layout_ggplotly <- function(gg, x = -0.1, y = -0.05, x_legend=1.05, y_legend=0.95, mar=list(l=50, r=150)){
  # The 1 and 2 goes into the list that contains the options for the x and y axis labels respectively
  gg[['x']][['layout']][['annotations']][[1]][['y']] <- x
  gg[['x']][['layout']][['annotations']][[2]][['x']] <- y
  gg[['x']][['layout']][['annotations']][[11]][['x']] <- x_legend # the legend title was the 11-th list element in my case!
  gg[['x']][['layout']][['legend']][['y']] <- y_legend
  gg[['x']][['layout']][['legend']][['x']] <- x_legend
  gg %>% layout(margin = mar)
}

Still a bit fiddly and you'll likely have to play with the coordinate values. More generally plotly_json() really helps identifying what's wrong with your layout as @cpsievert suggested.

@cpsievert
Copy link
Collaborator

cpsievert commented Nov 2, 2021

It appears #1975 fixes this (please open a new issue if you're still struggling with it)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants