Skip to contents

A plot needs to have at least the following 3 components:

  • data: the data frame used, initialized via x_chart(data = data)

  • x: an x variable (quoted column name, default to “x”), can be passed in x_chart or (overridden) in later calls of specific plot types, e.g. either x_chart(x = "x") of x_bar(x = "x") is acceptable.

  • y: an y variable, default to “y”, settings are similar to x

xkcd provides plot types for line charts, bar charts, pie charts, scatter plots and radar charts.

Plot types

Line graph

df <- gapminder::gapminder |>
  filter(country %in% c("United States", "United Kingdom", "Canada", "Australia"))

df |>
  x_chart(title = "my xkcd line graph", x = "year", y = "lifeExp") |>
  x_line(color = "country")

Scatterplot

iris |>
  x_chart() |>
  x_point(x = "Sepal.Width", 
          y = "Sepal.Length", 
          fill = "Species", 
          size = 1.5) |>
  x_options(title = "scatterplot", palette = c("#f7feae", "#46aea0", "#045275"))

The line parameter in x_point controls wheather points are connected, this can be used like geom_point + geom_line in ggplot2.

data.frame(x = 1:10, y = rnorm(10)) |>
  x_chart() |>
  x_point(line = TRUE) |>
  x_options(ylabel = "")

Bar and pie plot

data.frame(
  character = c("Ross", "Rachel", "Joey", "Phoebe", "Monica", "Chandler"),
  relationships = c(16, 14, 17, 16, 13, 9)
) |>
  x_chart(title = "# of relationships in Friends") |>
  x_bar(x = "character", y = "relationships") |>
  x_options(legend = FALSE)

Stacked bar chart

t <- as.data.frame(Titanic)
t <- t[rep(1:nrow(t), t$Freq), c("Class", "Survived")]

t |>
  count(Class, Survived) |>
  x_chart(title = "Titanic survivors by class and age") |>
  x_bar(x = "Class", y = "n", fill = "Survived") |>
  x_options(ylabel = "")

pie chart (does not support grouping)

data.frame(
  x = 1:6,
  y = 1:6
) |>
  x_chart(title = "What I am made of") |>
  x_pie(radius = 0) |>
  x_options(legend_position = 2)

Radar chart

data.frame(
  x = rep(c("strength", "patience", "skillfulness", "resilience", "flexibility"), 2),
  y = abs(rnorm(10)),
  g = rep(c("tiger", "lion"), each = 5)
) |>
  x_chart(title = "Tiger and lion: a random comparison") |>
  x_radar(color = "g")

Configuration options

Common options that apply to all types of plots (set via x_options):

  • title: plot title
  • xlabel: x-axis title, default to x variable
  • ylabel: y-axis title, default to x variable
  • palette: color palette for grouping variables, the number of colors should equal the number of groups
  • font_family: fonts to use, default to “xkcd”
  • legend: whether to show the legend
  • legend_position: legend position, 1 (top-right), 2 (top-left), 3 (bottom-left), 4 (bottom-right)
  • time_format: time format to use if the x values are time, chart.xkcd use dayjs to format time, find the all the available formats at https://github.com/iamkun/dayjs/blob/dev/docs/en/API-reference.md#list-of-all-available-formats
  • x_breaks: number of breaks for x axis, default is 3
  • y_breaks: number of breaks for y axis, default is 3

specific options for each plot type:

  • scatterplot
    • size: dot size, default to 1
    • line: if dots should be connected, default to FALSE
  • pie chart:
    • radius: arc radius, default to 0.5, for traditional pie chart set it to 0
  • radar chart:
    • size: dot size, default to 1
    • label: whether to show labels for each dimension, default to TRUE
    • ticks number of ticks in each dimension