English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

R Plot Scatter Plot

Scatter plots display all data as points in a Cartesian coordinate system to show the degree of mutual influence between variables, with the position of each point determined by the value of the variable, with each point corresponding to a X and Y axis point coordinate.

Scatter plots can be drawn using the plot() function, with the syntax format as follows:

plot(x, y, type="p", main, xlab, ylab, xlim, ylim, axes)
  • x The data set for the x-axis coordinate.

  • y The data set for the y-axis coordinate.

  • type: The type of plotting, p for point, l for line, o for drawing both points and lines with the line passing through the points.

  • main The title of the chart.

  • xlab, ylab The label names of x-axis and y-axis.

  • xlim, ylim The range of x-axis and y-axis.

  • axes Boolean value, whether to draw two x-axes.

The可选值 for type parameter:

  • p: Point chart

  • l: Line chart

  • b: Draw both points and lines

  • c: Draw only the line shown by parameter b

  • o: Draw both points and lines, and the line passes through the points

  • h: Draw vertical lines from points to the horizontal axis

  • s: Step chart, horizontal first then vertical

  • S: Step chart, vertical first then horizontal

  • n: Empty graph

Create a simple line chart:

x<-c(10,40)
y<-c(20,60)
# Generate png image
png(file = "runnob-test-plot2.png")
plot(x, y, "l")

Create a simple line chart, using the o parameter for type, drawing both points and lines, and the line passing through the points:

x<-c(10,40)
y<-c(20,60)
# Generate png image
png(file = "runnob-test-.plot.png")
plot(x, y, "o")

Next, we use the built-in R language dataset mtcars for testing.

We use the wt and mpg columns from the mtcars dataset:

input <- mtcars[,c('wt','mpg')]
print(head(input))

The output result is:

wt mpg
Mazda RX4         2.620 21.0
Mazda RX4 Wag     2.875 21.0
Datsun 710        2.320 22.8
Hornet 4 Drive    3.215 21.4
Hornet Sportabout 3.440 18.7
Valiant           3.460 18.1

Next, we use the above data to create a scatter plot

:

# Data
input <- mtcars[,c('wt','mpg')]
# Generate png image
png(file = "scatterplot.png")
# Set coordinate x-axis range 2.5 to 5, y-axis range 15 to 30.
plot(x = input$wt, y = input$mpg,
   xlab = "Weight",
   ylab = "Milage",
   xlim = c(2.5,5,
   ylim = c(15,30),              
   main = "Weight vs Milage"
)

Scatter plot matrix

Scatter plot matrices are created using the plotting method of scatter plots of two variables. It can be considered as a large graphic square matrix, where each non-diagonal element is the scatter plot of the variable corresponding to the row and the variable corresponding to the column. The main diagonal elements are the names of each variable. In this way, scatter plot matrices can clearly show the pairwise correlation between the multiple variables being studied.

Scatter plot matrices are scatter plots of each numerical variable in the dataset paired with each other.

R language uses the following functions to create scatter plot matrices:

pairs(formula, data)

Parameters:

  • formula Variable Series

  • data Dataset of variables

# Output the image
png(file = "scatterplot_matrices.png")
# 4 a variable drawn as a matrix,12 a plot 
pairs(~wt+mpg+disp+cyl, data = mtcars, main = "Scatterplot Matrix")