散点图用于比较变量。当我们需要定义一个变量受另一变量影响多少时,需要对变量进行比较。在散点图中,数据表示为点的集合。散点图上的每个点都定义了两个变量的值。垂直轴选择一个变量,水平轴选择另一个变量。在R中,有两种创建散点图的方法,即使用plot()函数和使用ggplot2包的函数。
在R中创建散点图有以下语法:
plot(x, y, main, xlab, ylab, xlim, ylim, axes)
x,y - 它是值是水平/垂直坐标的数据集。
main - 它是图形的标题。
xlab,ylab - 它是横轴/竖轴上的标签。
xlim,ylim - 是用于绘图的x/y值的极限。
axes - 它指示是否应在绘图上绘制两个轴。
让我们看一个示例,以了解如何使用plot函数构造散点图。在我们的示例中,我们将使用数据集" mtcars",它是R环境中可用的预定义数据集。
#从 mtcars 中获取两列 data <-mtcars[,c('wt','mpg')] # 为图表文件命名。 png(file = "scatterplot.png") # 绘制重量在 2.5 到 5 之间且里程在 15 到 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") # 保存文件。 dev.off()
输出:
在R中,还有另一种创建散点图的方法,即借助ggplot2包。
ggplot2包提供了用于创建散点图的ggplot()和geom_point()函数。 ggplot()函数接受一系列输入项。第一个参数是输入向量,第二个参数是aes()函数,我们在其中添加x轴和y轴。
让我们在一个使用熟悉的数据集" mtcars"的示例的帮助下开始了解如何使用ggplot2软件包。
#加载 ggplot2 包 library(ggplot2) # 为图表文件命名。 png(file = "scatterplot_ggplot.png") # 使用 ggplot() 和 geom_point() 函数绘制图表。 ggplot(mtcars, aes(x = drat, y = mpg)) +geom_point() # 保存文件。 dev.off()
输出:
我们可以添加更多函数,也可以绘制出更具吸引力的散点图。下面是一些添加了不同参数的示例。
#加载 ggplot2 包 library(ggplot2) # 为图表文件命名。 png(file = "scatterplot1.png") # 使用 ggplot() 和 geom_point() 函数绘制图表。 # geom_point() 函数内的 aes() 函数控制组的颜色。 ggplot(mtcars, aes(x = drat, y = mpg)) + geom_point(aes(color=factor(gear))) # 保存文件。 dev.off()
输出:
#加载 ggplot2 包 library(ggplot2) # 为图表文件命名。 png(file = "scatterplot2.png") # 使用 ggplot() 和 geom_point() 函数绘制图表。 # geom_point() 函数内的 aes() 函数控制组的颜色。 ggplot(mtcars, aes(x = log(mpg), y = log(drat))) +geom_point(aes(color=factor(gear))) # 保存文件。 dev.off()
输出:
#加载 ggplot2 包 library(ggplot2) # 为图表文件命名。 png(file = "scatterplot3.png") #使用拟合值创建散点图。 #附加函数 stst_smooth 用于线性回归。 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) #在上面的例子中,lm 用于线性回归,se 代表标准误差。 #保存文件。 dev.off()
输出:
#加载 ggplot2 包 library(ggplot2) #为图表文件命名。 png(file = "scatterplot4.png") #使用拟合值创建散点图。 #附加函数 stst_smooth 用于线性回归。 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) #在上面的例子中,lm 用于线性回归,se 代表标准误差。 new_graph+ labs( title = "Scatterplot with more information" ) # 保存文件。 dev.off()
输出:
#加载 ggplot2 包 library(ggplot2) # 为图表文件命名。 png(file = "scatterplot5.png") #使用拟合值创建散点图。 #附加函数 stst_smooth 用于线性回归。 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) #在上面的例子中,lm 用于线性回归,se 代表标准误差。 #寻找 mpg 的平均值 mean_mpg<- mean(mtcars$mpg) #添加带有动态名称的标题 new_graph + labs( title = paste("Adding additiona information. Average mpg is", mean_mpg) ) # 保存文件。 dev.off()
输出:
#加载 ggplot2 包 library(ggplot2) #为图表文件命名。 png(file = "scatterplot6.png") #使用拟合值创建散点图。 #附加函数 stst_smooth 用于线性回归。 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) #在上面的例子中,lm 用于线性回归,se 代表标准误差。 #添加带有动态名称的标题 new_graph + labs( title = "Relation between Mile per hours and drat", subtitle = "Relationship break down by gear class", caption = "Authors own computation" ) # 保存文件。 dev.off()
输出:
#加载 ggplot2 包 library(ggplot2 #为图表文件命名。 png(file = "scatterplot7.png") #使用拟合值创建散点图。 #附加函数 stst_smooth 用于线性回归。 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) #在上面的例子中,lm 用于线性回归,se 代表标准误差。 #添加带有动态名称的标题 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" ) #保存文件。 dev.off()
输出:
#加载 ggplot2 包 library(ggplot2 #为图表文件命名。 png(file = "scatterplot8.png") #使用拟合值创建散点图。 #附加函数 stst_smooth 用于线性回归。 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) #在上面的例子中,lm 用于线性回归,se 代表标准误差。 #添加带有动态名称的标题 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" ) #保存文件。 dev.off()
输出:
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)