Introduction
Pascal Schmidt
2025-04-21
Introduction.Rmd
Introduction
- The main function in
SveltePlots
is thesp()
function where we can specify the chart type,line
,points
/scatter
,bar
,hist
, anddensity
. - All functions start with
sp_
. - Series can be drawn individually with
sp_add_series()
and it currently supportspoints
,line
, orbands
. - There is also a
spaes()
function which works similar to theaes()
function inggplot2
.
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.
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
)