How to make a scatterplot in R | R-bloggers

How to make a scatterplot in R | R-bloggers

Now that you’ve learned the very basics of plotting from our earlier tutorial on making your very first plot in R, this blog post will teach you how to customize your scatterplots to make them look better. If you want to take this even a step further, check out my step-by-step tutorial introduction to publication-quality scatterplots.

You can also watch this blog post as a video by clicking on the image below.

Scatterplots are one of the most common types of plots in ecology, where they show the relationship (or lack thereof) between two continuous variables.

We’re going to create the same scatterplot that we did in the other lesson by loading up the data set .

This data set has 30 rows of data and two columns. The first column, “weight”, represents the dry biomass of each plant in grams. The second column, “group”, lists the experimental treatment that each plant was given. We’re going to add another column to this data set called “water”, which will describe the amount of water that each plant has received throughout its life, in liters. If you’re following along in RStudio (which you should be! ????), then you can just copy and paste the code below to add the new column.

Awesome. Now, using the function, let’s create a plot of plant weight versus the amount of water that the plant received.

Now we have a basic scatterplot, but it doesn’t look all that great aesthetically. To help with that, I’m going to show you some different customizations that allow you to modify several of the plot elements.

Let’s start with the axis labels. We can modify the and arguments within the function. refers to the label on the X axis, while refers to the label on the Y axis. Notice that I also pressed the “Enter” or “Return” key after each comma in the function. This just keeps the code cleaner and more readable, but you could have also written it all in one long line.

Great! Our axis labels look good. We can also make the graph a little more spacious by editing the limits of the axes. We can do this using the and arguments. These arguments accept vectors of the form . So if we wanted the X axis to go from 1 to 5, we would say .

Nice, our plot looks a little less crowded. The last aspect of the axes that you might want to change are the axis tick marks. We can do this using the and arguments. These arguments accept vectors in the form . So if we want the X axis tick marks to go from 1.25 to 3.75 with 5 intervals in between, we would write .

Now let’s change the appearance of the points in the plot. The open circles that we currently have can be nice, especially if many of the points overlap. However, normally we would probably want to have simple, filled-in circles.

We can change the shape of the points using the argument. 16 happens to be the value that corresponds to filled-in points, but you can play around with other numbers to see the types of symbols that are available.

You can also change the color of the points using the argument, where you can just type the name of a color in quotes.

It can be fun to use different colors, but the best practice is to keep your figures in grayscale unless the colors in your figure specifically signify something. In the case of our figure, there isn’t really a reason to change the color of the points except for the purposes of demonstration. So let’s change the color back to black.

You can also change point size using the argument . The default for is 1, which represents 100%. So if we change the argument to 1.5, the points will be 50% larger.

And now we have a nicer-looking scatterplot. The axis labels are clearer, the points have been filled in, and our plot looks less crowded. Now you know how to customize the axis labels, the axis tick marks and limits, and the point shape, color, and size within your scatterplot.

There is of course a lot more that you can do, but this tutorial is aimed at giving you the most important attributes that you can modify in the base function. I used only these for the longest time without needing to branch out to ggplot or other more advanced techniques. But be sure to check out my other tutorial that takes this just a bit further to show you how to make publication-quality scatterplots. Happy visualizing!

Images Powered by Shutterstock