无法使用 ggplot2 和 ggpmisc 获得两位小数的 R2 值
Can't get R2 values in two decimal using ggplot2 and ggpmisc
很抱歉,在这里寻求帮助时,我不知道如何制作可重现的示例。因此,我提供以下代码和数据; <350 KB 这里(数据:https://easyupload.io/1r5xuo),如果你能看看。
我的问题是:我想要 R2 的两位十进制值(如附图所示),我无法使用 ggpmisc 的 stat_fit_glance()即使我使用了 digits=2 功能,但它只显示一个(R2=0.7 和 R2=0.9),可能是因为小数点后一位为零(见图)。
但是,我希望它们是 R2=0.70 和 R2=0.90。
谁能帮我解决这个问题。
library(ggplot2)
library(ggpmisc)
library(dplyr)
library(openxlsx)
library(ggpubr)
# data
data_all <- read.xlsx("./DJFMA_new_files/djfma_hourly_clear_overcast_scatter.xlsx")
colnames(data_all)[1] <- "h"
colnames(data_all)[2] <- "le"
colnames(data_all)[3] <- "ta_ts"
colnames(data_all)[4] <- "q_qRS"
# filter
data_clear <- data_all %>%
filter(type == "Clear-sky")
data_over <- data_all %>%
filter(type == "Overcast")
# Formula
formula1 <- y ~ x
# Plot
ggplot(data=data_all, aes(x=ta_ts, y=h, color=type)) +
geom_hline(yintercept=0, linetype="dashed", color = "grey50", size=.3) +
geom_vline(xintercept=0, linetype="dashed", color = "grey50", size=.3) +
geom_point(alpha=0.3, size=1.5, shape=20) +
labs(x=NULL,
y=expression("H"~ "[W" ~ m^-2~"]")) +
scale_colour_manual("",values = c("Overcast"="#51806a","Clear-sky"="#DD3C51")) +
theme_bw() +
theme(legend.position = c(.25, .85),
legend.background = element_rect(fill = "transparent"),
axis.ticks.length=unit(-0.12, "cm"),
axis.text.y = element_text(margin=margin(5,7,5,5,"pt")),
axis.text.x = element_text(margin=margin(7,5,5,5,"pt"))) +
stat_fit_glance(method = 'lm',
method.args = list(formula = formula1),
#geom = 'text',
label.y = "bottom",
label.x = "right",
aes(label = paste("R^2 ==", round(..r.squared.., digits = 2),
sep = "~")),
parse=TRUE)
样图
我们可以用 format
包裹 round
并使用简单的 paste0
来组合它们。这里要记住的一件事是 format
的输出类型是 character
.
示例:
> format(round(0.70, 2), nsmall = 2)
[1] "0.70"
> typeof(format(round(0.70, 2), nsmall = 2))
[1] "character"
解法(只有最后的情节):
ggplot(data=data_all, aes(x=ta_ts, y=h, color=type)) +
geom_hline(yintercept=0, linetype="dashed", color = "grey50", size=.3) +
geom_vline(xintercept=0, linetype="dashed", color = "grey50", size=.3) +
geom_point(alpha=0.3, size=1.5, shape=20) +
labs(x=NULL,
y=expression("H"~ "[W" ~ m^-2~"]")) +
scale_colour_manual("",values = c("Overcast"="#51806a","Clear-sky"="#DD3C51")) +
theme_bw() +
theme(legend.position = c(.25, .85),
legend.background = element_rect(fill = "transparent"),
axis.ticks.length=unit(-0.12, "cm"),
axis.text.y = element_text(margin=margin(5,7,5,5,"pt")),
axis.text.x = element_text(margin=margin(7,5,5,5,"pt"))) +
stat_fit_glance(method = 'lm',
method.args = list(formula = formula1),
#geom = 'text',
label.y = "bottom",
label.x = "right",
aes(label = paste0("R^2 =", format(round(..r.squared.., digits = 2), nsmall = 2))))
使用stat_poly_eq()
可以轻松调整位数。这是使用来自 CRAN 的 'ggpmisc' 的当前版本。我最近更新了代码,以便不删除尾随零。
library(ggpmisc)
library(dplyr)
library(openxlsx)
# data
data_all <- read.xlsx("./R bits and pieces/djfma_hourly_clear_overcast_scatter.xlsx")
colnames(data_all)[1] <- "h"
colnames(data_all)[2] <- "le"
colnames(data_all)[3] <- "ta_ts"
colnames(data_all)[4] <- "q_qRS"
# filter
data_clear <- data_all %>%
filter(type == "Clear-sky")
data_over <- data_all %>%
filter(type == "Overcast")
# Plot
ggplot(data=data_all, aes(x=ta_ts, y=h, color=type)) +
geom_hline(yintercept=0, linetype="dashed", color = "grey50", size=.3) +
geom_vline(xintercept=0, linetype="dashed", color = "grey50", size=.3) +
geom_point(alpha=0.3, size=1.5, shape=20) +
labs(x=NULL,
y=expression("H"~ "[W" ~ m^-2~"]")) +
scale_colour_manual("",values = c("Overcast"="#51806a","Clear-sky"="#DD3C51")) +
theme_bw() +
theme(legend.position = c(.25, .85),
legend.background = element_rect(fill = "transparent"),
axis.ticks.length=unit(-0.12, "cm"),
axis.text.y = element_text(margin=margin(5,7,5,5,"pt")),
axis.text.x = element_text(margin=margin(7,5,5,5,"pt"))) +
stat_poly_eq(label.y = "bottom",
label.x = "right",
rr.digits = 2)
很抱歉,在这里寻求帮助时,我不知道如何制作可重现的示例。因此,我提供以下代码和数据; <350 KB 这里(数据:https://easyupload.io/1r5xuo),如果你能看看。
我的问题是:我想要 R2 的两位十进制值(如附图所示),我无法使用 ggpmisc 的 stat_fit_glance()即使我使用了 digits=2 功能,但它只显示一个(R2=0.7 和 R2=0.9),可能是因为小数点后一位为零(见图)。
但是,我希望它们是 R2=0.70 和 R2=0.90。
谁能帮我解决这个问题。
library(ggplot2)
library(ggpmisc)
library(dplyr)
library(openxlsx)
library(ggpubr)
# data
data_all <- read.xlsx("./DJFMA_new_files/djfma_hourly_clear_overcast_scatter.xlsx")
colnames(data_all)[1] <- "h"
colnames(data_all)[2] <- "le"
colnames(data_all)[3] <- "ta_ts"
colnames(data_all)[4] <- "q_qRS"
# filter
data_clear <- data_all %>%
filter(type == "Clear-sky")
data_over <- data_all %>%
filter(type == "Overcast")
# Formula
formula1 <- y ~ x
# Plot
ggplot(data=data_all, aes(x=ta_ts, y=h, color=type)) +
geom_hline(yintercept=0, linetype="dashed", color = "grey50", size=.3) +
geom_vline(xintercept=0, linetype="dashed", color = "grey50", size=.3) +
geom_point(alpha=0.3, size=1.5, shape=20) +
labs(x=NULL,
y=expression("H"~ "[W" ~ m^-2~"]")) +
scale_colour_manual("",values = c("Overcast"="#51806a","Clear-sky"="#DD3C51")) +
theme_bw() +
theme(legend.position = c(.25, .85),
legend.background = element_rect(fill = "transparent"),
axis.ticks.length=unit(-0.12, "cm"),
axis.text.y = element_text(margin=margin(5,7,5,5,"pt")),
axis.text.x = element_text(margin=margin(7,5,5,5,"pt"))) +
stat_fit_glance(method = 'lm',
method.args = list(formula = formula1),
#geom = 'text',
label.y = "bottom",
label.x = "right",
aes(label = paste("R^2 ==", round(..r.squared.., digits = 2),
sep = "~")),
parse=TRUE)
样图
我们可以用 format
包裹 round
并使用简单的 paste0
来组合它们。这里要记住的一件事是 format
的输出类型是 character
.
示例:
> format(round(0.70, 2), nsmall = 2)
[1] "0.70"
> typeof(format(round(0.70, 2), nsmall = 2))
[1] "character"
解法(只有最后的情节):
ggplot(data=data_all, aes(x=ta_ts, y=h, color=type)) +
geom_hline(yintercept=0, linetype="dashed", color = "grey50", size=.3) +
geom_vline(xintercept=0, linetype="dashed", color = "grey50", size=.3) +
geom_point(alpha=0.3, size=1.5, shape=20) +
labs(x=NULL,
y=expression("H"~ "[W" ~ m^-2~"]")) +
scale_colour_manual("",values = c("Overcast"="#51806a","Clear-sky"="#DD3C51")) +
theme_bw() +
theme(legend.position = c(.25, .85),
legend.background = element_rect(fill = "transparent"),
axis.ticks.length=unit(-0.12, "cm"),
axis.text.y = element_text(margin=margin(5,7,5,5,"pt")),
axis.text.x = element_text(margin=margin(7,5,5,5,"pt"))) +
stat_fit_glance(method = 'lm',
method.args = list(formula = formula1),
#geom = 'text',
label.y = "bottom",
label.x = "right",
aes(label = paste0("R^2 =", format(round(..r.squared.., digits = 2), nsmall = 2))))
使用stat_poly_eq()
可以轻松调整位数。这是使用来自 CRAN 的 'ggpmisc' 的当前版本。我最近更新了代码,以便不删除尾随零。
library(ggpmisc)
library(dplyr)
library(openxlsx)
# data
data_all <- read.xlsx("./R bits and pieces/djfma_hourly_clear_overcast_scatter.xlsx")
colnames(data_all)[1] <- "h"
colnames(data_all)[2] <- "le"
colnames(data_all)[3] <- "ta_ts"
colnames(data_all)[4] <- "q_qRS"
# filter
data_clear <- data_all %>%
filter(type == "Clear-sky")
data_over <- data_all %>%
filter(type == "Overcast")
# Plot
ggplot(data=data_all, aes(x=ta_ts, y=h, color=type)) +
geom_hline(yintercept=0, linetype="dashed", color = "grey50", size=.3) +
geom_vline(xintercept=0, linetype="dashed", color = "grey50", size=.3) +
geom_point(alpha=0.3, size=1.5, shape=20) +
labs(x=NULL,
y=expression("H"~ "[W" ~ m^-2~"]")) +
scale_colour_manual("",values = c("Overcast"="#51806a","Clear-sky"="#DD3C51")) +
theme_bw() +
theme(legend.position = c(.25, .85),
legend.background = element_rect(fill = "transparent"),
axis.ticks.length=unit(-0.12, "cm"),
axis.text.y = element_text(margin=margin(5,7,5,5,"pt")),
axis.text.x = element_text(margin=margin(7,5,5,5,"pt"))) +
stat_poly_eq(label.y = "bottom",
label.x = "right",
rr.digits = 2)