R text combine superscript with round and cor functions - r

How can I integrate this phrase into the text function:
expression(R^2,"=", round(cor(tkw14_cor$TKW, tkw14_cor$Yield),2))
I also tried
expression(R^2 == round(cor(tkw14_cor$TKW, tkw14_cor$Yield),2)))
and
expression(R^2 ~ == ~ round(cor(tkw14_cor$TKW, tkw14_cor$Yield),2)))
and all the 'paste' possibilities..
It does not work like this.
My goal is to have the text print:
R^2 (as superscript) = rounded correlation coefficient.
Thanks,

You can use bquote function.
Random example here:
plot(1)
set.seed(1)
value <- round(cor(rnorm(1000), rnorm(1000)), 2)
text(1,1.2,bquote("R"^"2" ~ " = " ~.(value)))

Related

How to add model name to the regression plot

I'm trying to plot the regression coefficient where I get a plot like this
For example I would like to add the model name into the plot such as this on the top of the image
`PC2 ~ Index + Lane + Gen`
How to do that? I would like to add the model names to the respective plots
My code which I'm using
plot_list = list()
for (i in seq(length(bb))) {
p = ggcoefstats(bb[[i]])
plot_list[[i]] = p
}
pdf("plots1.pdf",height = 10,width = 6)
for (i in seq(length(bb))) {
print(plot_list[[i]])
}
dev.off()
My data bb which is my model output
> bb
$`PC2 ~ Sex + Index + Lane`
Call:
lm(formula = x, data = mrna.pcs)
Coefficients:
(Intercept) SexM IndexAR002 IndexAR003 IndexAR004 IndexAR005 IndexAR006 IndexAR007 IndexAR008 IndexAR009 IndexAR010
0.8055 -11.3695 2.6964 -7.9438 -1.7453 -10.5309 -10.7135 -9.8775 4.4912 0.7830 -4.8674
IndexAR011 IndexAR012 IndexAR013 IndexAR014 IndexAR015 IndexAR016 IndexAR018 IndexAR019 IndexAR020 IndexAR021 IndexAR022
-8.1402 -10.6590 -8.1678 1.0441 -0.4174 7.2952 12.9489 -7.4206 -6.6895 -10.6862 4.9863
IndexAR023 IndexAR025 IndexAR027 Lane2 Lane3 Lane4 Lane5 Lane6 Lane7 Lane8
1.5614 -15.7488 -1.5925 12.3677 -10.3617 -55.5894 25.6420 34.1251 42.4888 16.1013
Any suggestion or help would be really appreciated
I was not previously familiar with this package your using to create this visual, which is not actually ggplot2. According to this page, there is a title parameter that you should use within the function: https://indrajeetpatil.github.io/ggstatsplot/reference/ggcoefstats.html
You might benefit by converting this to ggplot2, however. The first thing I thought when I saw your visual was "this needs ggrepel". That will automatically move your text labels around so that they do not overlap. Seeing those coefficient values would seem to be very important. AFAIK, ggrepel only works with ggplot2 not other graphing libraries.

Pasting value into R plot title

I would like to paste the value of the mean in a plot title using the title() function.
e.g. title("My plot \n mean = mean(x)")
where x is a numeric vector of observations.
I know how to do this using plot(main = " ... ", ). Simply use paste() or substitute(expression()); however, this doesn't seem work with the title() function.
Any ideas?
Answer by #MarcoSandri is correct. The correct way of using the example codes you have written are,
title(main= paste("My plot \n mean =", mean(x)))
and
avg <- mean(x)
title(paste("My Plot \n Mean = ", avg))
See this example where title works correctly:
set.seed(1)
x <- rnorm(30)
txt <- paste("Mean =", round(mean(x),3) )
plot(x)
title(main=txt)

How to write an equation with a variable in legend?

I am trying to write an equation like "R^2=0.00575" in the legend, and the number 0.00575 can be embedded in the legend automatically. Here is an example.
set.seed(100)
x=rnorm(100)
y=1:100
fit=lm(y~x)
R_squared=format(summary(fit)$r.squared,digits = 3)
plot(x,y,type="l")
legend("topleft",legend =expression(R^{2}~"="~R_squared),bty = "n")
As the figure shows, the variable "R_squared" is not embedded in the equation. Is there any solution? Thanks.
For this task I think it is best to do parse(text=sprintf(...)). You can code the R language syntax into the string literal to be parsed into an R expression using parse(), and use sprintf() format specifications to embed any numeric or string values that are stored in variables into the expression.
set.seed(100L);
x <- rnorm(100L);
y <- 1:100;
fit <- lm(y~x);
R_squared <- format(summary(fit)$r.squared,digits=3L);
plot(x,y,type='l');
legend('topleft',legend=parse(text=sprintf('paste(R^2,\' = %s\')',R_squared)),bty='n');
An alternative syntax that leverages the fact that == is plotted as a single equal sign:
legend('topleft',legend=parse(text=sprintf('R^2 == %s',R_squared)),bty='n');
See the plotmath documentation.
You can also use bquote:
set.seed(100L);
x <- rnorm(100L);
y <- 1:100;
fit <- lm(y~x);
R_squared <- format(summary(fit)$r.squared,digits=3L);
plot(x,y,type='l');
legend('topleft',legend=bquote(R^{2} ~ "=" ~ .(R_squared)),bty='n');
More information on partial substitution of expressions with bquote can be found here, which defines the function as:
An analogue of the LISP backquote macro. bquote quotes its argument
except that terms wrapped in .() are evaluated in the specified where
environment.

Using plot with title containing text, formulas and variables

I want to make the title of my plot contain text, formulas and variables. Consider the toy example where I want the title to read as:
Histogram of normal distribution with (mu/sigma) equal to (value of mu/sigma)
(where the first bracket is to be rendered as a formula)
Based on some questions around this site, I tried the following code:
x <- rnorm(1000)
mu <- 1
sigma <- 0
hist(x, main=bquote("Histogram of normal distribution with " *frac(mu,sigma)* " equal to ", .(mu/sigma) ) )
Now the problem is that the value of mu/sigma is not shown, like so:
How can I get the last bit to show?
Here's one way to do it:
title(main=substitute(paste("Histogram of normal distribution with ",
frac(mu,sigma), " equal to ", frac(m,s)),
list(m=mu, s=sigma)))

Superscript R squared for legend

I want to write a R-squared term for my legend but I do not know how. Could someone help me please?
My legend syntax is:
legend(2,10, c("BW (MPE=3%, R-squared=0.77)",
"MY (MPE=5%, R-squared=0.80)", pch=c(2,3))
I would liek to express R-squared as R2 as we normally have in the text.
It will work if you combine bquote and as.expression:
plot(1:10)
legend(2, 10, c(as.expression(bquote("BW (MPE = 3%," ~ R^2 ~ "= 0.77)")),
as.expression(bquote("MY (MPE = 5%," ~ R^2 ~ "= 0.80)"))),
pch=c(2,3))
This is less complex than using c( as.expression ( bquote... multiple times:
plot(1:10)
legend(2, 10, expression("BW (MPE = 3%," ~ R^2 ~ "= 0.77)",
"MY (MPE = 5%," ~ R^2 ~ "= 0.80)"),
pch=c(2,3))
It is useful to understand that the expression function is really a way to make lists of expressions and that commas are therefore reserved as separators for that process. This means you cannot have a "naked" comma in something you want to be inside one of the distinct elements. The commas immediately after the %-signs are protected from parsing by the quotes. This could fully plotmath()-ified with:
plot(1:10)
legend(2, 10, expression(BW * list(MPE == 3*'%',
R^2 == 0.77),
MY * list( MPE == 5*'%',
R^2 == 0.80)
),
pch=c(2,3))
That way the only character needing special attention is the '%'-sign because plotmath() uses that character to delimit items in the list of 'special' math tokens. See ?plotmath for the full list.

Resources