gpt4 book ai didi

r 为 modebarButtons 设置全局选项

转载 作者:行者123 更新时间:2023-12-03 22:33:06 28 4
gpt4 key购买 nike

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

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,与 Shiny 的应用程序一起使用 renderPlotly() 会遭受很多延迟。

最佳答案

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

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)

关于r 为 modebarButtons 设置全局选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41954595/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com