<- c(1, 2, 3, 4, 5)
x <- c(2, 4, 6, 8, 10)
y plot(x, y)
Introduction
As an R programmer, you may want to draw circles in plots to highlight certain data points or to create visualizations. Here are some simple steps to draw circles in plots using R:
Examples
- First, create a scatter plot using the
plot()
function in R. For example, you can create a scatter plot ofx
andy
values using the following code:
- To draw a circle on the plot, you can use the
symbols()
function in R. Thesymbols()
function allows you to draw various shapes, including circles, squares, triangles, and more. To draw a circle, set thecircles
argument toTRUE
. For example, to draw a circle with a radius of 0.5 at the point (3, 6), use the following code:
plot(x, y)
symbols(3, 6, circles = 1, add = TRUE)
- You can also customize the color and border of the circle using the
bg
andfg
arguments. For example, to draw a red circle with a blue border, use the following code:
plot(x, y)
symbols(3, 6, circles = 1, add = TRUE, bg = "red", fg = "blue")
- To draw multiple circles on the plot, you can use a loop to iterate over a list of coordinates and radii. For example, to draw three circles with different radii at different points, use the following code:
plot(x, y)
<- list(c(2, 4), c(3, 6), c(4, 8))
coords <- c(0.1, 0.2, 0.3)
radii
for (i in 1:length(coords)) {
symbols(
1], coords[[i]][2], circles = radii[[i]],
coords[[i]][add = TRUE, bg = "red", fg = "blue", inches = FALSE
) }
- Finally, you can add a title and axis labels to the plot using the
title()
,xlab()
, andylab()
functions. For example, to add a title and axis labels to the plot, use the following code:
<- c(1, 2, 3, 4, 5)
x <- c(2, 4, 6, 8, 10)
y plot(
main = "Scatter Plot with Circles",
x, y, xlab = "X Values", ylab = "Y Values"
)
<- list(c(2, 4), c(3, 6), c(4, 8))
coords <- c(0.1, 0.2, 0.3)
radii
for (i in 1:length(coords)) {
symbols(
1], coords[[i]][2], circles = radii[[i]],
coords[[i]][add = TRUE, bg = "red", fg = "blue", inches = FALSE
) }
Here is one last exmple:
# Create a scatter plot with multiple circles
<- 10
n <- runif(n, -2, 2)
x <- runif(n, -2, 2)
y <- runif(n, 0.1, 1)
size <- sample(colors(), n)
fill <- sample(colors(), n)
border
symbols(x, y, circles = size, inches = FALSE, add = F, bg = fill, fg = border)
Conclusion
Overall, drawing circles in plots is a simple and effective way to highlight certain data points or to create visualizations. Try experimenting with different coordinates, radii, colors, and borders to create your own custom plots.