Skip to content

Commit d2b2ff0

Browse files
committed
feat: add lines to the scatterplot
1 parent 1a42f2a commit d2b2ff0

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

R/tm_p_scatterplot.R

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ ui_p_scatterplot <- function(id) {
3030
ns <- NS(id)
3131
bslib::page_sidebar(
3232
sidebar = div(
33+
bslib::input_switch(ns("add_lines"), "Add lines", value = FALSE),
3334
colour_picker_ui(ns("colors"))
3435
),
3536
tags$div(
@@ -72,6 +73,7 @@ srv_p_scatterplot <- function(id,
7273
y_var = str2lang(y_var),
7374
color_var = str2lang(color_var),
7475
colors = color_inputs(),
76+
add_lines = input$add_lines,
7577
expr = {
7678
plot_data <- scatterplot_ds |>
7779
dplyr::select(subject_var, x_var, y_var, color_var) |>
@@ -83,10 +85,24 @@ srv_p_scatterplot <- function(id,
8385
color = ~color_var,
8486
colors = colors,
8587
mode = "markers",
86-
type = "scatter"
88+
type = "scatter",
89+
source = "scatterplot"
8790
) |>
8891
plotly::layout(dragmode = "select")
89-
p()
92+
93+
if (add_lines) {
94+
p <- p %>%
95+
plotly::add_trace(
96+
x = ~x_var,
97+
y = ~y_var,
98+
split = ~subject_var,
99+
mode = "lines",
100+
line = list(color = "grey"),
101+
showlegend = FALSE,
102+
inherit = FALSE
103+
)
104+
}
105+
p
90106
}
91107
)
92108
})

R/utils.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ setup_trigger_tooltips <- function(plot, ns) {
485485
#' @keywords internal
486486
#' @noRd
487487
set_plot_data <- function(plot, data_id) {
488+
# Make sure to have a `customdata` column in the dataset and pass it to `plotly::plot_ly`.
488489
htmlwidgets::onRender(
489490
plot,
490491
paste0(

0 commit comments

Comments
 (0)