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
Legend position not preserved when converting ggplot2 to plotly #2407
When converting a ggplot2 plot to plotly using ggplotly(), the legend position specified in theme(legend.position=...) is not preserved in the resulting plotly graph. This requires additional layout configuration after conversion.
Reproducible Example
library(ggplot2)
library(plotly)
# Sample data
set.seed(123)
data<-data.frame(
category=LETTERS[1:10],
value= runif(10),
group=factor(rep(1:2, 5))
)
# Create ggplot with specified legend positionp<- ggplot(data) +
aes(x=category, y=value, fill=group) +
geom_col() +
theme(legend.position="bottom") # This position is not preserved# Convert to plotlyp_interactive<- ggplotly(p)
# Current workaround neededp_interactive<- ggplotly(p) %>%
layout(
legend=list(
orientation="h",
x=0.5,
y=-0.2,
xanchor="center",
yanchor="top"
)
)
Expected Behavior
The legend position specified in the ggplot2 theme should be automatically translated to the equivalent plotly layout configuration.
Actual Behavior
The legend position from ggplot2 is ignored, and the legend defaults to plotly's default position (right side). Manual configuration using layout() is required to achieve the desired legend position.
Environment Information
R version: 4.4.2
The plot is rendered inside a Shiny App
Additional Context
This affects the workflow when converting static ggplot2 visualizations to interactive plotly graphs, requiring additional code to maintain consistent legend positioning. It would be helpful if the conversion process could automatically map ggplot2's legend positions to plotly's layout configurations.
Activity
Fix #2407, #2187: legend.position theme element now translated to plo…
Fix #2407, #2187: legend.position theme element now translated
Fix #2407, #2187: legend.position theme element now translated
Fix multiple ggplotly() regressions (#2469)