r为plotbarButtons设置全局选项

时间:2017-01-31 10:01:58

标签: r ggplot2 shiny plotly

创建ggplot对象的绘图对象。是否可以全局设置模式栏选项,以便每个绘图图仅显示myPlot_c中的以下modebarButtons。

library(ggplot2)
library(plotly)
library(dplyr)


myPlot_a <- ggplot(mtcars, aes(x=disp,y=mpg)) + geom_point()
myPlot_b <- ggplotly(myPlot_a)

#Is it possible to set this global, so don't have to repeat for each plot
myPlot_c <- myPlot_b %>% 
  config(displaylogo=F,
         modeBarButtonsToRemove = 
           list("sendDataToCloud","zoom2d","pan2d","select2d","lasso2d",
                "zoomIn2d","zoomOut2d","autoScale2d","hoverClosestCartesian",
                "hoverCompareCartesian"))

我可以包装一个函数,但是发现如果每次都设置了modebarButtons,那么与一个闪亮的应用程序一起使用renderPlotly()会遇到很多延迟。

2 个答案:

答案 0 :(得分:0)

我的解决方案是定义一个自定义包装器

library(ggplot2)

clean_ggplotly <- function(
  p = ggplot2::last_plot(),
  width = NULL,
  height = NULL,
  tooltip = "all",
  dynamicTicks = FALSE,
  layerData = 1,
  originalData = TRUE,
  source = "A",
  ...
) {

  stopifnot(requireNamespace("plotly"))

  plotly::config(
    plotly::ggplotly(
      p = p,
      width = width,
      height = height,
      tooltip = tooltip,
      dynamicTicks = dynamicTicks,
      layerData = layerData,
      originalData = originalData,
      source = source,
      ... = ...
    ),
    displaylogo = FALSE,
    modeBarButtonsToRemove = c(
      "sendDataToCloud",
      "zoom2d", "zoomIn2d", "zoomOut2d",
      "pan2d", "select2d", "lasso2d", "autoScale2d",
      "hoverClosestCartesian", "hoverCompareCartesian"
    )
  )
}


gg <- ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point()

clean_ggplotly(gg)

答案 1 :(得分:-1)

尝试指定配置来自的包:

library(ggplot2)
library(plotly)
library(dplyr)


myPlot_a <- ggplot(mtcars, aes(x=disp,y=mpg)) + geom_point()
myPlot_b <- ggplotly(myPlot_a)

#Is it possible to set this global, so don't have to repeat for each plot
myPlot_c <- myPlot_b %>% 
  plotly::config(displaylogo=F,
         modeBarButtonsToRemove = 
           list("sendDataToCloud","zoom2d","pan2d","select2d","lasso2d",
                "zoomIn2d","zoomOut2d","autoScale2d","hoverClosestCartesian",
                "hoverCompareCartesian"))