# How to seperate regression eqation and R sqare value in two different line?

Hi,
I have created a plot with regression equation ansd R2 square value with the following data and codes:

Average of As Average of P
342.6887442 3146.338707
294.0990009 4078.622709
342.5575215 2817.677112
383.823409 3484.552344
301.772448 3862.843974
440.7775607 3408.683938
412.1475502 3697.561471
459.9299341 2992.494928
299.2736006 3929.504011
322.9774859 3289.192968
492.9749533 2741.138114
562.0608856 2396.736431
408.5571033 3224.471533
407.043385 3341.253078
451.4992111 3659.334532
523.9603681 3143.806
350.6685461 3337.42596
356.4404658 3165.389528
366.303439 3509.087612
410.171813 3480.002509
342.2522307 2727.603886
219.3114962 4191.392309
293.0386077 3737.109768
308.8191644 3663.484616
367.4614531 3502.876818
374.4156344 3462.328755
375.8402695 3338.155716
325.0661626 3373.088852
265.100086 3437.084432
369.3491764 3180.972658
558.813142 2783.304062
414.6535678 3170.003679
325.0043337 3409.056318
431.9034133 3355.668521
304.7024603 3772.156399
450.5858859 3092.547493
346.5755461 3714.374521
302.008048 3910.785176
377.0109416 3000.960041
366.2166368 3645.377007
338.9748026 3218.408448
259.7520629 4080.917932
308.8896932 4167.083522
496.0897122 4222.41253
319.2983607 3904.806111
369.9230149 3772.156399

library(ggplot2)
library(dplyr)
library(ggpmisc)
library(tidyverse)
library(scales)

R

formula <- y ~ x

B<-ggplot(R,aes(x=As,y=P))+
geom_smooth(method=lm, formula = formula, fill = "#542788")+
geom_point(size = 1)+
scale_y_continuous(breaks = seq(0,6000, 1500), limits = c(0,6000))+
scale_x_continuous(breaks = seq(0.20,0.60,0.20), limits = c(0.20,0.60),
labels = label_number(accuracy = 0.1))+
labs(y=expression(P~(mg~kg^-1)),
x=expression(Total~As~(mg~kg^-1)))+
stat_poly_line(color = "#542788")+
stat_poly_eq(aes(label = paste(after_stat(eq.label),
after_stat(rr.label), sep = "", "")),
label.x = 'centre', label.y = 6000, size = 2)+
stat_fit_glance(method = 'lm',
method.args = list(formula = formula),
geom = 'text',
aes(label = paste("P = ", ifelse(after_stat(p.value) <0.001, "<0.001",
round(after_stat(p.value), digits = 3)))),
label.x = 0.394, label.y =5000,
size = 2)+
theme_classic()+
theme(axis.text.x = element_text(color = "black",size = 8))+
theme(axis.text.y = element_text(color = "black",size = 8))+
theme(axis.title.x = element_blank())+
theme(axis.title.y = element_text(color = "black",size = 8))
B

Here, the R square value is seperated by comma. But I want in a seperate line. I have tried with sep = '\n'. But it didn't work. Please give a solution.

Will something like this work for you?

``````library(ggplot2)
library(ggpmisc)
#>
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#>
#>     annotate
R <- data.frame(As = 1:4, P = c(1.1,1.8,2.5,4.3))
ggplot(R,aes(x=As,y=P))+
geom_smooth(method=lm, formula = y~x, fill = "#542788")+
stat_poly_line(color = "#542788")+
stat_poly_eq(use_label(c("eq")), formula = y~x,label.y.npc = 0.8)+
stat_poly_eq(use_label(c("R2")), formula = y~x, label.y.npc = 0.7)
``````

Created on 2023-12-12 with reprex v2.0.2

Dear FJCC,
Thank you very much. It worked.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.