lily23 - 1 year ago 68

R Question

I need to include a text caption on a graph in R. I basically want to include a regression coefficient (R^2) - which I have done with the code below

`text(-8, 2, bquote(R^2 == .(round((summary(fit1)$r.squared),2))))`

But now I would like a few changes. I would like the R^2 to be in italics, and to add other text information.

I could make the R^2 italics like this

`text(-8, 2, expression(italic(R^2)))`

But is there a way to do this using bquote() so I can include the regression coefficient as well?

A second question would be how can I also include other text in the same caption.

For example I have also tried this code to plot some text before the R^2 - but here the maths doesn't work at all

`text(-9, 2, paste("Linear Regression \n R^2" , round((summary(fit1)$r.squared),2)))`

Any help would be appreciated.

Thanks.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

We can just wrap it with `italic`

```
text(-8, 2, bquote(italic(R^2) == .(round((summary(fit1)$r.squared),2))))
```

and to add some strings

```
text(-8, 2, bquote('Linear Regression'~italic(R^2) == .(round((summary(fit1)$r.squared),2))))
```

Using a reproducible example

```
set.seed(425)
x <- sample(10, 10, replace=TRUE)
y <- sample(20, 10, replace=TRUE)
fit1 <- lm(x~y)
plot(x, y, xlim = c(0,10), ylim = c(0,20))
text(8, 2, bquote('Linear Regression'~italic(R^2) == .(round((summary(fit1)$r.squared),2))))
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**