Day 4 of viz with me

Data viz Beginner ggplot scatterplot labels DataViz Challenge

We elevate our scatterplot with titles and labels.

Soundarya Soundararajan true
2024-10-04

Welcome to Day 4 of “Viz with Me”!

Goals for today: Adding titles and labels to our plot.

Yesterday on day 3 of viz with me series, we created our first plot.

Here’s a reminder of the code we wrote yesterday:

penguins %>%
  ggplot(aes(x = bill_length_mm, y = body_mass_g)) +
  geom_point()

This is a simplified visualization of our previous code, illustrating the flow from data to a scatterplot. While this cartoon representation may not reflect the exact syntax you’ll see in R, it serves to clarify the process and components involved in creating a scatterplot using ggplot2.

cartoon representation of R code to scatterplot

Let’s dive into the codes for today.

Today, we will learn how to add some labels to our plot, including a title and titles for the x and y axes. To do this, we will use the labs() function.

penguins %>%
  ggplot(aes(x = bill_length_mm, y = body_mass_g)) +
  geom_point() +
  labs(
    title = "My First Plot",
    x = "Bill Length of the Penguins",
    y = "Body Mass of the Penguins"
  )

Note: I have specified the title as well as the x and y labels explicitly. Each label is followed by a comma within the labs() function.

Remember two things:

  1. You can write anything within the quotes (““).

  2. While it is not necessary to break the lines for each label, we do it for better readability. You can also write the x and y labels on the same line as the title without moving to the next line (see below); it will still work, but separating them makes the code more readable. Later in the series, we will also see how to style the code, but for now, we will do it manually.

# code 1 with line breaks
penguins %>%
  ggplot(aes(x = bill_length_mm, y = body_mass_g)) +
  geom_point() +
  labs(
    title = "My First Plot",
    x = "Bill Length of the Penguins",
    y = "Body Mass of the Penguins"
  )

# code 2 without line breaks
penguins %>%
  ggplot(aes(x = bill_length_mm, y = body_mass_g)) +
  geom_point() +
  labs(title = "My First Plot", x = "Bill Length of the Penguins",y = "Body Mass of the Penguins"
  )

# Which code do you find more readable?

Both the codes above will give you the same output. The only difference is the way the code is written. You can find that the earlier one with line breaks is more readable.

Bonus Tip

You can store your base plot in an object and then use the + sign to add labels and further customizations. Here’s an example:

p <- penguins %>%
  ggplot(aes(x = bill_length_mm, y = body_mass_g)) +
  geom_point()

p  # Prints the plot

Now p is an object which holds the plot. you can call p and see that it will print the plot.

And how did we add the plot to p? We used the assign operator <- to store the plot in the object p. It can be anything but don’t start with a number or special character. That is the general rule for naming objects in R.

Now, let’s add labels to the plot using the + sign:

p + labs(
    title = "My First Plot",
    x = "Bill Length of the Penguins",
    y = "Body Mass of the Penguins",
    caption = "Data from the Palmer Penguins package"
)

If you observe, I added a caption now. You can also add subtitle—give it a try! Hint: It’s similar to adding the title, x, and y labels, but use subtitle = argument.

Now, what is an argument? An argument is something that we specify in a function. In the labs() function, the arguments we used today are title, x, y, caption, and subtitle. Each argument is followed by an equal sign and the value you want to assign to it.

A visual representation of an argument and a function

Now at the end of 4th day, your script should look like this:

When crafting titles and labels, think about what story you want to tell with your data. A clear and descriptive title can instantly convey the essence of your plot.

Jump to day 5 where we put in practice what we learnt so far!

Citation

For attribution, please cite this work as

Soundararajan (2024, Oct. 4). My R Space: Day 4 of viz with me. Retrieved from https://github.com/soundarya24/SoundBlog/posts/2024-10-04-day-4-of-viz-with-me/

BibTeX citation

@misc{soundararajan2024day,
  author = {Soundararajan, Soundarya},
  title = {My R Space: Day 4 of viz with me},
  url = {https://github.com/soundarya24/SoundBlog/posts/2024-10-04-day-4-of-viz-with-me/},
  year = {2024}
}