Data Visualization with R-Scatter plots

Data Visualization with R, In this tutorial, we will describe how to create a scatter plot in the R programming language.

“ggplot2” is a fantastic package for making visually appealing data displays. If you are not already installed, let us do so.

How to Change Background Color in ggplot2 »


Now we can run the “library” method in order to use “ggplot2”.



Do you have any experience with qplot?

qplot will be useful for building a basic scatter plot. Let’s make a plot using the qplot tool.

In R, we may utilize the built-in dataset “mtcars,” which monitors 11 variables for automobiles from 1973 to 1974.

“mpg” represents miles per gallon, while “wt” stands for weight in this data set. Each output point reflects the miles per gallon and weight of a single automobile.

We can now generate a scatter plot using the “qplot” function.

qplot(mpg, wt, data=mtcars)

However, if you require more customization, you must utilize the “ggplot” package.

Draw a trend line using ggplot-Quick Guide »


The ggplot2 commands differ slightly from the qplot function. The “geom point” attribute, which changes the shape of the dots in the graph, is an important function. Different values for the shape will result in different shapes.

For example, if you set the shape parameter to 19, as seen in the code below, you’ll receive solid circles.

ggplot(mtcars, aes(x=mpg, y=wt))+geom_point(shape=19)

With the code above, we can now add a new variable to our scatter plot, in addition to “miles per gallon” and “weight.” The number of cylinders in a vehicle can be used to categorize it, thus we’ll utilize the geometry of each point to indicate this.

Correlation Analysis Different Types of Plots in R »

To produce the scatter plot, we must first create a factor from the cylinder variable and then use the ggplot function.

The code appears to be,

ggplot(mtcars, aes(x=mpg, y=wt, shape=cyl_factor))+geom_point() 

color, as the shape before it, can now be used to represent the third variable.

ggplot(mtcars, aes(x=mpg, y=wt, color=cyl))+geom_point()

Yes, you can see in the output that each point is now colored based on the number of cylinders.

QQ-plots in R: Quantile-Quantile Plots-Quick Start Guide »

Each hue is represented by a legend on the right. The preceding example used a numerical interpretation of the cylinder count, but it makes more sense to think of the cylinder variable as categorical.

So we’ll specify the color parameter once more, but this time we’ll use the “cylfactor” variable.

ggplot(mtcars, aes(x=mpg, y=wt, color=cyl_factor))+geom_point()

The result shows that the legend indicates a distinct color for each cylinder as a category.

Yes, using the factor approach results in a lot more visually appealing picture.


Let us now update the legend’s label.

ggplot(mtcars, aes(x=mpg, y=wt, color=cyl_factor))+geom_point()+labs(color="Cylinders")

Dot Plots in R-Strip Charts for Small Sample Size »

Title & Axis Labels

Using the “xlab” and “ylab” functions, we may rename the axes. We can also define the plot’s title using “ggtitle.”

ggplot(mtcars, aes(x=mpg, y=wt, color=cyl_factor))+geom_point()+labs(color="Cylinders")+xlab("MPG")+ylab("wt")+ggtitle("ScatterPlot")

You can see how these changes might appear on the scatter plot.

Side-by-Side plots with ggplot2 »

You may also like...

2 Responses

  1. Jiří Makovička says:

    How to choose color of points?

    • finnstats says:

      Currently plotted default colors.
      You can set the color manually also.
      # Box plot
      plot + scale_fill_manual(values=c(“#999999”, “#E69F00”, “#56B4E9”))
      # Scatter plot
      plot + scale_color_manual(values=c(“#999999”, “#E69F00”, “#56B4E9”))

      # Box plot
      plot + scale_fill_hue(l=40, c=35)
      # Scatter plot
      plot + scale_color_hue(l=40, c=35)

Leave a Reply

Your email address will not be published. Required fields are marked *

nine − four =