散点图和拟合线条

当有两个连续变量(数值变量)时,可以通过作一个散点图(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 ="")


欢迎关注公众号:R语言和统计
▌课程相关咨询可添加R师妹微信: kefu_rstats
▌邮箱:contact@rstats.cn
▌网站:www.rstats.cn
我们致力于让R语言和统计变得简单!



2023-02-21 16:28
首页    ggplot2    散点图和拟合线条