Skip to Content
Course content

42: Practice Exercise: Visualizing Trends with ggplot2

Click on the "Edit" button in the top corner of the screen to edit your slide content.

A few years ago, I was mentoring a junior analyst who spent an entire afternoon preparing a massive table of daily active user (DAU) counts for a quarterly review. He presented a slide with 90 rows of numbers, expecting the VP to see the growth. The VP looked at it for five seconds and asked, "I can't tell if we're actually growing or just having a few lucky Tuesdays. Can you just show me the trend?"

It was a humbling moment for him, but a perfect lesson for us both: raw data is often too noisy to communicate a point. In R, we don't just plot points; we use ggplot2 to separate the signal from the noise. When you're visualizing trends, your goal isn't to show every single fluctuation—it's to tell a story about the direction the data is moving.

Cutting Through the Volatility

When you use geom_line(), you're showing the raw truth, which is often jagged and distracting. To find the actual trend, we layer on geom_smooth(). I've found that the most common mistake people make here is leaving the default settings and not understanding what the "ribbon" around the line actually represents (it's the confidence interval).

Depending on your data, you'll want to choose between a linear model—which is great for showing a constant rate of growth—and a LOESS (locally estimated scatterplot smoothing) curve, which is better for capturing seasonal dips and peaks. In ggplot2, you control this with the method argument. If you want a straight line of best fit, use method = "lm". If you want a flexible curve that follows the data's natural flow, method = "loess" is your best bet.

# A quick look at how to layer a trend over raw data
ggplot(dau_data, aes(x = date, y = users)) +
  geom_line(alpha = 0.3) + # Fade the raw data into the background
  geom_smooth(method = "lm", color = "blue") # Highlight the overall trajectory

The Art of Visual Layering

The secret to a professional trend chart is contrast. If you plot a thick black line for the raw data and a thick red line for the trend, they fight for the viewer's attention. I always recommend "ghosting" the raw data. By setting the alpha value of your geom_line() to something low (like 0.3 or 0.4), you push the noise into the background and let the trend line pop.

You should also be careful with your x-axis. When dealing with time-series trends, R can sometimes treat dates as factors or integers, which ruins the spacing. Always ensure your date column is actually a Date object. If the trend looks "off," check your data types first—it's a common headache that wastes way too much time.




📋 Practical Task

Analyzing Quarterly User Growth Trends for a SaaS Product

You've been handed a dataset representing the daily sign-ups for a new software product over the last 90 days. The data is volatile because sign-ups spike on Mondays and drop on weekends, making it hard for the product manager to see if the marketing campaign is actually working.

Your Task:

  • Create a data frame called growth_data with two columns: day (1 to 90) and signups. For signups, use a base value that increases over time but add random noise using rnorm() to simulate daily volatility.
  • Build a ggplot2 visualization that does the following:
    • Plots the raw daily sign-ups as a line with a light grey color and low opacity (alpha).
    • Adds a linear trend line (method = "lm") in a bold color (like "darkgreen") to show the overall growth trajectory.
    • Labels the x-axis as "Day of Quarter" and the y-axis as "New Sign-ups".
    • Adds a clear title: "Daily Sign-up Growth: Quarter 1".

Ensure that the trend line is clearly visible over the "noisy" daily data.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.