2

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)

Sample Figure enter image description here

2 Answers2

2

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)

The plot created

Pedro J. Aphalo
  • 5,796
  • 1
  • 22
  • 23
1

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))))

Output

Shibaprasadb
  • 1,307
  • 1
  • 7
  • 22
  • Hi Shibaprasadb, Thank you for the quick solution to my problem. It perfectly gives two decimal digits. But I am unable to superscript the 2 on top of R. It remains same as it is in your given plot. Do you have any solution to make that R2 in superscript? That would be the final output I wanted. Thank you. – Arindan Mandal Sep 27 '21 at 08:55
  • @ArindanMandal One can make this work by embedding the formatted number as a character string inside the label to be parsed. This is how I have recently updated `stat_poly_eq()` to work. Please, see my answer. – Pedro J. Aphalo Sep 30 '21 at 00:49
  • Hi Pedro, Thanks a lot for the update and solving this. Now the stat_poly_eq() is super cool with very limited codes. Much appreciated. Thank you. – Arindan Mandal Sep 30 '21 at 10:36
  • 2
    @ArindanMandal You are welcome. Could you please accept the answer as good if you think it is good? It is useful to others to easily find the answer that solves the question (and also for my reputation as author of the answer). – Pedro J. Aphalo Sep 30 '21 at 13:20
  • Hi Pedro, ops, I forgot to accept your answer. But now I did it. Thanks for the help. – Arindan Mandal Oct 02 '21 at 05:55