Skip to contents

Introduction

  • The main function in SveltePlots is the sp() function where we can specify the chart type, line, points/scatter, bar, hist, and density.
  • All functions start with sp_.
  • Series can be drawn individually with sp_add_series() and it currently supports points, line, or bands.
  • There is also a spaes() function which works similar to the aes() function in ggplot2.

Scatter Plots

The first chart we are building is a scatter plot. We are providing the data frame, the mapping, and the type of the plot and the sp() function will plot the chart.

data("penguins")
sp(
  data = penguins,
  mapping = spaes(x = flipper_length_mm, y = bill_length_mm, group = species),
  type = "points"
) |> sp_title("Penguins")

We can also specify different colors, the size of the points, the opacity, if we want tooltips and a legend, and the height of the chart.

sp(
  data = penguins,
  mapping = spaes(x = flipper_length_mm, y = bill_length_mm, group = species),
  type = "points",
  size = 3, 
  alpha = 1,
  colors = c("red", "blue", "green"),
  height = 500
)
n <- 500  

variable1 <- rnorm(n, mean = 0, sd = 1)  
variable2 <- rnorm(n, mean = 5, sd = 2) 

big_data <- data.frame(Variable1 = variable1, Variable2 = variable2)

sp(
  data = big_data,
  type = "points",
  mapping = spaes(x = Variable1, y = Variable2), 
  alpha = 0.5,
  height = 500,
  include_legend = FALSE
)

Line Charts

data("walmart_sales_weekly")
walmart_sales_weekly <- walmart_sales_weekly |>
  dplyr::select(Dept, Date, Weekly_Sales) |>
  dplyr::arrange(Date) |>
  dplyr::as_tibble() |>
  dplyr::mutate(
    Date = lubridate::ymd(Date)
  )

sp(
  data = walmart_sales_weekly,
  type = "line",
  mapping = spaes(x = Date, y = Weekly_Sales, group = Dept)
) |> 
  sp_y_axis(format = "$,.3r") |> 
  sp_title("Walmart Weekly Sales")

Individual series can be added with the sp_add_series() function. For individual series, we can also specify if we want the legend and also tooltip for the series. By default, the plots displays the tooltip and also legend.

data("purchases")
sp(
  data = purchases, 
  type = "points",
  mapping = spaes(x = date, y = revenue_roll, group = age)
) |>
  sp_add_series(
    data = purchases[purchases$revenue == max(purchases$revenue), ], 
    mapping = spaes(x = date, y = revenue, group = age),
    type = "points",
    size = 5
  ) 

Bar Charts

data("fruit")
sp(
  data = fruit[, c("year", "fruit", "value")] |> 
    dplyr::filter(fruit != "bananas"),
  mapping = spaes(x = year, y = value, group = fruit),
  type = "bar",
  mode = "grouped",
  height = 500
)  |> 
  sp_add_series(
    data = fruit[, c("year", "fruit", "value")] |> 
      dplyr::filter(fruit == "bananas"),
    mapping = spaes(x = year, y = value, group = fruit),
    type = "bar",
  )
sp <- sp(
  data = fruit[, c("year", "fruit", "value")],
  mapping = spaes(x = year, y = value, group = fruit),
  type = "bar",
  mode = "stacked", height = 500
) |> 
  sp_tooltip(
    type = "vertical_line"
  )
sp
sp <- sp(
  data = fruit[, c("year", "fruit", "value")],
  mapping = spaes(x = year, y = value, group = fruit),
  type = "bar",
  mode = "percent", height = 500
) |> 
  sp_tooltip(
    type = "vertical_line"
  )
sp
sp(
  data = mtcars |> tibble::rownames_to_column() |> dplyr::slice(1:10),
  mapping = spaes(x = rowname, y = mpg),
  type = "bar", include_legend = FALSE
) |>  
  sp_x_axis(
    label = "Date", rotation_axis_ticks = -30,
    text_anchor = "start"
  )

Density Charts

data("gapminder")

gapminder <- gapminder |> 
  dplyr::mutate(
    country = as.character(country),
    year = lubridate::ymd(paste0(year, "-01-01"))
  )

sp(
  data = gapminder |> 
    dplyr::filter(
      year == lubridate::ymd("2007-01-01"), 
      continent != "Oceania"
    ),
  mapping = spaes(x = lifeExp, group = continent),
  type = "density", alpha = 1, size = 1
)
sp(
  data = mtcars,
  mapping = spaes(x = mpg),
  type = "density", alpha = 0.5, size = 1
)

Histograms

sp(
  data = gapminder |> dplyr::filter(year == lubridate::ymd("2007-01-01"), continent != "Oceania"),
  mapping = spaes(x = lifeExp, group = continent),
  type = "histogram", alpha = 0.5, size = 1
)
sp(
  data = mtcars,
  mapping = spaes(x = mpg),
  type = "histogram", alpha = 0.5, size = 1
) 
sp(
  data = diamonds,
  mapping = spaes(x = price),
  type = "histogram", alpha = 0.5, size = 1, breaks = 50
)