You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ggplotly() does not reflect axis/facet position changes in ggplot. #946
Would like to note a similar issue, where I included facet_wrap(~name, ncol=1, strip.position = "left") in a ggplot and the resulting strips were at the top. (ggplotly, without the plotly wrapping, rendered this properly.)
I recently answered an SO question that asked about the ignored strip position. I came up with a workaround after generating the plot with ggplotly. The questioner wanted the strips at the bottom. When I looked at the plotly object, I found that a lot of the settings were preset to place the strips at the bottom. I needed to make one change to annotations (where the text in the strips is coming from) and one change to shapes (where the background for the strips comes from). After that I adjust the ranges on the y-axis, to account for the strips, and the margin, to get rid of the empty white space on the top (where the strips were originally).
Obviously, these changes will have to be modified depending on what you have in your plot and where you want to place the strips.
library(tidyverse)
library(plotly)
# plot from Stack Overflow Question
p = iris %>%
ggplot(aes(x = Species , y = Sepal.Width)) +
geom_boxplot() +
facet_wrap(~ Species , strip.position = "bottom")
p
pp <- ggplotly(p) # lacking the strip position set in ggplot
# adjust the strip positions
invisible(
lapply(1:length(pp$x$layout$shapes),
function(i) { # if a strip shape (using the strip color)
if(isTRUE(pp$x$layout$shapes[[i]]$fillcolor == "rgba(217,217,217,1)")) {
pp$x$layout$shapes[[i]]$yanchor <<- 0 # anchor at the bottom
} # if there are at least i annotations (there are less annotations than shapes)
if(i <= length(pp$x$layout$annotations)) { # if the text is a strip label
if(any(pp$x$layout$annotations[[i]]$text %in% unique(iris$Species))) {
pp$x$layout$annotations[[i]]$y <<- 0.01 # move text to the bottom
}
}
})
)
pp %>% layout(yaxis = list(range = c(1.5, 4.5)), # add space for strip position
yaxis2 = list(range = c(1.5, 4.5)),
yaxis3 = list(range = c(1.5, 4.5)),
margin = list(t = 10, r = 10, b = 43, l = 43)) # remove excess on top
Activity
kojisposts commentedon Aug 8, 2019
Would like to note a similar issue, where I included
facet_wrap(~name, ncol=1, strip.position = "left")in a ggplot and the resulting strips were at the top. (ggplotly, without the plotly wrapping, rendered this properly.)bvaldebenitom commentedon Feb 22, 2020
Just stumbled upon this same problem. Is there any part of the plotly code we can take a look at?
mattjmeier commentedon Nov 3, 2021
This is still an issue as far as I can see. Did anyone come up with a workaround?
bvaldebenitom commentedon Nov 5, 2021
Hi @mattjmeier,
Unfortunately I haven't. Best I could was to switch some things in the original ggplot in order to "skip" the issue entirely.
fraupflaume commentedon Sep 7, 2022
I recently answered an SO question that asked about the ignored strip position. I came up with a workaround after generating the plot with
ggplotly. The questioner wanted the strips at the bottom. When I looked at theplotlyobject, I found that a lot of the settings were preset to place the strips at the bottom. I needed to make one change toannotations(where the text in the strips is coming from) and one change toshapes(where the background for the strips comes from). After that I adjust the ranges on the y-axis, to account for the strips, and the margin, to get rid of the empty white space on the top (where the strips were originally).You can see the Q&A here.
Obviously, these changes will have to be modified depending on what you have in your plot and where you want to place the strips.