A Scatter Plot is useful to visualize the relationship between any two sets of data. In this article we will show you, How to Create a Scatter Plot, Format its size, shape, color, adding the linear progression, changing theme of a Scatter Plot using ggplot2 in R Programming language with example. For this we are going to use the* diamonds *data set that is provided by the R, and the data inside this dataset is:

**TIP:** ggplot2 package is not installed by default. Please refer Install R Packages article to understand the steps involved in installing a package.

## Create a Scatter Plot using ggplot2 in R

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

**NOTE:** If your requirement is to import data from external files then please refer R Read CSV article to understand the steps involved in csv file import

**R CODE**

# 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))

**OUTPUT**

### Change Color of a Scatter Plot using ggplot2 in R

In this example we will show you, How to change the color of a scatter plot drawn by the ggplot2.

**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 color argument will assign 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.

**R CODE**

# 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"))

**OUTPUT**

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

In this example we will show you, How to change the size and shape of a dot.

**shape:**This argument can help you to change the default dot to any other shape. Or you can assign any column values to this as well, like we did in this example.**size:**This argument may help you change the size of each dot

**R CODE**

# 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"))

**OUTPUT**

### Change Axis of a Scatter Plot using ggplot2 in R

In this example we will show you, How to change the default axis limits drawn by the ggplot.

**scale_x_continuous:**This function can help you to specify the limits for the X-Axis**scale_y_continuous:**This function can help you to specify the limits for the Y-Axis

**R CODE**

# 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"))

**OUTPUT**

### Add labels to Scatter Plot using ggplot2 in R

In this example we will show you, How to add labels for each spot in a scatter plot.

**R CODE**

# 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))

**OUTPUT**

### Alter Legend Position of a Scatter Plot using ggplot2 in R

By default ggplot will position the legend at the right hand side of the scatter plot. In this example we will show you, How to change the legend position from right to top. Remember, You can use * legend.position = “none”* to completely remove the legend.

**R CODE**

# 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"))

**OUTPUT**

## Add Regression Lines

The following examples will show you, How to add new layer called regression lines to the scatter plot region

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

In this example we will show you, How to add the smoothing curve to a scatter plot using * geom_smooth()* function.

**R CODE**

# 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()

**OUTPUT**

### Remove Standard Error from Scatter Plot using ggplot2 in R

By default * geom_smooth()* function will add the standard error to the smoothing curve. In this example we will show you, How to remove that smoothing curve.

**R CODE**

# 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 you can see that, we changed the default color of the scatter plot points, and the smoothing curve.

**OUTPUT**

### Add Multiple regression lines to Scatter Plot using ggplot2 in R

In this example we will show you, How to 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,

*function is plotting regression line for all the different diamond cuts.*

**geom_smooth()****R CODE**

# 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"))

**OUTPUT**

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

In this example we will show you, How to add the linear progression to scatter plot changing the * method* argument default value to

*(linear model).*

**lm****R CODE**

# 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"))

**OUTPUT**

## Changing Theme of a Scatter Plot using ggplot2 in R

In this example we will show you, How to assign name to Scatter plot, and change the default names of X-Axis and Y-Axis using * labs* function

**theme_dark():**We are using this function to change the scatter plot default theme to dark. Typethen R Studio intelligence will show you the list of available options. For example, theme_grey()**theme_****title:**You can provide the Title for your scatter plot.**x:**Please specify the label for the X-Axis**y:**Please specify the label for the Y-Axis

**R CODE**

# 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()

**OUTPUT**

## Adding 2D Density

The following examples will show you, How to add 2D density layer to the scatter plot region

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

The ggplot2 allows us add multiple layers to the plot, and In this example we will show you, How to add the 2D density layer to the scatter plot using the * geom_density_2d()* function

**R CODE**

# 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()

**OUTPUT**

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

**faithful****R CODE**

# 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()

**OUTPUT**

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

In this example we will show you, How to change the defaults colors drawn by the scatter Plot, and the * geom_density_2d()* function using the

**color**argument

**R CODE**

# 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")

**OUTPUT**

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

In this example we will show you, How to change the default lines drawn by geom to points using the * stat_density_2d()* function.

**R CODE**

# 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")

**OUTPUT**

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

In this example we will show you, How to change the default 2D stat density scatter Plot using the * scale_fill_gradient()* function. There are many functions like scale_fill_gradient2 etc so try them to change the look and feel

**R CODE**

# 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")

**OUTPUT**

Thank You for Visiting Our Blog