I've looked through some other threads and tried using expression
and bquote
but without any luck. I'm a beginner with R and would appreciate help with this specific example. I've found the following code on another website and it is working for me, but I can't make the 2 in R2 a superscript.
This is the initial code:
ggplotRegression <- function (fit) {
require(ggplot2)
ggplot(fit$model, aes_string(x = names(fit$model)[2], y = names(fit$model)[1])) +
geom_point() +
stat_smooth(method = "lm") +
labs(title = paste("R2 = ",signif(summary(fit)$r.squared, 5),
"Intercept =",signif(fit$coef[[1]],5 ),
" Slope =",signif(fit$coef[[2]], 5),
" P =",signif(summary(fit)$coef[2,4], 5)))
}
ggplotRegression(lm(TOA_NDVI ~ Field_NDVI, data = NDVI_type))
This is one of the things I've tried:
ggplotRegression <- function (fit) {
require(ggplot2)
ggplot(fit$model, aes_string(x = names(fit$model)[2], y = names(fit$model)[1])) +
geom_point() +
stat_smooth(method = "lm") +
labs(title = paste(expression("R^2 *="),signif(summary(fit)$r.squared, 5)),
"Intercept =",signif(fit$coef[[1]],5 ),
" Slope =",signif(fit$coef[[2]], 5),
" P =",signif(summary(fit)$coef[2,4], 5)))
}
ggplotRegression(lm(TOA_NDVI ~ Field_NDVI, data = NDVI_type))
Any help would be very much appreciated. Thank you.
You can use "bquote". Here is the code:
ggplotRegression <- function (fit) {
require(ggplot2)
ggplot(fit$model, aes_string(x = names(fit$model)[2], y = names(fit$model)[1])) +
geom_point() +
stat_smooth(method = "lm") +
labs(title = bquote(R^2== .(signif(summary(fit)$r.squared, 5))~
"Intercept = "~ .(signif(fit$coef[[1]],5 )) ~
" Slope = "~ .(signif(fit$coef[[2]], 5)) ~
" P = "~ .(signif(summary(fit)$coef[2,4], 5)) ))
}
ggplotRegression(lm(speed ~ dist, data = cars))