# R散点图示例图解

## 本文概述

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

S.No Parameters Description
1. x 它是数据集, 其值是水平坐标。
2. y 它是数据集, 其值是垂直坐标。
3. main 它是图形的标题。
4. xlab 它是横轴上的标签。
5. ylab 它是纵轴上的标签。
6. xlim x值的极限用于绘图。
7. ylim 是用于绘制的y值的极限。
8. axes 它指示是否应在绘图上绘制两个轴。

### 例子

``````#Fetching two columns from mtcars
data <-mtcars[, c('wt', 'mpg')]
# Giving a name to the chart file.
png(file = "scatterplot.png")
# Plotting the chart for cars with weight between 2.5 to 5 and mileage between 15 and 30.
plot(x = data\$wt, y = data\$mpg, xlab = "Weight", ylab = "Milage", xlim = c(2.5, 5), ylim = c(15, 30), main = "Weight v/sMilage")
# Saving the file.
dev.off()``````

## 使用ggplot2的散点图

ggplot2包提供了用于创建散点图的ggplot()和geom_point()函数。 ggplot()函数采用一系列输入项。第一个参数是输入向量, 第二个参数是aes()函数, 我们在其中添加x轴和y轴。

### 例子

``````#Loading ggplot2 package
library(ggplot2)
# Giving a name to the chart file.
png(file = "scatterplot_ggplot.png")
# Plotting the chart using ggplot() and geom_point() functions.
ggplot(mtcars, aes(x = drat, y = mpg)) +geom_point()
# Saving the file.
dev.off()``````

### 示例1：具有组的散点图

``````#Loading ggplot2 package
library(ggplot2)
# Giving a name to the chart file.
png(file = "scatterplot1.png")
# Plotting the chart using ggplot() and geom_point() functions.
#The aes() function inside the geom_point() function controls the color of the group.
ggplot(mtcars, aes(x = drat, y = mpg)) +
geom_point(aes(color=factor(gear)))
# Saving the file.
dev.off()``````

### 示例2：轴的变化

``````#Loading ggplot2 package
library(ggplot2)
# Giving a name to the chart file.
png(file = "scatterplot2.png")
# Plotting the chart using ggplot() and geom_point() functions.
#The aes() function inside the geom_point() function controls the color of the group.
ggplot(mtcars, aes(x = log(mpg), y = log(drat))) +geom_point(aes(color=factor(gear)))
# Saving the file.
dev.off()``````

### 示例3：具有拟合值的散点图

``````#Loading ggplot2 package
library(ggplot2)
# Giving a name to the chart file.
png(file = "scatterplot3.png")
#Creating scatterplot with fitted values.
# An additional function stst_smooth is used for linear regression.
ggplot(mtcars, aes(x = log(mpg), y = log(drat))) +geom_point(aes(color = factor(gear))) + stat_smooth(method = "lm", col = "#C42126", se = FALSE, size = 1)
#in above example lm is used for linear regression and se stands for standard error.
# Saving the file.
dev.off()``````

## 向图表添加信息

### 示例4：添加标题

``````#Loading ggplot2 package
library(ggplot2)
# Giving a name to the chart file.
png(file = "scatterplot4.png")
#Creating scatterplot with fitted values.
# An additional function stst_smooth is used for linear regression.
new_graph<-ggplot(mtcars, aes(x = log(mpg), y = log(drat))) +geom_point(aes(color = factor(gear))) +
stat_smooth(method = "lm", col = "#C42126", se = FALSE, size = 1)
#in above example lm is used for linear regression and se stands for standard error.
new_graph+
labs(
)
# Saving the file.
dev.off()``````

### 示例5：添加带有动态名称的标题

``````#Loading ggplot2 package
library(ggplot2)
# Giving a name to the chart file.
png(file = "scatterplot5.png")
#Creating scatterplot with fitted values.
# An additional function stst_smooth is used for linear regression.
new_graph<-ggplot(mtcars, aes(x = log(mpg), y = log(drat))) +geom_point(aes(color = factor(gear))) +
stat_smooth(method = "lm", col = "#C42126", se = FALSE, size = 1)
#in above example lm is used for linear regression and se stands for standard error.
#Finding mean of mpg
mean_mpg<- mean(mtcars\$mpg)
new_graph + labs(
)
# Saving the file.
dev.off()``````

### 示例6：添加字幕

``````#Loading ggplot2 package
library(ggplot2)
# Giving a name to the chart file.
png(file = "scatterplot6.png")
#Creating scatterplot with fitted values.
# An additional function stst_smooth is used for linear regression.
new_graph<-ggplot(mtcars, aes(x = log(mpg), y = log(drat))) +geom_point(aes(color = factor(gear))) +
stat_smooth(method = "lm", col = "#C42126", se = FALSE, size = 1)
#in above example lm is used for linear regression and se stands for standard error.
new_graph + labs(
title =
"Relation between Mile per hours and drat", subtitle =
"Relationship break down by gear class", caption = "Authors own computation"
)
# Saving the file.
dev.off()``````

### 示例7：更改x轴和y轴的名称

``````#Loading ggplot2 package
library(ggplot2
# Giving a name to the chart file.
png(file = "scatterplot7.png")
#Creating scatterplot with fitted values.
# An additional function stst_smooth is used for linear regression.
new_graph<-ggplot(mtcars, aes(x = log(mpg), y = log(drat))) +geom_point(aes(color = factor(gear))) +
stat_smooth(method = "lm", col = "#C42126", se = FALSE, size = 1)
#in above example lm is used for linear regression and se stands for standard error.
new_graph + labs(
x = "Drat definition", y = "Mile per hours", color = "Gear", title = "Relation between Mile per hours and drat", subtitle = "Relationship break down by gear class", caption = "Authors own computation"
)
# Saving the file.
dev.off()``````

### 示例8：添加主题

``````#Loading ggplot2 package
library(ggplot2
# Giving a name to the chart file.
png(file = "scatterplot8.png")
#Creating scatterplot with fitted values.
# An additional function stst_smooth is used for linear regression.
new_graph<-ggplot(mtcars, aes(x = log(mpg), y = log(drat))) +geom_point(aes(color = factor(gear))) +
stat_smooth(method = "lm", col = "#C42126", se = FALSE, size = 1)
#in above example lm is used for linear regression and se stands for standard error.
new_graph+
theme_dark() +
labs(
x = "Drat definition, in log", y = "Mile per hours, in log", color = "Gear", title = "Relation between Mile per hours and drat", subtitle = "Relationship break down by gear class", caption = "Authors own computation"
)
# Saving the file.
dev.off()``````

• 回顶