How to make concentric circles in r plot
I need to make concentric circles, preferably offset from the source, in the function plot()
to serve as a visual reference for a function. I tried abline()
, but did not succeed.
2 answers
Use the draw.circle
command from the plotrix
package:
x <- seq(-3, 3, by=0.01)
y <- x^2
plot(y ~ x, asp=1, type="l")
library(plotrix)
draw.circle(1, 1, 1, border="red")
draw.circle(1, 1, 2, border="green")
draw.circle(1, 1, 3, border="blue")
The syntax of the draw.circle
Command is quite simple:
-
The first argument is the coordinate of the center of the circle on the X axis
-
The second argument is the coordinate of the center of the circle on the Y axis
-
The third argument is the value of the radius of the circle
I changed their border with the argument border
just to emphasize the difference between the circles designed. By default, the edge of them is black.
Building on Marcus Nunes ' excellent answer, you could also use the symbols()
function of the graphics
package.
Just remember to use add=TRUE
(for it to add the circle to a pre-existing plot) and inches = FALSE
, so that the natural scale of the radius of the circle, reported in circles
, is that of the axis x
.
x <- seq(-3, 3, by=0.01)
y <- x^2
plot(y ~ x, asp=1, type="l")
symbols(1, 1, circles=1, inches=FALSE, fg="red", add=TRUE)
symbols(1, 1, 2, inches=FALSE, fg="green", add=TRUE) # circles é o 3º arg
symbols(1, 1, 3, inches=FALSE, fg="blue", add=TRUE)
Abs!