Note on line chart
This section is tightly linked with other sections. A connected scatterplot is almost the same thing, but each observation is represented as a dot. An area chart fills the surface between the line and the X axis. More generally, the time series section can interest you.
connected scatter area chart time series
Step by step with ggplot2
ggplot2
allows to draw line charts thanks to the geom_line()
function. It expects as input a data frame with 2 numeric variables, one displayed on each axis. Start your journey with the most basic line chart.
Most basic Most basic line chart with R and ggplot2
Basic customization Basic customization to improve the line chart: size, color, type, theme, title and more
Log transform How to use a log transformation for the Y axis with scale_x_log10().
Deal with date How to avoid struggling with dates on the X axis
Basic Grouped line chart How to build a line chart representing several groups
Customized Grouped Pick up a nice color palette, use a theme, add titles, and more.
Linear trend geom_smooth allows to add the result of a model to your scatterplot, with confidence interval as well.
Annotation Annotation allows to highlight main features of a chart. Learn how to add text, circles, lines and more.
Dual Y axis A few examples showing how to use the sec.axis argument to add a second Y axis.
geom_ribbon
and geom_smooth
Line charts are often displayed together with confidence intervals. ggplot2
offers 2 main functions to build them. geom_ribbon allows to build the area around the curve from precomputed values. geom_smooth will compute a model for you and plot the result directly.
Geom_smooth() geom_smooth allows to add the result of a model to your scatterplot, with confidence interval as well.
Geom_ribbon() If you already know the upper and lower limits of the error envelop, geom_ribbon will plot it for you.
Mind the Spaghetti (ggplot2
)
When too many groups are displayed on the same line chart it gets very hard to get insight from the figure. This is commonly called a spaghetti chart. Here are a few alternatives using ggplot2: annotation and small multiple.
Step by step with base R
In base R, the line function
allows to build quality line charts.
Line color and Y value Change the line color according to the Y axis value
How to add a legend to base R plot The legend() function allows to add a legend. See how to use it with a list of available customization.
Reversed Y axis Learn how to flip the Y axis upside down using the ylim argument.
Polynomial curve Line charts are often used to represent the result of a statistical model.
Parameters reminder A cheatsheet to quickly reminder what option to use with what value to customize your chart.
Dual Y axis with ggplot2
Warning: a dual Y axis line chart represents the evolution of 2 series, each plotted according to its own Y scale. This kind of chart must be avoided, since playing with Y axis limits can lead to completely different conclusions. Visit data-to-viz for more info.
Why you should avoid it
Most basic dual Axis Most basic usage of sec.axis attribute to display a second Y axis.
More style Customize the chart style with title, colors, theme and more.
Mix geoms A dual axis chart mixing a line chart with a barplot.
Dual Y axis with latticeExtra
Warning: a dual Y axis line chart represents the evolution of 2 series, each plotted according to its own Y scale. This kind of chart must be avoided, since playing with Y axis limits can lead to completely different conclusions. Visit data-to-viz for more info.
Why you should avoid it
Line chart with 2 series A classic line chart with 2 series, only 1 Y axis.
Dual Y axis line chart Add a second Y axis for the second sery. See how chart conclusion gets different and wrong.
Add legend Add a legend to specify what color is linked to what value.