Learn R Programming

d3po (version 1.0.3)

po_scatter: Scatter

Description

Plot an scatter chart.

Usage

po_scatter(d3po, ..., data = NULL, inherit_daes = TRUE)

Value

an 'htmlwidgets' object with the desired interactive plot

Arguments

d3po

Either the output of d3po() or d3po_proxy().

...

Aesthetics, see daes().

data

Any dataset to use for plot, overrides data passed to d3po().

inherit_daes

Whether to inherit aesthetics previous specified.

Examples

Run this code
if (interactive()) {
  # Create a wide dataset with x = 2019 and y = 2023 trade values
  trade_wide_2019 <- d3po::trade[d3po::trade$year == 2019L, c("reporter", "trade")]
  trade_wide_2019 <- aggregate(trade ~ reporter, data = trade_wide_2019, FUN = sum)

  trade_wide_2023 <- d3po::trade[d3po::trade$year == 2023L, c("reporter", "trade")]
  trade_wide_2023 <- aggregate(trade ~ reporter, data = trade_wide_2023, FUN = sum)

  trade_wide <- merge(
    trade_wide_2019,
    trade_wide_2023,
    by = "reporter",
    suffixes = c("_2019", "_2023")
  )

  # my_pal <- tintin::tintin_pal(option = "red_rackhams_treasure")(7)
  # [1] "#2C8560" "#50B78F" "#6A785A" "#98BF8C" "#B95D59" "#DCC67D" "#F35A54"

  my_pal <- c("#2C8560", "#50B78F", "#6A785A", "#98BF8C", "#B95D59", "#DCC67D", "#F35A54")

  d3po(trade_wide, width = 800, height = 600) %>%
    po_scatter(daes(x = trade_2019, y = trade_2023, group = reporter, color = my_pal)) %>%
    po_labels(
      x = "Trade in 2019 (USD billion)",
      y = "Trade in 2023 (USD billion)",
      title = "Trade Volume by Country in 2019 and 2023"
    )
}

Run the code above in your browser using DataLab