R散点图示例图解

本文概述

  • 使用ggplot2的散点图
  • 向图表添加信息
散点图用于比较变量。当我们需要定义一个变量受另一变量影响的程度时, 需要对变量进行比较。在散点图中, 数据表示为点的集合。散点图上的每个点都定义了两个变量的值。垂直轴选择一个变量, 水平轴选择另一个变量。在R中, 有两种创建散点图的方法, 即使用plot()函数和使用ggplot2包的函数。
在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 它指示是否应在绘图上绘制两个轴。
让我们看一个示例, 以了解如何使用plot函数构造散点图。在我们的示例中, 我们将使用数据集” mtcars” , 它是R环境中可用的预定义数据集。
例子
#Fetching two columns from mtcarsdata < -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()

输出
R散点图示例图解

文章图片
使用ggplot2的散点图 在R中, 还有另一种创建散点图的方法, 即借助ggplot2包。
【R散点图示例图解】ggplot2包提供了用于创建散点图的ggplot()和geom_point()函数。 ggplot()函数采用一系列输入项。第一个参数是输入向量, 第二个参数是aes()函数, 我们在其中添加x轴和y轴。
让我们在一个使用熟悉的数据集” mtcars” 的示例的帮助下开始了解如何使用ggplot2软件包。
例子
#Loading ggplot2 packagelibrary(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()

输出
R散点图示例图解

文章图片
我们可以添加更多功能, 也可以绘制出更具吸引力的散点图。下面是一些示例, 其中添加了不同的参数。
示例1:具有组的散点图
#Loading ggplot2 packagelibrary(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()

输出
R散点图示例图解

文章图片
示例2:轴的变化
#Loading ggplot2 packagelibrary(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()

输出
R散点图示例图解

文章图片
示例3:具有拟合值的散点图
#Loading ggplot2 packagelibrary(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()

输出
R散点图示例图解

文章图片
向图表添加信息 示例4:添加标题
#Loading ggplot2 packagelibrary(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(title = "Scatterplot with more information")# Saving the file.dev.off()

输出
R散点图示例图解

文章图片
示例5:添加带有动态名称的标题
#Loading ggplot2 packagelibrary(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 mpgmean_mpg< - mean(mtcars$mpg)#Adding title with dynamic namenew_graph + labs(title = paste("Adding additiona information. Average mpg is", mean_mpg))# Saving the file.dev.off()

输出
R散点图示例图解

文章图片
示例6:添加字幕
#Loading ggplot2 packagelibrary(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.#Adding title with dynamic namenew_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()

输出
R散点图示例图解

文章图片
示例7:更改x轴和y轴的名称
#Loading ggplot2 packagelibrary(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.#Adding title with dynamic namenew_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()

输出
R散点图示例图解

文章图片
示例8:添加主题
#Loading ggplot2 packagelibrary(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.#Adding title with dynamic namenew_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()

输出
R散点图示例图解

文章图片

    推荐阅读