Skip to contents

Generates a customizable scatterplot using ggplot2. This module allows users to select variables for the x and y axes, color and size encodings, faceting options, and more. It supports log transformations, trend line additions, and dynamic adjustments of point opacity and size through UI controls.

Usage

tm_g_scatterplot(
  label = "Scatterplot",
  x,
  y,
  color_by = NULL,
  size_by = NULL,
  row_facet = NULL,
  col_facet = NULL,
  plot_height = c(600, 200, 2000),
  plot_width = NULL,
  alpha = c(1, 0, 1),
  shape = shape_names,
  size = c(5, 1, 15),
  max_deg = 5L,
  rotate_xaxis_labels = FALSE,
  ggtheme = c("gray", "bw", "linedraw", "light", "dark", "minimal", "classic", "void"),
  pre_output = NULL,
  post_output = NULL,
  table_dec = 4,
  ggplot2_args = teal.widgets::ggplot2_args()
)

Arguments

label

(character(1)) Label shown in the navigation item for the module or module group. For modules() defaults to "root". See Details.

x

(data_extract_spec or list of multiple data_extract_spec) Specifies variable names selected to plot along the x-axis by default.

y

(data_extract_spec or list of multiple data_extract_spec) Specifies variable names selected to plot along the y-axis by default.

color_by

(data_extract_spec or list of multiple data_extract_spec) optional, defines the color encoding. If NULL then no color encoding option will be displayed.

size_by

(data_extract_spec or list of multiple data_extract_spec) optional, defines the point size encoding. If NULL then no size encoding option will be displayed.

row_facet

(data_extract_spec or list of multiple data_extract_spec) optional, specifies the variable(s) for faceting rows.

col_facet

(data_extract_spec or list of multiple data_extract_spec) optional, specifies the variable(s) for faceting columns.

plot_height

(numeric) optional, specifies the plot height as a three-element vector of value, min, and max intended for use with a slider UI element.

plot_width

(numeric) optional, specifies the plot width as a three-element vector of value, min, and max for a slider encoding the plot width.

alpha

(integer(1) or integer(3)) optional, specifies point opacity.

  • When the length of alpha is one: the plot points will have a fixed opacity.

  • When the length of alpha is three: the plot points opacity are dynamically adjusted based on vector of value, min, and max.

shape

(character) optional, character vector with the names of the shape, e.g. c("triangle", "square", "circle"). It defaults to shape_names. This is a complete list from vignette("ggplot2-specs", package="ggplot2").

size

(integer(1) or integer(3)) optional, specifies point size.

  • When the length of size is one: the plot point sizes will have a fixed size.

  • When the length of size is three: the plot points size are dynamically adjusted based on vector of value, min, and max.

max_deg

(integer) optional, maximum degree for the polynomial trend line. Must not be less than 1.

rotate_xaxis_labels

(logical) optional, whether to rotate plot X axis labels. Does not rotate by default (FALSE).

ggtheme

(character) optional, ggplot2 theme to be used by default. Defaults to "gray".

pre_output

(shiny.tag) optional, text or UI element to be displayed before the module's output, providing context or a title. with text placed before the output to put the output into context. For example a title.

post_output

(shiny.tag) optional, text or UI element to be displayed after the module's output, adding context or further instructions. Elements like shiny::helpText() are useful.

table_dec

(integer) optional, number of decimal places used to round numeric values in the table.

ggplot2_args

(ggplot2_args) object created by teal.widgets::ggplot2_args() with settings for the module plot. The argument is merged with options variable teal.ggplot2_args and default module setup.

For more details see the vignette: vignette("custom-ggplot2-arguments", package = "teal.widgets")

Value

Object of class teal_module to be used in teal applications.

Note

For more examples, please see the vignette "Using scatterplot" via vignette("using-scatterplot", package = "teal.modules.general").

Examples in Shinylive

example-1

Open in Shinylive

example-2

Open in Shinylive

Examples

# general data example
data <- teal_data()
data <- within(data, {
  require(nestcolor)
  CO2 <- CO2
})

app <- init(
  data = data,
  modules = modules(
    tm_g_scatterplot(
      label = "Scatterplot Choices",
      x = data_extract_spec(
        dataname = "CO2",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["CO2"]], c("conc", "uptake")),
          selected = "conc",
          multiple = FALSE,
          fixed = FALSE
        )
      ),
      y = data_extract_spec(
        dataname = "CO2",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["CO2"]], c("conc", "uptake")),
          selected = "uptake",
          multiple = FALSE,
          fixed = FALSE
        )
      ),
      color_by = data_extract_spec(
        dataname = "CO2",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(
            data[["CO2"]],
            c("Plant", "Type", "Treatment", "conc", "uptake")
          ),
          selected = NULL,
          multiple = FALSE,
          fixed = FALSE
        )
      ),
      size_by = data_extract_spec(
        dataname = "CO2",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["CO2"]], c("conc", "uptake")),
          selected = "uptake",
          multiple = FALSE,
          fixed = FALSE
        )
      ),
      row_facet = data_extract_spec(
        dataname = "CO2",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["CO2"]], c("Plant", "Type", "Treatment")),
          selected = NULL,
          multiple = FALSE,
          fixed = FALSE
        )
      ),
      col_facet = data_extract_spec(
        dataname = "CO2",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["CO2"]], c("Plant", "Type", "Treatment")),
          selected = NULL,
          multiple = FALSE,
          fixed = FALSE
        )
      )
    )
  )
)
#> Initializing tm_g_scatterplot
#> Initializing reporter_previewer_module
if (interactive()) {
  shinyApp(app$ui, app$server)
}
# CDISC data example
data <- teal_data()
data <- within(data, {
  require(nestcolor)
  ADSL <- rADSL
})
join_keys(data) <- default_cdisc_join_keys[names(data)]

app <- init(
  data = data,
  modules = modules(
    tm_g_scatterplot(
      label = "Scatterplot Choices",
      x = data_extract_spec(
        dataname = "ADSL",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["ADSL"]], c("AGE", "BMRKR1", "BMRKR2")),
          selected = "AGE",
          multiple = FALSE,
          fixed = FALSE
        )
      ),
      y = data_extract_spec(
        dataname = "ADSL",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["ADSL"]], c("AGE", "BMRKR1", "BMRKR2")),
          selected = "BMRKR1",
          multiple = FALSE,
          fixed = FALSE
        )
      ),
      color_by = data_extract_spec(
        dataname = "ADSL",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(
            data[["ADSL"]],
            c("AGE", "BMRKR1", "BMRKR2", "RACE", "REGION1")
          ),
          selected = NULL,
          multiple = FALSE,
          fixed = FALSE
        )
      ),
      size_by = data_extract_spec(
        dataname = "ADSL",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["ADSL"]], c("AGE", "BMRKR1")),
          selected = "AGE",
          multiple = FALSE,
          fixed = FALSE
        )
      ),
      row_facet = data_extract_spec(
        dataname = "ADSL",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["ADSL"]], c("BMRKR2", "RACE", "REGION1")),
          selected = NULL,
          multiple = FALSE,
          fixed = FALSE
        )
      ),
      col_facet = data_extract_spec(
        dataname = "ADSL",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["ADSL"]], c("BMRKR2", "RACE", "REGION1")),
          selected = NULL,
          multiple = FALSE,
          fixed = FALSE
        )
      )
    )
  )
)
#> Initializing tm_g_scatterplot
#> Initializing reporter_previewer_module
if (interactive()) {
  shinyApp(app$ui, app$server)
}