Creating a Smoother Line Chart like Google Sheets with ggplot2

Emulating Google Sheets Smoother Line Chart with ggplot2

Google Sheets provides a feature to create smoothed line charts that draw a curve through all data points. This post will guide you on how to emulate this feature using the popular R library, ggplot2.

Introduction

R is a powerful statistical programming language that offers an extensive range of libraries and tools for data analysis and visualization. One of the most widely used data visualization libraries in R is ggplot2. This post will explore how to use ggplot2 to create a smoother line chart similar to Google Sheets’ smoothed line charts.

Background

Google Sheets provides several features to create interactive and dynamic visualizations, including the smoothed line chart feature. The smoothed line chart is created using the loess function in R, which estimates a smooth curve through a set of data points. However, the loess function can be prone to errors and produce unusual results.

On the other hand, ggplot2 provides several alternative methods to create smoother lines, including the geom_smooth function with various methods such as linear regression (lm), generalized additive models (gam), and non-parametric methods like loess.

Data Preparation

Before we dive into creating a smoothed line chart using ggplot2, let’s prepare our data. We will use the same dataset provided in the original question:

library(tidyverse)

data <- tibble(
  date = seq.Date(as.Date("2018-12-01"), as.Date("2018-12-20"), by = "days"),
  var = c(0.329, 0.348, 0.349, 0.355, 0.382, 0.363, 0.340, 0.359, 0.336, 0.358, 0.398, 0.389, 0.389, 0.390, 0.383, 0.343, 0.352, 0.415, 0.397, 0.430),
  lower = c(0.311, 0.330, 0.330, 0.336, 0.364, 0.345, 0.321, 0.342, 0.319, 0.341, 0.384, 0.375, 0.375, 0.374, 0.369, 0.329, 0.337, 0.400, 0.382, 0.417),
  upper = c(0.347, 0.366, 0.368, 0.374, 0.400, 0.381, 0.358, 0.377, 0.354, 0.375, 0.413, 0.404, 0.403, 0.405, 0.397, 0.357, 0.368, 0.430, 0.412, 0.444)
)

Creating a Smoother Line Chart with ggplot2

To create a smoother line chart using ggplot2, we can use the geom_smooth function and specify the method as “loess”. However, this will produce an uneven curve that may not be visually appealing.

A better approach is to use the geom_xspline function from the ggalt package, which provides a more accurate and smooth curve through all data points.

Here’s an example code snippet:

library(ggplot2)
library(ggalt)

ggplot(data, aes(seq_along(date), var)) + 
  geom_point(size = 2, color = "blue", alpha = 0.2) +
  geom_xspline() +
  theme_classic() +
  theme(axis.line = element_line(size = 0.5, colour = "grey80"))

This code will produce a smoother line chart with a more accurate curve through all data points.

Conclusion

In this post, we explored how to create a smoothed line chart similar to Google Sheets using ggplot2 and the ggalt package. We used the geom_xspline function to achieve a more accurate and smooth curve through all data points. This approach provides a better alternative to using the loess function or other non-parametric methods.

References


Last modified on 2025-04-04