R - Line Graphs: A Beginner's Guide

Welcome, aspiring data visualizers! Today, we're going to embark on an exciting journey into the world of R and line graphs. Don't worry if you've never written a line of code before - we'll start from the very beginning and work our way up together. By the end of this tutorial, you'll be creating beautiful line graphs that will make your data sing!

R - Line Graphs

What is a Line Graph?

Before we dive into R, let's quickly discuss what a line graph is. Imagine you're tracking your daily coffee consumption over a month. A line graph would show this data as a series of points connected by lines, with days on the x-axis and number of coffees on the y-axis. It's perfect for showing trends over time!

Getting Started with R

First things first, let's make sure R is set up on your computer. If you haven't installed R and RStudio yet, head over to the R project website and RStudio website to download and install them.

Once you have R and RStudio ready, open RStudio and let's begin our line graph adventure!

Creating Your First Line Graph

Step 1: Preparing Your Data

Let's start with a simple example. We'll create a line graph showing the average temperature for a week.

# Create vectors for days and temperatures
days <- c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
temps <- c(20, 22, 25, 23, 21, 19, 22)

# Combine into a data frame
weather_data <- data.frame(Day = days, Temperature = temps)

# View the data
print(weather_data)

This code creates a simple dataset with days of the week and corresponding temperatures. The data.frame() function combines our days and temperatures into a neat table-like structure.

Step 2: Creating a Basic Line Graph

Now, let's create our first line graph using the plot() function:

plot(weather_data$Temperature, type = "l", col = "blue",
     xlab = "Day of the Week", ylab = "Temperature (°C)",
     main = "Weekly Temperature")

Let's break this down:

  • weather_data$Temperature: This selects the Temperature column from our data.
  • type = "l": This tells R to create a line plot ("l" stands for line).
  • col = "blue": This sets the color of our line to blue.
  • xlab and ylab: These set labels for our x and y axes.
  • main: This sets the title of our graph.

Enhancing Your Line Graph

Adding Points to Your Line

To make our graph more informative, let's add points at each data point:

plot(weather_data$Temperature, type = "b", col = "blue", pch = 16,
     xlab = "Day of the Week", ylab = "Temperature (°C)",
     main = "Weekly Temperature")

The type = "b" parameter tells R to include both lines and points ("b" stands for both). The pch = 16 parameter sets the point shape to a filled circle.

Customizing the X-Axis

Our x-axis currently shows numbers instead of days. Let's fix that:

plot(weather_data$Temperature, type = "b", col = "blue", pch = 16,
     xlab = "Day of the Week", ylab = "Temperature (°C)",
     main = "Weekly Temperature", xaxt = "n")
axis(1, at = 1:7, labels = weather_data$Day)

The xaxt = "n" parameter suppresses the default x-axis, and the axis() function allows us to create a custom x-axis with our day labels.

Multiple Lines in a Line Chart

Now that we've mastered the basics, let's kick it up a notch! Imagine we want to compare temperatures from two different weeks.

# Create data for two weeks
week1_temps <- c(20, 22, 25, 23, 21, 19, 22)
week2_temps <- c(19, 21, 24, 25, 23, 18, 20)

# Combine into a data frame
weather_data <- data.frame(
  Day = days,
  Week1 = week1_temps,
  Week2 = week2_temps
)

# Create the plot
plot(weather_data$Week1, type = "b", col = "blue", pch = 16,
     xlab = "Day of the Week", ylab = "Temperature (°C)",
     main = "Two-Week Temperature Comparison", xaxt = "n", ylim = c(15, 30))
lines(weather_data$Week2, type = "b", col = "red", pch = 17)
axis(1, at = 1:7, labels = weather_data$Day)
legend("topright", legend = c("Week 1", "Week 2"),
       col = c("blue", "red"), pch = c(16, 17), lty = 1)

This code introduces a few new concepts:

  • We use plot() for the first line and lines() to add the second line.
  • ylim = c(15, 30) sets the y-axis range to accommodate both datasets.
  • The legend() function adds a legend to help distinguish between the two lines.

Conclusion

Congratulations! You've just created your first line graphs in R. We've covered the basics of creating simple and multiple line graphs, customizing colors, adding points, and even creating legends.

Remember, practice makes perfect. Try playing around with different datasets, colors, and styles. Before you know it, you'll be creating stunning visualizations that bring your data to life!

Here's a table summarizing the main functions we've used:

Function Purpose
plot() Create the initial plot
lines() Add additional lines to an existing plot
axis() Customize axis labels
legend() Add a legend to the plot

Happy plotting, and may your lines always trend upward!

Credits: Image by storyset