Draw ggplot2 Plot with Lines and Points in R (Example)

 

In this article you’ll learn how to draw a ggplot2 plot with points and lines in the R programming language.

The tutorial contains the following content:

Let’s start right away!

 

Constructing Exemplifying Data

We’ll use the data below as basement for this R tutorial:

data <- data.frame(x = 1:5,        # Create example data
                   y = c(1, 7, 4, 1, 2))
data                               # Print example data
#   x y
# 1 1 1
# 2 2 7
# 3 3 4
# 4 4 1
# 5 5 2

The previous RStudio console output reveals the structure of the example data frame – Our data has five rows and two numeric columns.

Let’s draw these data!

 

Example: Drawing ggplot2 Plot with Lines & Points

The following syntax illustrates how to create a ggplot2 scatterplot with lines. First, we need to install and load the ggplot2 package:

install.packages("ggplot2")        # Install & load ggplot2 package
library("ggplot2")

Now, we can use the geom_line & geom_point functions to draw a ggplot2 graph with lines and points:

ggplot(data, aes(x, y)) +          # Draw ggplot2 plot
  geom_line() +
  geom_point()

 

r graph figure 1 draw ggplot2 lines and points r

 

As shown in Figure 1, we created a line and point plot (i.e. a graph where the lines connect the points) using the ggplot2 package with the previously shown R syntax.

 

Video, Further Resources & Summary

Have a look at the following video on my YouTube channel. In the video, I illustrate the R programming code of this tutorial in a programming session in the R programming language.

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Besides that, you may want to have a look at the other articles of this website. A selection of tutorials about ggplot2 graphics can be found below:

 

To summarize: In this tutorial, I illustrated how to draw lines and points to the same ggplot2 plot in the R programming language. In case you have additional questions and/or comments, let me know in the comments section.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top