Description
Description
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 position
p <- ggplot(data) +
aes(x = category, y = value, fill = group) +
geom_col() +
theme(legend.position = "bottom") # This position is not preserved
# Convert to plotly
p_interactive <- ggplotly(p)
# Current workaround needed
p_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