散点图和拟合线条
当有两个连续变量(数值变量)时,可以通过作一个散点图(Scatter plot)来观察两个变量间的关系。
载入R包。
library(ggplot2)
画出一个基本的散点图。
ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length)) +
geom_point()

简单修饰散点图。
ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length)) +
geom_point(colour = "lightsalmon", stroke = 2) +
theme_classic() +
labs(x = "",
y ="")
在散点图上再拟合一条直线。
ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length)) +
geom_point(colour = "lightsalmon", stroke = 2) +
geom_smooth(method = "lm", se = T) +
theme_classic() +
labs(x = "",
y ="")
2023-02-21 16:28