I am sorry that I don't know how to make reproducible example while asking for help here. Therefore, I am providing the code below and data; <350 KB here (data: https://easyupload.io/1r5xuo), if you can look at.
My problem is: I want to have two decimal value of R2 (shown in attached figure), which I can't get using ggpmisc's stat_fit_glance() function even if I have used digits=2, but it shows only one (R2=0.7 and R2=0.9), could be because the values after 1 decimal is zero (see in figure).
But, I want them as R2=0.70 and R2=0.90.
Can anyone help me to solve this issue.
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)
Using stat_poly_eq()
it is possible to easily adjust the number of digits. This is using the current version of 'ggpmisc' from CRAN. I updated the code recently so that trailing zeros are not dropped.
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)