diff --git a/r/2021-08-27-configuration-options.Rmd b/r/2021-08-27-configuration-options.Rmd index e4dc6c83..5808c5cd 100644 --- a/r/2021-08-27-configuration-options.Rmd +++ b/r/2021-08-27-configuration-options.Rmd @@ -30,20 +30,25 @@ For the complete list of configuration options and their defaults see: https://g This option allows users to zoom in and out of figures using the scroll wheel on their mouse and/or a two-finger scroll. ```{r} -library(plotly) - -fig <- plot_ly()%>% - add_trace(x = c(1, 2, 3), y = c(1, 3, 1), type = 'scatter', mode = 'lines+markers') -config(fig, scrollZoom = TRUE)%>%layout(plot_bgcolor='#e5ecf6', - xaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff'), - yaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff') - ) +library(plotly) + +fig <- plot_ly() %>% + add_trace(x = c(1, 2, 3), y = c(1, 3, 1), type = "scatter", mode = "lines+markers") + +config(fig, scrollZoom = TRUE) %>% + layout( + plot_bgcolor = "#e5ecf6", + xaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ), + yaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ) + ) ``` ### Turning Off Responsiveness @@ -55,39 +60,49 @@ Try resizing your browser window to see this behavior in effect on this page. If you would like to disable this default behavior and force your figures to always have the same height and width regardless of the window size, set the value of the `responsive` key to `FALSE` in your figure's configuration dictionary. ```{r} -library(plotly) - -fig <- plot_ly()%>% - add_trace(x = c(1, 2, 3), y = c(1, 3, 1), type = 'scatter', mode = 'lines+markers') -config(fig, responsive = FALSE)%>%layout(plot_bgcolor='#e5ecf6', - xaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff'), - yaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff') - ) +library(plotly) + +fig <- plot_ly() %>% + add_trace(x = c(1, 2, 3), y = c(1, 3, 1), type = "scatter", mode = "lines+markers") + +config(fig, responsive = FALSE) %>% + layout( + plot_bgcolor = "#e5ecf6", + xaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ), + yaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ) + ) ``` ### Making A Static Chart ```{r} -library(plotly) - -fig <- plot_ly()%>% - add_trace(x = c(1, 2, 3), y = c(1, 3, 1), type = 'scatter', mode = 'lines+markers') -config(fig, staticPlot = TRUE)%>%layout(plot_bgcolor='#e5ecf6', - xaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff'), - yaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff') - ) +library(plotly) + +fig <- plot_ly() %>% + add_trace(x = c(1, 2, 3), y = c(1, 3, 1), type = "scatter", mode = "lines+markers") + +config(fig, staticPlot = TRUE) %>% + layout( + plot_bgcolor = "#e5ecf6", + xaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ), + yaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ) + ) ``` ### Forcing The Modebar to Always Be Visible @@ -97,20 +112,25 @@ When users hover over a figure generated with `plotly`, a **modebar** appears in By default, the modebar is only visible while the user is hovering over the chart. If you would like the modebar to always be visible regardless of whether or not the user is currently hovering over the figure, set the displayModeBar attribute in the configuration of your figure to true. ```{r} -library(plotly) - -fig <- plot_ly()%>% - add_trace(x = c(1, 2, 3), y = c(1, 3, 1), type = 'scatter', mode = 'lines+markers') -config(fig, displayModeBar = TRUE)%>%layout(plot_bgcolor='#e5ecf6', - xaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff'), - yaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff') - ) +library(plotly) + +fig <- plot_ly() %>% + add_trace(x = c(1, 2, 3), y = c(1, 3, 1), type = "scatter", mode = "lines+markers") + +config(fig, displayModeBar = TRUE) %>% + layout( + plot_bgcolor = "#e5ecf6", + xaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ), + yaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ) + ) ``` ### Preventing the Modebar from Appearing @@ -120,40 +140,50 @@ When users hover over a figure generated with `plotly`, a modebar appears in the By default, the modebar is only visible while the user is hovering over the chart. If you would like the modebar to never be visible, then set the `displayModeBar` attribute in the config of your figure to FALSE. ```{r} -library(plotly) - -fig <- plot_ly()%>% - add_trace(x = c(1, 2, 3), y = c(1, 3, 1), type = 'scatter', mode = 'lines+markers') -config(fig, displayModeBar = FALSE)%>%layout(plot_bgcolor='#e5ecf6', - xaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff'), - yaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff') - ) +library(plotly) + +fig <- plot_ly() %>% + add_trace(x = c(1, 2, 3), y = c(1, 3, 1), type = "scatter", mode = "lines+markers") + +config(fig, displayModeBar = FALSE) %>% + layout( + plot_bgcolor = "#e5ecf6", + xaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ), + yaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ) + ) ``` ### Hiding the Plotly Logo on the Modebar ```{r} -library(plotly) - -fig <- plot_ly()%>% - add_trace(x = c(1, 2, 3), y = c(1, 3, 1), type = 'scatter', mode = 'lines+markers') -config(fig, displaylogo = FALSE)%>%layout(plot_bgcolor='#e5ecf6', - xaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff'), - yaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff') - ) +library(plotly) + +fig <- plot_ly() %>% + add_trace(x = c(1, 2, 3), y = c(1, 3, 1), type = "scatter", mode = "lines+markers") + +config(fig, displaylogo = FALSE) %>% + layout( + plot_bgcolor = "#e5ecf6", + xaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ), + yaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ) + ) ``` ### Customizing Modebar "Download Plot" Button @@ -163,45 +193,59 @@ The camera icon on the modebar causes a static version of the figure to be downl This behavior can be controlled via the `toImageButtonOptions` configuration key. ```{r} -library(plotly) - -fig <- plot_ly()%>% - add_trace(x = c(1, 2, 3), y = c(1, 3, 1), type = 'bar') -config(fig, toImageButtonOptions = list(format= 'svg', # one of png, svg, jpeg, webp - filename= 'custom_image', - height= 500, - width= 700, - scale= 1 ))%>%layout(plot_bgcolor='#e5ecf6', - xaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff'), - yaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff') - ) +library(plotly) + +fig <- plot_ly() %>% + add_trace(x = c(1, 2, 3), y = c(1, 3, 1), type = "bar") + +config(fig, toImageButtonOptions = list( + format = "svg", # one of png, svg, jpeg, webp + filename = "custom_image", + height = 500, + width = 700, + scale = 1 +)) %>% + layout( + plot_bgcolor = "#e5ecf6", + xaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ), + yaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ) + ) ``` Figures can be set to download at the currently-rendered size by setting `height` and `width` to `NULL`: ```{r} -library(plotly) - -fig <- plot_ly()%>% - add_trace(x = c(1, 2, 3), y = c(1, 3, 1), type = 'bar') -config(fig, toImageButtonOptions = list(height= NULL, - width= NULL))%>%layout(plot_bgcolor='#e5ecf6', - xaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff'), - yaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff') - ) +library(plotly) + +fig <- plot_ly() %>% + add_trace(x = c(1, 2, 3), y = c(1, 3, 1), type = "bar") + +config(fig, toImageButtonOptions = list( + height = NULL, + width = NULL +)) %>% + layout( + plot_bgcolor = "#e5ecf6", + xaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ), + yaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ) + ) ``` ### Removing Modebar Buttons @@ -217,39 +261,49 @@ To delete buttons from the modebar, pass an array of strings containing the name - **Other**: `hoverClosestGl2d`, `hoverClosestPie`, `toggleHover`, `resetViews`, `toImage`, `sendDataToCloud`, `toggleSpikelines`, `resetViewMapbox` ```{r} -library(plotly) - -fig <- plot_ly()%>% - add_trace(x = c(1, 2, 3), y = c(1, 3, 1), type = 'scatter', mode = 'lines+markers') -config(fig, modeBarButtonsToRemove = c('zoom2d','pan2d'))%>%layout(plot_bgcolor='#e5ecf6', - xaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff'), - yaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff') - ) +library(plotly) + +fig <- plot_ly() %>% + add_trace(x = c(1, 2, 3), y = c(1, 3, 1), type = "scatter", mode = "lines+markers") + +config(fig, modeBarButtonsToRemove = c("zoom2d", "pan2d")) %>% + layout( + plot_bgcolor = "#e5ecf6", + xaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ), + yaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ) + ) ``` You can also use a pipe instead of the approach used above: ```{r} -library(plotly) - -fig <- plot_ly()%>% - add_trace(x = c(1, 2, 3), y = c(1, 3, 1), type = 'scatter', mode = 'lines+markers')%>% - config(modeBarButtonsToRemove = c('zoom2d','pan2d'))%>%layout(plot_bgcolor='#e5ecf6', - xaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff'), - yaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff') - ) +library(plotly) + +fig <- plot_ly() %>% + add_trace(x = c(1, 2, 3), y = c(1, 3, 1), type = "scatter", mode = "lines+markers") %>% + config(modeBarButtonsToRemove = c("zoom2d", "pan2d")) %>% + layout( + plot_bgcolor = "#e5ecf6", + xaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ), + yaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ) + ) + fig ``` @@ -258,67 +312,89 @@ fig Some modebar buttons of Cartesian plots are optional and have to be added explicitly, using the `modeBarButtonsToAdd` config attribute. These buttons are used for drawing or erasing shapes. See [the tutorial on shapes and shape drawing](https://plotly.com/r/shapes/#drawing-shapes-on-cartesian-plots) for more details. ```{r} -library(plotly) -data(iris) - -fig <- plot_ly(data = iris, x = ~Petal.Width, y = ~Sepal.Length, color = ~Species, - type = "scatter", mode = "markers")%>% - layout(title="A Plotly Figure", legend=list(title=list(text='species'))) - -fig <- fig %>% layout(dragmode='drawopenpath', - newshape=list(line = list(color='cyan')), - title = 'Draw a path to separate versicolor and virginica') - -#Add modebar buttons -config(fig,modeBarButtonsToAdd = list('drawline', - 'drawopenpath', - 'drawclosedpath', - 'drawcircle', - 'drawrect', - 'eraseshape')) %>%layout(plot_bgcolor='#e5ecf6', - xaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff'), - yaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff') - ) +library(plotly) +data(iris) + +fig <- plot_ly( + data = iris, x = ~Petal.Width, y = ~Sepal.Length, color = ~Species, + type = "scatter", mode = "markers" +) %>% + layout(title = "A Plotly Figure", legend = list(title = list(text = "species"))) + +fig <- fig %>% + layout( + dragmode = "drawopenpath", + newshape = list(line = list(color = "cyan")), + title = "Draw a path to separate versicolor and virginica" + ) + +# Add modebar buttons +config(fig, modeBarButtonsToAdd = list( + "drawline", + "drawopenpath", + "drawclosedpath", + "drawcircle", + "drawrect", + "eraseshape" +)) %>% + layout( + plot_bgcolor = "#e5ecf6", + xaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ), + yaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ) + ) ``` You can also use a pipe instead of the approach used above: ```{r} -library(plotly) -data(iris) - -fig <- plot_ly(data = iris, x = ~Petal.Width, y = ~Sepal.Length, color = ~Species, - type = "scatter", mode = "markers")%>% - layout(title="A Plotly Figure", legend=list(title=list(text='species'))) - -fig <- fig %>% layout(dragmode='drawopenpath', - newshape=list(line = list(color='cyan')), - title = 'Draw a path to separate versicolor and virginica') - -#Add modebar buttons -fig <- fig %>% - config(modeBarButtonsToAdd = c('drawline', - 'drawopenpath', - 'drawclosedpath', - 'drawcircle', - 'drawrect', - 'eraseshape')) %>%layout(plot_bgcolor='#e5ecf6', - xaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff'), - yaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff') - ) - +library(plotly) +data(iris) + +fig <- plot_ly( + data = iris, x = ~Petal.Width, y = ~Sepal.Length, color = ~Species, + type = "scatter", mode = "markers" +) %>% + layout(title = "A Plotly Figure", legend = list(title = list(text = "species"))) + +fig <- fig %>% + layout( + dragmode = "drawopenpath", + newshape = list(line = list(color = "cyan")), + title = "Draw a path to separate versicolor and virginica" + ) + +# Add modebar buttons +fig <- fig %>% + config(modeBarButtonsToAdd = c( + "drawline", + "drawopenpath", + "drawclosedpath", + "drawcircle", + "drawrect", + "eraseshape" + )) %>% + layout( + plot_bgcolor = "#e5ecf6", + xaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ), + yaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ) + ) + fig ``` @@ -327,25 +403,31 @@ Sets the maximum delay between two consecutive clicks to be interpreted as a dou This setting propagates to all on-subplot double clicks (except for `geo` and `mapbox`). ```{r} -library(plotly) - -fig <- plot_ly()%>% - add_trace(y = c(3, 5, 3, 2), x = c("2019-09-02", "2019-10-10", "2019-11-12", "2019-12-22"), - type = 'bar', - texttemplate = "%{label}", - textposition = "inside")%>% - layout(xaxis = list(type = 'date')) - -config(fig, doubleClickDelay= 1000)%>%layout(plot_bgcolor='#e5ecf6', - xaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff'), - yaxis = list( - zerolinecolor = '#ffff', - zerolinewidth = 2, - gridcolor = 'ffff') - ) +library(plotly) + +fig <- plot_ly() %>% + add_trace( + y = c(3, 5, 3, 2), x = c("2019-09-02", "2019-10-10", "2019-11-12", "2019-12-22"), + type = "bar", + texttemplate = "%{label}", + textposition = "inside" + ) %>% + layout(xaxis = list(type = "date")) + +config(fig, doubleClickDelay = 1000) %>% + layout( + plot_bgcolor = "#e5ecf6", + xaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ), + yaxis = list( + zerolinecolor = "#ffff", + zerolinewidth = 2, + gridcolor = "ffff" + ) + ) ``` ### Configuring Figures in Dash Apps