Jawad
Jawad

Reputation: 27

How to keep abline within boundaries of the corrplot?

I am trying to add an abline to corrplot, but the line stretches out of the boundaries. How can I keep the line within the corrplot axis?

Here is the code:

library(corrplot)
M <- cor(mtcars)
corrplot(M, method = "circle")
abline(v=c(2.5,5.5), col=c("black", "black"), lty=c(2,2), lwd=c(3, 3))

Also how to add x and y labels to the image?

Upvotes: 1

Views: 293

Answers (1)

Karolis Koncevičius
Karolis Koncevičius

Reputation: 9656

abline extends through the whole space. But you can use lines to specify the starts and ends of each line. The centers of each square in corrplot start from (1,1) in the bottom left and increase by 1 with each square. You can use this to get the needed starts and ends for your lines:

corrplot(M, method = "circle")
lines(c(2.5, 2.5), c(0, 12), lwd=3, lty=2)
lines(c(5.5, 5.5), c(0, 12), lwd=3, lty=2)

To add the labels simply use the title function:

title(xlab="x label", ylab="y label")

And here is the result:

image

Upvotes: 1

Related Questions