library(rtemis.draw)15 Line and area charts
A line joins one value per x in each line. power_generation is a synthetic dataset of monthly electricity generation in 2024 by source (see ?power_generation); its month column is a date, so the x-axis is a time axis spaced by elapsed time.
15.1 Single Series
solar <- power_generation[power_generation$source == "Solar", ]
draw_line(
solar$month,
solar$gwh,
xlab = "Month",
ylab = "Solar generation (GWh)"
)15.2 Multiple Series
group draws one line per level:
draw_line(
power_generation$month,
power_generation$gwh,
group = power_generation$source,
ylab = "Generation (GWh)"
)15.3 Summaries per x
When several rows share an x, aggregate combines them into one point per line. ChickWeight records the weight of each chick at each age, so the mean weight per age and diet gives one growth curve per diet:
draw_line(
ChickWeight$Time,
ChickWeight$weight,
group = ChickWeight$Diet,
aggregate = "mean",
xlab = "Age (days)",
ylab = "Mean weight (g)"
)Without aggregate, two rows at one x in a line are an error: a line through them would draw a spike whose order is arbitrary.
15.4 Markers
Lines carry no markers unless points = TRUE; a value with no neighbor to join keeps its marker, so no value goes undrawn.
draw_line(
solar$month,
solar$gwh,
points = TRUE,
smooth = TRUE,
ylab = "Solar generation (GWh)"
)15.5 Stacked Area Chart
The three sources add up to the total generated, so their areas stack. The y-axis of a filled area starts at zero, since each band is read by its height.
draw_line(
power_generation$month,
power_generation$gwh,
group = power_generation$source,
area = TRUE,
stack = TRUE,
ylab = "Generation (GWh)"
)15.6 Configuration
setup_LineConfig() describes a line chart by column names; draw() binds it to a data frame:
config <- setup_LineConfig(
x = "month", y = "gwh", group = "source", area = TRUE, stack = TRUE,
ylab = "Generation (GWh)"
)
draw(config, data = power_generation)