How to Make Stunning Scatter Plots in R: A Complete Guide with ggplot2

Estimated time:

<h2><span data-preserver-spaces="true">Scatter Plots with R</span></h2> <span data-preserver-spaces="true">Do you want to make stunning visualizations, but they always end up looking like a potato? It's a tough place to be. Luckily, R makes it easy to produce great-looking visuals. Today you'll learn how to create impressive scatter plots with R and the <code>ggplot2</code> package.</span> <span data-preserver-spaces="true">Read the series from the beginning:</span> <ul><li><a class="editor-rtfLink" href="" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">How to Make Stunning Bar Charts with R</span></a></li><li><a class="editor-rtfLink" href="" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">How to Make Stunning Line Charts with R</span></a></li></ul> <span data-preserver-spaces="true">This article demonstrates how to make a scatter plot for any occasion and how to make it look extraordinary at the same time. After reading, visualizing relationships between any continuous variables shouldn't be a problem. Today you'll learn how to:</span> <ul><li><a href="#first-scatter-plot">Make your first scatter plot</a></li><li><a href="#basic-aesthetics">Change size, color, shapes, and themes</a></li><li><a href="#text-and-labels">Add text and labels</a></li><li><a href="#titles-subtitles">Add titles, subtitles, captions, and axis labels</a></li><li><a href="#other">Add other useful things</a></li></ul> <h2 id="first-scatter-plot"><span data-preserver-spaces="true">Make Your First Scatter Plot</span></h2> <span data-preserver-spaces="true">R has many datasets built-in, and one of them is <code>mtcars</code>. It's one of the most popular datasets, and today you'll use it to make a lot of scatter plots. Package-wise, you'll only need <code>ggplot2</code>. </span> <span data-preserver-spaces="true">Here's how to import the packages and take a look at the first couple of rows:</span> <script src=""></script> <img class="size-full wp-image-6274" src="" alt="Image 1 - Head of MTCars dataset" width="1000" height="242" /> Image 1 - Head of MTCars dataset <span data-preserver-spaces="true">The most widely used R package for data visualization is <code>ggplot2</code>. It's a straightforward package based on the layering principle. The first layer is used to specify the data, and the layers after are used to make and tweak the visualization.</span> <span data-preserver-spaces="true">The <code>geom_point()</code> layer is used to draw scatter plots. Your first chart will show the relationship between the <code>mpg</code> attribute on the x-axis, and the <code>hp</code> column on the y-axis:</span> <script src=""></script> <img class="size-full wp-image-6275" src="" alt="Image 2 - Relationship between MPG and HP variables" width="2162" height="1378" /> Image 2 - Relationship between MPG and HP variables <span data-preserver-spaces="true">We agree with you - it's not the prettiest visualization. You'll learn how to deal with that in the following sections.</span> <h2 id="basic-aesthetics"><span data-preserver-spaces="true">Change Size, Color, Shape, and Theme</span></h2> <span data-preserver-spaces="true">You can't make stunning visuals with default stylings. You can change a couple of things in the <code>geom_point()</code> layer, such as shape, color, size, and so on.</span> <span data-preserver-spaces="true">Here's how to make the points blue and a bit larger:</span> <script src=""></script> <img class="size-full wp-image-6276" src="" alt="Image 3 - Changing size and color" width="2162" height="1378" /> Image 3 - Changing size and color <span data-preserver-spaces="true">Better, but what if you don't want to hardcode color and size values? You can put variable names instead. Here's how to change the color based on the <code>cyl</code> variable and size by <code>qsec</code>:</span> <script src=""></script> <img class="size-full wp-image-6277" src="" alt="Image 4 - Changing size and color by variables" width="2162" height="1378" /> Image 4 - Changing size and color by variables <span data-preserver-spaces="true">Changing shapes is also straightforward. Dots aren't appropriate for every use case, and you're free to change the shape with the <code>shape</code> attribute. You can find the list of all available shapes </span><a class="editor-rtfLink" href="" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">here</span></a><span data-preserver-spaces="true">.</span> <span data-preserver-spaces="true">The following code snippet replaces dots with triangles:</span> <script src=""></script> <img class="size-full wp-image-6278" src="" alt="Image 5 - Replacing dots with triangles" width="2162" height="1378" /> Image 5 - Replacing dots with triangles <span data-preserver-spaces="true">And finally, let's talk about themes. The default one isn't for everyone because it's a bit too harsh with the background. With R, you can change the theme with a single line of code:</span> <script src=""></script> <img class="size-full wp-image-6279" src="" alt="Image 6 - Changing chart theme" width="2162" height="1378" /> Image 6 - Changing chart theme <span data-preserver-spaces="true">Now that's progress. But it's still not quite there yet. Let's see how to add text and labels next.</span> <h2 id="text-and-labels"><span data-preserver-spaces="true">Add Text and Labels</span></h2> <span data-preserver-spaces="true">You can use text and labels to add additional information to your visualizations. The only difference between these two is that there's a box around labels, making it easier to read. </span> <span data-preserver-spaces="true">Here's how to add text to represent car names:</span> <script src=""></script> <img class="size-full wp-image-6280" src="" alt="Image 7 - Adding text to the visualization" width="2162" height="1378" /> Image 7 - Adding text to the visualization <span data-preserver-spaces="true">The <code>ggrepel</code> package is here to prevent the overlap between text. You can add text with the plain <code>geom_text</code> layer, but it would be impossible to read the text for the points that are close. </span> <span data-preserver-spaces="true">If this still isn't as readable as you would want, use labels instead of text. Here's how:</span> <script src=""></script> <img class="size-full wp-image-6281" src="" alt="Image 8 - Adding labels to the visualization" width="2162" height="1378" /> Image 8 - Adding labels to the visualization <span data-preserver-spaces="true">Visualization isn't complete without title and axis labels. Let's see how to add and style these next.</span> <h2 id="titles-subtitles"><span data-preserver-spaces="true">Add Titles, Subtitles, Captions, and Axis Labels</span></h2> <span data-preserver-spaces="true">First, you'll learn how to add titles, subtitles, and captions to the chart. The title is mandatory for any decent visualization, and the other two can help further clarify things and for citing sources, respectively.</span> <span data-preserver-spaces="true">The most convenient way to add these is through a <code>labs()</code> layer. It takes in values for <code>title</code>, <code>subtitle</code>, and <code>caption</code>:</span> <script src=""></script> <img class="size-full wp-image-6282" src="" alt="Image 9 - Adding title, subtitle, and caption" width="2162" height="1378" /> Image 9 - Adding title, subtitle, and caption <span data-preserver-spaces="true">By default, these don't look so great. You can change color, size, alignment, and emphasize/italicize the text in the <code>theme()</code> layer. Here's how:</span> <script src=""></script> <img class="size-full wp-image-6283" src="" alt="Image 10 - Styling title, subtitle, and caption" width="2162" height="1378" /> Image 10 - Styling title, subtitle, and caption <span data-preserver-spaces="true">Let's talk about axis labels next. You can change and style them the same you did with titles, subtitles, and captions - in <code>labs()</code> and <code>theme()</code> layers. </span> <span data-preserver-spaces="true">The code snippet below adds labels for both X and Y axes and styles them a bit:</span> <script src=""></script> <img class="size-full wp-image-6284" src="" alt="Image 11 - Adding and styling axis labels" width="2162" height="1378" /> Image 11 - Adding and styling axis labels <span data-preserver-spaces="true">To wrap things up, let's take a look at a couple of useful tweaks you can do to scatter plots that don't fall into any of the discussed sections.</span> <h2 id="other"><span data-preserver-spaces="true">Add Other Useful Things</span></h2> <span data-preserver-spaces="true">Let's start by changing the legend position. The default position on the right might not be the best for some use cases. You can put the legend on the top by adding the <code>legend.position</code> argument to the <code>theme()</code> layer and specifying the position.</span> <span data-preserver-spaces="true">Here's an example:</span> <script src=""></script> <img class="size-full wp-image-6285" src="" alt="Image 12 - Changing the legend position" width="2162" height="1378" /> Image 12 - Changing the legend position <span data-preserver-spaces="true">The other potentially useful layer you can use is <code>geom_rug()</code>. It shows the variable distribution on the edges of both X and Y axes for the specified variables.</span> <span data-preserver-spaces="true">Here's how to use it:</span> <script src=""></script> <img class="aligncenter size-full wp-image-6286" src="" alt="Image 13 - Adding the geom_rug() layer" width="2162" height="1378" /> <span data-preserver-spaces="true">With this layer, you can get a rough idea of how your variables are distributed and on which point(s) most of the observations are located.</span> <h2><span data-preserver-spaces="true">Conclusion</span></h2> <span data-preserver-spaces="true">Today you've learned how to make scatter plots with R and <code>ggplot2</code> and how to make them aesthetically pleasing. You've learned how to change colors, marker types, size, titles, subtitles, captions, axis labels, and a couple of other useful things.</span> <span data-preserver-spaces="true">This alone will be enough to make almost any data visualization you can imagine. It's up to you now to choose an appropriate theme, color, and title.</span> <span data-preserver-spaces="true">You can expect more basic R tutorials weekly. Fill out the subscribe form below, so you never miss an update.</span> <span data-preserver-spaces="true">BQ: Are you completely new to R but have some programming experience? </span><a class="editor-rtfLink" href="" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">Check out our detailed R guide for programmers.</span></a> <h2><span data-preserver-spaces="true">Learn More:</span></h2><ul><li><a class="editor-rtfLink" href="" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">How Our Project Leader Built Her First Shiny Dashboard with No R Experience</span></a></li><li><a class="editor-rtfLink" href="" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">A crash course in R Shiny UI</span></a></li><li><a class="editor-rtfLink" href="" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">How to translate R Shiny dashboards</span></a></li><li><a class="editor-rtfLink" href="" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">How to make R Shiny faster</span></a></li><li><a class="editor-rtfLink" href="" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">How to scale R Shiny dashboards</span></a></li></ul> <a href="" target="_blank" rel="noopener noreferrer"><img class="aligncenter size-large wp-image-5940" src="" alt="" width="1024" height="576" /></a> <p style="text-align: center;"><strong><span data-preserver-spaces="true">Appsilon is hiring for remote roles! See our </span></strong><a class="editor-rtfLink" href="" target="_blank" rel="noopener noreferrer"><strong><span data-preserver-spaces="true">Careers</span></strong></a><strong><span data-preserver-spaces="true"> page for all open positions, including <a href="" target="_blank" rel="noopener noreferrer">R Shiny Developers</a>, <a href="" target="_blank" rel="noopener noreferrer">Fullstack Engineers</a>, <a href="" target="_blank" rel="noopener noreferrer">Frontend Engineers</a>, a <a href="" target="_blank" rel="noopener noreferrer">Senior Infrastructure Engineer</a>, </span></strong><strong><span data-preserver-spaces="true">and a </span></strong><a class="editor-rtfLink" href="" target="_blank" rel="noopener noreferrer"><strong><span data-preserver-spaces="true">Community Manager</span></strong></a><strong><span data-preserver-spaces="true">. Join Appsilon and work on groundbreaking projects with the world's most influential Fortune 500 companies.</span></strong></p>

Contact us!
Damian's Avatar
Damian Rodziewicz
Head of Sales
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Have questions or insights?
Engage with experts, share ideas and take your data journey to the next level!
Join Slack
Explore Possibilities

Take Your Business Further with Custom Data Solutions

Unlock the full potential of your enterprise with our data services, tailored to the unique needs of Fortune 500 companies. Elevate your strategy—connect with us today!

Talk to our Experts
data visualization