Ggplotly 破坏 facet_wrap() 固定尺度

Ggplotly corrupts facet_wrap() fixed scales

当我仅使用 ggplot2 以固定比例绘制此图时,多面图没有问题

  ggplot(aes(x = fecha, y = prom_imagen_pos)) +
  geom_line(size = 1.5) +
   annotate(geom='line', x=oposicion$fecha,y=oposicion$imagen , size = 1.5, color = "darkgoldenrod") +
  facet_wrap(dirigente~.,scales = "fixed",) +
  scale_y_continuous(labels = function(x) paste0(x, "%"),breaks = c(20,30,40,50,60)) +
  labs(x = "",
       y = "") +
  theme_light()

注意 y 尺度是固定的

当我向该图添加 ggplotly() 函数时,多面 y 刻度被破坏(顶部的有一个,底部的有另一个)。有办法解决这个问题,并像第一个情节一样有固定比例吗?

代码:

plot <-   ggplot(aes(x = fecha, y = prom_imagen_pos)) +
  geom_line(size = 1.5) +
   annotate(geom='line', x=oposicion$fecha,y=oposicion$imagen , size = 1.5, color = "darkgoldenrod") +
  facet_wrap(dirigente~.,scales = "fixed",) +
  scale_y_continuous(labels = function(x) paste0(x, "%"),breaks = c(20,30,40,50,60)) +
  labs(x = "",
       y = "") +
  theme_light()

ggplotly(plot, dynamicTicks = TRUE) 

您需要在 ggplotly(...) 中设置 dynamicTicks = FALSE。根据文档:

Dynamic ticks are useful for updating ticks in response to zoom/pan interactions; however, they can not always reproduce labels as they would appear in the static ggplot2 image

我使用 ggplot2 附带的 diamonds 数据集快速测试了这一点。

library(ggplot2)
library(plotly)
my.plt <- ggplot(diamonds, aes(x = price, y = carat)) + 
   geom_point() + 
   facet_wrap(~clarity, nrow = 2) # scales = "fixed" is used by default


ggplotly(my.plt, dynamicTicks = FALSE)

设置 dynamicTicks = TRUE 覆盖 facets 中固定的 scales/labels。

ggplotly(my.plt, dynamicTicks = TRUE)