R ggplot2 Scatter Plot

A R ggplot2 Scatter Plot is useful to visualize the relationship between any two sets of data. Let us see how to Create a Scatter Plot, Format its size, shape, color, adding the linear progression, changing the theme of a Scatter Plot using ggplot2 in R Programming language with an example.

For this r ggplot scatter plot demonstration, we are going to use the diamonds data set that is provided by the R Programming, and the data inside this dataset is:

Create a Scatter Plot using ggplot2 in R 1

Create a Scatter Plot using ggplot2 in R

In this example, we show you the different ways to create a scatter Plot using the R ggplot2 package. And we use the above-shown diamonds data set, which is provided by the R Studio.

TIP: R ggplot2 package not installed by default to draw this scatter plot. Please refer Install R Packages article to install the R Programming package.

# Create Scatter Plot using ggplot2 in R
# Importing the ggplot2 library
library(ggplot2)

# Default way to draw Scatter Plot
ggplot(data = diamonds, aes(x = carat, y = price)) + geom_point()

# Approach 2 - to draw Scatter plot
ggplot(diamonds, aes(x = carat, y = price)) + geom_point()

# Approach 3
ggplot(diamonds) + geom_point(aes(x = carat, y = price))

# Fourth Approach to plot scatter plot
ggplot() + geom_point(data = diamonds, aes(x = carat, y = price))
Create a Scatter Plot using ggplot2 in R 2

Change Color of a Scatter Plot using ggplot2 in R

In this example, we change the color of a scatter plot drawn by the ggplot in R.

  • color: Please specify the color you want to use for your Scatter plot. For example “red”, “blue”, “green” etc. In this example, we are using the values present in the cut column as color. You can try changing it to any other column.
  • scale_color_manual: By default, the color argument assigns some default colors, and to change this, we can use this function. From the below code snippet, you can observe that we are assigning some random colors to each diamond cut.

NOTE: If you require to import data from external files, then please refer R Read CSV article to understand importing the CSV file.

# Changing Colors of Scatter Plot using ggplot2 in R
library(ggplot2)
ggplot() + 
        geom_point(data = diamonds, 
                   aes(x = carat, 
                       y = price, 
                       color = cut)) +
        scale_color_manual(values = c("orchid", "chocolate4", 
                                      "goldenrod2", "tomato2", "midnightblue"))
Create a Scatter Plot using ggplot2 in R 3

Change Shape & Size of a Scatter Plot using ggplot2 in R

In this example, we change the size and shape of a dot in the R ggplot scatter plot.

  • shape: This argument can help you to change the default dot to any other shape. Or assign any column values to this as well, as we did in this example.
  • size: It helps to change the size of each dot.
# Changing Shapes of the Scatter Plot using ggplot2 in R
library(ggplot2)
ggplot(diamonds)  + 
  geom_point(aes(x = carat, 
                 y = price, 
                 color = clarity, 
                 shape = cut)) + 
  scale_shape_manual(values = c(1, 4, 9, 7, 5)) +
  scale_color_manual(values = c("orchid", "chocolate4", "goldenrod2", 
                                "pink3", "tomato2", "midnightblue",
                                "khaki4" ,"seagreen"))
Create a Scatter Plot using ggplot2 in R 4

Change Axis of a Scatter Plot using ggplot2 in R

In this R example, we show how to change the default axis limits drawn by the ggplot scatter plot.

  • scale_x_continuous: This function can help to specify the limits for the X-Axis
  • scale_y_continuous: It helps to specify the limits for the Y-Axis
# Changing X-Axis, Y-Axis of a Scatter Plot using ggplot2 in R

library(ggplot2)
ggplot(diamonds)  + 
  geom_point(aes(x = carat, y = price, color = clarity, shape = cut)) + 
  scale_x_continuous(limits = c(0, 7)) +
  scale_y_continuous(limits = c(0, 25000)) +  
  scale_color_manual(values = c("orchid", "chocolate4", "goldenrod2", 
                                "pink3", "tomato2", "midnightblue",
                                "khaki4" ,"seagreen"))
Create a Scatter Plot using ggplot2 in R 5

Add labels to Scatter Plot using ggplot2 in R

In this example, we add labels for each spot in an R ggplot scatter plot.

# Add labels to Scatter Plot using ggplot2 in R

library(ggplot2)
ggplot(diamonds)  + 
  geom_point(aes(x = carat, 
                 y = price, 
                 color = clarity, 
                 shape = cut)
             ) + 
  geom_text(diamonds, 
            mapping = aes(x = carat, y = price), 
            label = rownames(diamonds))
Create a Scatter Plot using ggplot2 in R 6

Alter Legend Position of a Scatter Plot using ggplot2 in R

By default, ggplot position the legend at the right hand side of the scatter plot. Here, we change the legend position from right to top. Remember, You can use a legend.position = “none” to completely remove the legend.

# Changing Legend of a Scatter Plot using ggplot2 in R

library(ggplot2)
ggplot(diamonds)  + 
  geom_point(aes(x = carat, y = price, color = cut)) + 
  theme(legend.position = "top") +
  scale_color_manual(values = c("midnightblue", "chocolat4", "goldenrod2",
                                "orchid", "tomato2"))
Create a Scatter Plot using ggplot2 in R 7

Add Regression Lines

The following examples show how to add a new layer called regression lines to the to scatter plot region.

Add Smoothed Curve to Scatter Plot using ggplot2 in R

In this example, we show how to add the smoothing curve to a scatter plot in R using geom_smooth() function.

# Add Smoothed Curve to a Scatter Plot using ggplot2 in R

library(ggplot2)
ggplot(diamonds, aes(x = carat, y = price))  + 
  geom_point(color = "midnightblue") + 
  geom_smooth()
Create a Scatter Plot using ggplot2 in R 8

Remove Standard Error from Scatter Plot using ggplot in R

By default, the geom_smooth() function add the standard error to the smoothing curve. Let us remove that smoothing curve from the R ggplot scatter plot.

# Add Smoothed Curve to a Scatter Plot using ggplot2 in R

library(ggplot2)
ggplot(diamonds, aes(x = carat, y = price))  + 
  geom_point(color = "midnightblue") + 
  geom_smooth(se = FALSE, color = "goldenrod2")

From the above code snippet, see that we changed the default color of the scatter plot points and the smoothing curve.

Create a Scatter Plot using ggplot2 in R 9

Add Multiple regression lines to Scatter Plot using ggplot2 in R

In this example, we add the multiple regression lines to scatter plot using method argument. Here, we haven’t done much; we just added the color argument. It means the geom_smooth() function is plotting the regression line for all the different diamond cuts.

# Add Multiple Smoothing Curve to a Scatter Plot using ggplot2 in R

library(ggplot2)
ggplot(diamonds, aes(x = carat, y = price, color = cut))  + 
  geom_point() + 
  geom_smooth(method = "auto", se = FALSE) +
  scale_color_manual(values = c("orchid", "chocolate4", 
                                "goldenrod2", "tomato2", "midnightblue"))
Create a Scatter Plot using ggplot2 in R 10

Add Linear Model to a Scatter Plot using ggplot2 in R

Here, we add the linear progression to the scatter plot changing the method argument default value to lm (linear model).

# Adding Linear Model to a Scatter Plot using ggplot2 in R

library(ggplot2)
ggplot(diamonds, aes(x = carat, y = price, color = cut))  + 
  geom_point() + 
  geom_smooth(method = "lm", se = FALSE) +
  scale_color_manual(values = c("orchid", "chocolate4", 
                                "goldenrod2", "tomato2", "midnightblue"))
Create a Scatter Plot using ggplot2 in R 11

Changing Theme of a Scatter Plot using ggplot2 in R

Let us assign a name to Scatter plot, and change the default names of X-Axis and Y-Axis using labs function.

  • theme_dark(): Use this function to change the scatter plot default theme to dark. Type theme_ then R Studio intelligence shows the available options. For example, theme_grey().
  • title: Provide the Title for your scatter plot.
  • x: Please specify the label for the X-Axis.
  • y: Specify the label for the Y-Axis.
# Changing Theme of a Scatter Plot using ggplot2 in R

library(ggplot2)
ggplot(diamonds, aes(x = carat, y = price, color = cut))  + 
  geom_point() + 
  geom_smooth(method = "auto", se = FALSE) +
  scale_color_manual(values = c("orchid", "chocolate4", 
                                "goldenrod2", "tomato2", "midnightblue")) +
  labs(title = "Customized Scatter Plot for Diamonds", 
       x = "Diamond Weight", y = "Price in US Dollar") +
  theme_dark()
Create a Scatter Plot using ggplot2 in R 16

Adding 2D Density

The following examples show how to add the 2D density layer to the scatter plot region.

Adding 2D Density to a Scatter Plot using ggplot2 in R

The ggplot2 allows us to add multiple layers to the plot. In this example, we add the 2D density layer to the scatter plot using the geom_density_2d() function.

# Adding Linear Model to a Scatter Plot using ggplot2 in R

library(ggplot2)
ggplot(diamonds, aes(x = carat, y = price))  + 
  geom_point() + 
  geom_density_2d()
Create a Scatter Plot using ggplot2 in R 12

I think the diamonds data set is too large to show the 2D density. Let me change the data set to faithful

# Adding 2D Density to a Scatter Plot using ggplot2 in R

library(ggplot2)
ggplot(faithful, aes(x = eruptions, y = waiting))  + 
  geom_point() + 
  geom_density_2d()
Create a Scatter Plot using ggplot2 in R 13

Changing Colors of a 2D Density Scatter Plot using ggplot in R

Let us change the default colors drawn by the scatter Plot in R ggplot package, and the geom_density_2d() function using the color argument.

# Change Colors - 2D Density to a Scatter Plot using ggplot2 in R

library(ggplot2)
ggplot(faithful, aes(x = eruptions, y = waiting))  + 
  geom_point(color = "midnightblue") + 
  geom_density_2d(colour = "chocolate")
Create a Scatter Plot using ggplot2 in R 17

Add 2D Stat Density to a Scatter Plot using ggplot2 in R

This example shows how to change the R ggplot scatter plot default lines drawn by geom to points using the stat_density_2d() function.

# Changing Geom - 2D Density to a Scatter Plot using ggplot2 in R

library(ggplot2)
ggplot(faithful, aes(x = eruptions, y = waiting))  + 
  geom_point(color = "midnightblue") + 
  stat_density_2d(geom = "point")
Create a Scatter Plot using ggplot2 in R 14

Changing Colors of a 2D Stat Density Scatter Plot using ggplot in R

Let us change the default 2D stat density scatter Plot using the scale_fill_gradient() function in R ggplot2. There are many functions like scale_fill_gradient2, etc., so try them to change the look and feel.

# Changing Geom - 2D Density to a Scatter Plot using ggplot2 in R

library(ggplot2)
ggplot(faithful, aes(x = eruptions, y = waiting))  + 
  geom_point(color = "midnightblue") + 
  stat_density_2d(geom = "polygon",  aes(fill = ..level..)) + 
  scale_fill_gradient(low =  "midnightblue", high = "chocolate")
Create a Scatter Plot using ggplot2 in R 15