ggplot Shapes in R to Create Scatter Plots

RStudioDataLab
4 min readNov 15, 2023

--

Learn to use ggplot shapes in R to create beautiful and informative scatter plots with different point shapes, colors, sizes, and fills.

Read the Complete Article and download the code:

Explore Our Tutorials and Solved Examples on Our Data Analysis website.

KeyPoints

  • ggplot shapes represent the points in a scatter plot, and they can be used to distinguish different groups of data, highlight outliers, or add more information to the plot.
  • We can use 25 different ggplot shapes, ranging from simple circles and squares to more complex shapes like stars and triangles. We can customize the shapes, colors, sizes, and fill to suit our needs.
  • We can map a variable to the shape of the points using the aes() function, which will create a different point shape for each variable level and add a legend to the plot.
  • We can use geom_jitter() and geom_count() to deal with overplotting, which occurs when there are too many points in the plot that overlap with each other. geom_jitter() adds a small amount of random noise to the x and y coordinates of the points, and geom_count() adds points with sizes proportional to the number of observations at each position.
  • We can use geom_smooth() to add a regression line and a confidence interval to the plot, which can help to show the trend and the uncertainty of the relationship between the variables. Using the level argument, you can use different regression models, such as linear or loess, and control the confidence level.

Before We start, Make sure you read the following:

Setting the Stage: Basic Scatter Plot

Let’s kick off our journey by building a strong foundation. Creating a basic scatter plot involves utilizing the ggplot() function and specifying the aesthetics. Here’s a snippet of code to get you started:

##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

Assigning Specific Point Shapes

Further customization involves assigning specific point shapes to distinct levels of cylinders:

People Love to Read

Adding Regression Lines and Confidence Intervals

Moving towards more advanced analytics, include linear regression lines and confidence intervals:

People Also Read:

Using Loess Models for Smoothing

For a smoother representation of trends, employ loess models with a specified degree of smoothing:

People Also Read:

Labeling Points and Adding Text

To make your scatter plot more informative, consider labeling data points with the names of corresponding cars:

Please find us on Social Media and help us grow

--

--

RStudioDataLab
RStudioDataLab

Written by RStudioDataLab

I am a doctoral scholar, certified data analyst, freelancer, and blogger, offering complimentary tutorials to enrich our scientific community's knowledge.

No responses yet