R:ggplot2拟合,我推荐geom_smooth绘制拟合和ggpmisc添加统计信息。
几行代码就可以搞定了,对新手非常友好。
线性拟合
library(tidyverse)
library(readxl)
library(ggplot2)
library(ggpmisc)
repeat1_rawgrassland <-
read_excel("D:/OneDrive/Untitled Folder/R/repeat1-rawgrassland.xlsx")
colnames(repeat1_rawgrassland)[5] <- 'EE'
# 线性拟合
repeat1_rawgrassland %>%
ggplot(aes(x=latitude, y=EE)) +
geom_point(color='#4a86e8', size=5) +
geom_smooth(method = 'lm') +
stat_fit_deviations(formula = y ~ x, color = "skyblue") +
stat_poly_eq(use_label(c("eq", "adj.R2", "p.value.label")),
formula = y ~ x, parse = TRUE,
size = 5, #公式字体大小
label.x = 0.05, #位置 ,0-1之间的比例
label.y = 0.95)
# 自行查看拟合
model <- lm(EE~latitude,data=repeat1_rawgrassland)
values <- summary(model)
这是最简单的拟合方法,当然这张图还有很多优化的空间,如颜色、刻度等等。现在对代码进行说明:
-
sta_fit_deviations用于添加点与拟合线的连接直线,如不需要,可删去
-
stat_poly_eq第一个参数是所需的统计量,可以选择需要的值
-
label.xy都是相对于轴的大小,是百分比,不再是刻度值了
多项式拟合
# 多项式拟合
repeat1_rawgrassland %>%
ggplot(aes(x=latitude, y=EE)) +
geom_point(color='#4a86e8', size=5) +
geom_smooth(method = 'lm', formula = y ~ poly(x,3)) +
stat_fit_deviations(formula = y ~ poly(x,3), color = "skyblue") +
stat_poly_eq(use_label(c("eq", "adj.R2", "p.value.label")),
formula = y ~ poly(x,3), parse = TRUE,
size = 5, #公式字体大小
label.x = 0.05, #位置 ,0-1之间的比例
label.y = 0.95)
# 自行查看拟合
model <- lm(EE~poly(latitude, 3),data=repeat1_rawgrassland)
values <- summary(model)
好了,我们已经明白了拟合的基本原理了,下面来看一张完备的图吧
library(ggplot2)
library(tidyverse)
theme_set(theme_bw()) # pre-set the bw theme.
data("midwest", package = "ggplot2")
# midwest <- read.csv("http://goo.gl/G1K41K") # bkup data source
# Scatterplot
ggplot(midwest, aes(x=area, y=poptotal)) +
geom_point(aes(col=state, size=popdensity)) +
geom_smooth(method="loess", se=F) +
xlim(c(0, 0.1)) +
ylim(c(0, 500000)) +
labs(subtitle="Area Vs Population",
y="Population",
x="Area",
title="Scatterplot",
caption = "Source: midwest")
references:
https://github.com/cran/ggpmisc