使用ggplotly时定义滑块的平移

问题描述

我想更改滑块外观的平移。 我的x轴名称很长,因此类似于下面的示例,它们倾斜了90度。

data(gapminder,package = "gapminder")
gg <- ggplot(gapminder,aes(gdpPercap,lifeExp,color = continent)) +
  geom_point(aes(size = pop,frame = year,ids = country)) +
  scale_x_log10() + 
  theme(axis.text.x = element_text(angle = 90,vjust = 0.5,hjust=1))
gg <- ggplotly(gg)

image of the code example

我的问题是x轴名称和x轴标题干扰了plotly滑块。因此,我希望使用类似以下内容的滑块外观:

gg <- gg %>% layout(title = "Basic Slider",sliders = list( 
               pad = list(l = 60,b=100))))

但是我收到错误消息:

Error in p$x$layout$sliders[[vapply(p$x$layout$sliders,is_ani_slider,: 
  attempt to select less than one element in integerOneIndex

解决方法

您可以通过功能animation_slider为动画滑块设置样式,并通过animation_button为按钮设置样式。试试这个:

library(plotly)
data(gapminder,package = "gapminder")
gg <- ggplot(gapminder,aes(gdpPercap,lifeExp,color = continent)) +
  geom_point(aes(size = pop,frame = year,ids = country)) +
  scale_x_log10() + 
  theme(axis.text.x = element_text(angle = 90,vjust = 0.5,hjust=1))
gg <- ggplotly(gg)
gg

gg %>% 
  layout(title = "Basic Slider") %>% 
  animation_slider(
    pad = list(l = 60,b = 100)
  ) %>% 
  animation_button(label = "Run")

enter image description here