[This article was first published on Steve's Data Tips and Tricks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
< section id="introduction" class="level1">
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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:
< section id="examples" class="level1">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:
x <- c(1, 2, 3, 4, 5) y <- c(2, 4, 6, 8, 10) plot(x, y)
- 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) coords <- list(c(2, 4), c(3, 6), c(4, 8)) radii <- c(0.1, 0.2, 0.3) for (i in 1:length(coords)) { symbols( coords[[i]][1], coords[[i]][2], circles = radii[[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:
x <- c(1, 2, 3, 4, 5) y <- c(2, 4, 6, 8, 10) plot( x, y, main = "Scatter Plot with Circles", xlab = "X Values", ylab = "Y Values" ) coords <- list(c(2, 4), c(3, 6), c(4, 8)) radii <- c(0.1, 0.2, 0.3) for (i in 1:length(coords)) { symbols( coords[[i]][1], coords[[i]][2], circles = radii[[i]], add = TRUE, bg = "red", fg = "blue", inches = FALSE ) }
Here is one last exmple:
# Create a scatter plot with multiple circles n <- 10 x <- runif(n, -2, 2) y <- runif(n, -2, 2) size <- runif(n, 0.1, 1) fill <- sample(colors(), n) border <- sample(colors(), n) 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.
To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.