How to Make Stunning Scatter Plots in R: A Complete Guide with ggplot2
<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="https://wordpress.appsilon.com/ggplot2-bar-charts/" 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="https://wordpress.appsilon.com/ggplot2-line-charts/" 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="https://gist.github.com/darioappsilon/9708d0b5da95653a345afe281897e899.js"></script> <img class="size-full wp-image-6274" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b39591aa70ad977244031c_1-3.webp" 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="https://gist.github.com/darioappsilon/211424fa352be5363c2ecfbd3ef87922.js"></script> <img class="size-full wp-image-6275" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b3959185545dccd9bfdc9f_2-3.webp" 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="https://gist.github.com/darioappsilon/65749aabe37036bec7aeebfbcf42c7c9.js"></script> <img class="size-full wp-image-6276" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b39592a3cfbe0cd7a89433_3-3.webp" 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="https://gist.github.com/darioappsilon/19d3de5f0c4fed97653f9be90d60a20d.js"></script> <img class="size-full wp-image-6277" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b3958d85545dccd9bfd969_4-3.webp" 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="http://www.sthda.com/english/wiki/ggplot2-point-shapes" 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="https://gist.github.com/darioappsilon/1a13a9a459c7deb6e42f5f9330a17027.js"></script> <img class="size-full wp-image-6278" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b395935b5a6f8240aaab68_5-3.webp" 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="https://gist.github.com/darioappsilon/eb240140b52f1c5e0b26f41d5519cc2c.js"></script> <img class="size-full wp-image-6279" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b270775c15db9346e7666a_6-3.webp" 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="https://gist.github.com/darioappsilon/8f8bf97ac87c0bb842a8f431b7034838.js"></script> <img class="size-full wp-image-6280" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b2706c580c4700e3568e4c_7-2.webp" 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="https://gist.github.com/darioappsilon/cd28c5ec573e643ede24d98446d5ba5d.js"></script> <img class="size-full wp-image-6281" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b27073c10c89140f29b5d1_8-2.webp" 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="https://gist.github.com/darioappsilon/9a43d3857ae7bb21b56852a3a67ee710.js"></script> <img class="size-full wp-image-6282" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b3958a585f274a4fb9f602_9-2.webp" 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="https://gist.github.com/darioappsilon/5a2ff2852695daa7053e2fca5002b534.js"></script> <img class="size-full wp-image-6283" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b27097adeb0800875a5f54_10-2.webp" 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="https://gist.github.com/darioappsilon/9d926560175be38af0582c62b21b9b4e.js"></script> <img class="size-full wp-image-6284" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b27098c43d885b240061e6_11-2.webp" 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="https://gist.github.com/darioappsilon/a161f46b1be93593e9796658f8c45097.js"></script> <img class="size-full wp-image-6285" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b395b375449ff96071259e_12-2.webp" 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="https://gist.github.com/darioappsilon/fdc774e827a0a44c3d5f00a0df27c3be.js"></script> <img class="aligncenter size-full wp-image-6286" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b270e68dc472fdb053841f_13-2.webp" 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="https://wordpress.appsilon.com/r-for-programmers/" 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="https://wordpress.appsilon.com/how-i-built-an-interactive-shiny-dashboard-in-2-days-without-any-experience-in-r/" 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="https://wordpress.appsilon.com/shiny-dashboard-ui-crash-course/" 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="https://wordpress.appsilon.com/rapid-internationalization-of-shiny-apps-shiny-i18n-version-0-2/" 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="https://wordpress.appsilon.com/shiny-worker-package/" 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="https://wordpress.appsilon.com/how-to-scale-a-shiny-dashboard/" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">How to scale R Shiny dashboards</span></a></li></ul> <a href="https://appsilon.com/careers/" target="_blank" rel="noopener noreferrer"><img class="aligncenter size-large wp-image-5940" src="https://wordpress.appsilon.com/wp-content/uploads/2020/11/appsilon.hiring0-1024x576.jpg" 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="https://wordpress.appsilon.com/careers/" 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="https://appsilon.com/careers/#r-shiny-developer" target="_blank" rel="noopener noreferrer">R Shiny Developers</a>, <a href="https://appsilon.com/careers/#fullstack-software-engineer-tech-lead" target="_blank" rel="noopener noreferrer">Fullstack Engineers</a>, <a href="https://appsilon.com/careers/#frontend-engineer" target="_blank" rel="noopener noreferrer">Frontend Engineers</a>, a <a href="https://appsilon.com/careers/#senior-infrastructure-engineer" 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="https://wordpress.appsilon.com/careers/#community-manager" 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>