user3413262 - 3 months ago 15

R Question

I am able to plot a map and caption a specific point:

`library(maps)`

map("state")

text(-80.83,35.19,"Charlotte",cex=.6)

I can also plot a circle centered around that point:

`symbols(-80.83,35.19,circles=2, add=TRUE)`

However, I would like to control the size of the circle. In particular, I want to draw a circle with a radius of 100 mile around multiple locations contained in a data.frame, matrix, or list.

Answer

You can write a function to customize how you want the circle to look. For example:

```
plotCircle <- function(x, y, r) {
angles <- seq(0,2*pi,length.out=360)
lines(r*cos(angles)+x,r*sin(angles)+y)
}
```

Then if you had a set of coordinates in a data frame:

```
coords <- data.frame(x = c(-1,0,1), y = c(-1, 0.5, 1))
```

You can start with some initial plot (map, or empty plot, etc)

```
plot(1,type='n',xlim=c(-2,2),ylim=c(-2,2))
```

Then call the plotting function over your list of coordinates:

```
apply(coords,1,function(df) plotCircle(df[1],df[2],.3))
```

Source (Stackoverflow)

Comments