How to add margins and text to the chart (Plot)?

Asked

Viewed 1,063 times

2

I would like to know how to remove lines from the chart’s margins on the chart itself R (ex: image 1) and add the correlation value of the modelled curve with the coefficient of determination (R²).

inserir a descrição da imagem aqui

library(drc)

S.alba.m1 <- drm(DryMatter~Dose, Herbicide, data = S.alba, fct = LL.4())

plot(S.alba.m1)
cor( fitted(S.alba.m1), S.alba$DryMatter^2)
[1] 0.9292864

1 answer

6


By default, the R considers that his plot creates four axes:

  • axis 1 (x-axis equivalent, with defined scale)

  • axis 2 (equivalent to y-axis, with defined scale)

  • axis 3 (x-axis parallel, no defined cant)

  • axis 4 (y-axis equivalent, no defined cant)

In a traditional graph, these axes can be disconnected by argument axes=FALSE:

x <- seq(0, 2*pi, 0.01)
plot(sin(x) ~ x, axes=FALSE, type="l")

inserir a descrição da imagem aqui

It is easy to add the missing axes using the function axis, in the definition I put above:

axis(1)
axis(2)

inserir a descrição da imagem aqui

But note that the axes are not connected, as one would expect in a traditional graph. One way to get them to connect is through function box:

box(bty="L")

inserir a descrição da imagem aqui

The problem is that the function plot.rcd, even with the argument axes=FALSE, draws the graph box:

S.alba.m1 <- drm(DryMatter~Dose, Herbicide, data = S.alba, fct = LL.4())
plot(S.alba.m1, axes=FALSE)

inserir a descrição da imagem aqui

One way to avoid this is to draw the graph above, without the axes, but with the box. On top of the black box, draw a white box, with width equal to three. Thus, the original black box will have been "erased" (actually, it was just overwritten). After this, just add the axes and the box joining them:

box(col="white", lwd=3)
axis(1)
axis(2)
box(bty="L")

inserir a descrição da imagem aqui

Finally, the function of text puts any text on top of a graph. Just place the x and y coordinates from where the text should appear. In the case below, I put the text at x=5 and y=1 (which does not mean, under any circumstances, that this is the best place to put this information):

text(5, 1, labels=paste("R²=", "0.9293", sep=""))

inserir a descrição da imagem aqui

Observing: the question was answered as asked, but the statistic calculated by cor(fitted(S.alba.m1), S.alba$DryMatter^2) is not r 2 of the adjusted curve. This calculated value is only the correlation between the adjusted values and the square of DryMatter. Iasto has nothing to do with the coefficient of determination, which must always be calculated from the likelihood functions (except in the case of simple linear regression, which can be calculated as the square of a sample correlation). There are many different ways to calculate the R 2 in a logistic regression and use this formula is not one of them.

  • But once thank you Marcus, I had not realized that the function I was used serves only for simple linear regression, I will look for a way to calculate the coefficient of determination for the logistic models through the software R.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.