R 简明教程

R - Scatterplots

散点图显示在笛卡尔平面上绘制的许多点。每个点代表两个变量的值。一个变量选择在水平轴中,另一个选择在垂直轴中。

Scatterplots show many points plotted in the Cartesian plane. Each point represents the values of two variables. One variable is chosen in the horizontal axis and another in the vertical axis.

利用 plot() 函数创建简单的散点图。

The simple scatterplot is created using the plot() function.

Syntax

在 R 中创建散点图的基本语法为 -

The basic syntax for creating scatterplot in R is −

plot(x, y, main, xlab, ylab, xlim, ylim, axes)

以下是所用参数的描述 -

Following is the description of the parameters used −

  1. x is the data set whose values are the horizontal coordinates.

  2. y is the data set whose values are the vertical coordinates.

  3. main is the tile of the graph.

  4. xlab is the label in the horizontal axis.

  5. ylab is the label in the vertical axis.

  6. xlim is the limits of the values of x used for plotting.

  7. ylim is the limits of the values of y used for plotting.

  8. axes indicates whether both axes should be drawn on the plot.

Example

我们使用 R 环境中可用的数据集 "mtcars" 来创建一个基本的散点图。我们使用 mtcars 中的列“wt”和“mpg”。

We use the data set "mtcars" available in the R environment to create a basic scatterplot. Let’s use the columns "wt" and "mpg" in mtcars.

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

当我们执行上述代码时,会产生以下结果 -

When we execute the above code, it produces the following result −

                    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

Creating the Scatterplot

下面的脚本将为 wt(重量)和 mpg(每加仑英里数)之间的关系创建一个散点图。

The below script will create a scatterplot graph for the relation between wt(weight) and mpg(miles per gallon).

# Get the input values.
input <- mtcars[,c('wt','mpg')]

# Give the chart file a name.
png(file = "scatterplot.png")

# Plot the chart for cars with weight between 2.5 to 5 and mileage between 15 and 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"
)

# Save the file.
dev.off()

当我们执行上述代码时,会产生以下结果 -

When we execute the above code, it produces the following result −

scatterplot

Scatterplot Matrices

当我们有超过两个变量并且我们想要找到一个变量与其余变量之间的相关性时,我们使用散点图矩阵。我们使用 pairs() 函数创建散点图矩阵。

When we have more than two variables and we want to find the correlation between one variable versus the remaining ones we use scatterplot matrix. We use pairs() function to create matrices of scatterplots.

Syntax

在 R 中创建散点图矩阵的基本语法为 −

The basic syntax for creating scatterplot matrices in R is −

pairs(formula, data)

以下是所用参数的描述 -

Following is the description of the parameters used −

  1. formula represents the series of variables used in pairs.

  2. data represents the data set from which the variables will be taken.

Example

每个变量都与其余变量中的每个变量配对。针对每个对绘制一个散点图。

Each variable is paired up with each of the remaining variable. A scatterplot is plotted for each pair.

# Give the chart file a name.
png(file = "scatterplot_matrices.png")

# Plot the matrices between 4 variables giving 12 plots.

# One variable with 3 others and total 4 variables.

pairs(~wt+mpg+disp+cyl,data = mtcars,
   main = "Scatterplot Matrix")

# Save the file.
dev.off()

当执行上述代码时,我们将获得以下输出。

When the above code is executed we get the following output.

scatterplot matrices