Common Problems Encountered When Visualizing in R
Common Problems Encountered When Visualizing in R
Common Problems Encountered When Visualizing in R
visualizing in R
Coding errors are an inevitable part of writing code—especially when you are first beginning
to learn a new programming language. In this reading, you will learn how to recognize
common coding errors when creating visualizations using ggplot2. You will also find links to
some resources that you can use to help address any coding problems you might encounter
moving forward.
Let’s explore a few of the most common coding errors you might encounter in ggplot2.
Case sensitivity
R code is case sensitive. If you accidentally capitalize the first letter in a certain function, it
might affect your code. Here is an example:
Glimpse(penguins)
The error message lets you know that R cannot find a function named “Glimpse”:
But you know that the function “glimpse” (lowercase “g”) does exist. Notice that the error
message doesn’t explain exactly what is wrong but does point you in a general direction.
Based on that, you can figure out that this is the correct code:
glimpse(penguins)
RStudio does alert you to the problem. To the left of the line of code in your RStudio source
editor, you might notice a red circle with a white “X” in the center. If you hover over the circle
with your cursor, this message appears:
RStudio lets you know that you have an unmatched opening bracket. So, to correct the code,
you know that you need to add a closing bracket to match each opening bracket.
ggplot(data = penguins) +
geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g))
Here’s an example of code that includes incorrect placement of the plus sign:
ggplot(data = penguins)
+ geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g))
In this case, R’s error message identifies the problem, and prompts you to correct it:
ggplot(data = penguins) +
geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g))
You also might accidentally use a pipe instead of a plus sign to add a new layer to your plot, like
this:
ggplot(data = penguins)%>%
geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g))
You then get the following error message:
ggplot(data = penguins) +
geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g))
Keeping these issues in mind and paying attention to details when you write code will help you
reduce errors and save time, so you can stay focused on your analysis.
Help resources
Everyone makes mistakes when writing code–it is just part of the learning process.
Fortunately, there are lots of helpful resources available in RStudio and online.
● R documentation
○ R has built-in documentation for all functions and packages. To learn more
about any R function, just run the code ?function_name. For example, if you
want to learn more about the geom_bar function, type:
?geom_bar
When you run the code, an entry on “geom_bar” appears in the Help viewer in
the lower-right pane of your RStudio workspace. The entry begins with a
“Description” section that discusses bar charts:
The RDocumentation website contains much of the same content in a
slightly different format, with additional examples and links.
● ggplot2 website
○ The ggplot2 website, which is part of the official tidyverse documentation, is a
great resource for all things related to ggplot2. It includes entries on key topics,
useful examples of code, and links to other helpful resources.
● Search online
○ Doing an online search for the error message you are encountering (include “R”
and the function or package name in your search terms) is another option.
There is a good chance someone else has already encountered the same error
and posted about it online.
● The R community
○ If the other resources don’t help, you can try reaching out to the R community
online. There are lots of useful online forums and websites where people ask for
and receive help, including:
● R for Data Science Online Learning Community
● RStudio Community
● Stackoverflow
● Twitter (#rstats)