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:
CodePudding user response:
We can wrap the round
with format
and use simple paste0
to combine them. One thing to keep in mind here is the type of output from format
is character
.
Example:
> format(round(0.70, 2), nsmall = 2)
[1] "0.70"
> typeof(format(round(0.70, 2), nsmall = 2))
[1] "character"
Solution (only the final 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 = paste0("R^2 =", format(round(..r.squared.., digits = 2), nsmall = 2))))
CodePudding user response:
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)