drawing circle in R
You need to specify asp = 1
:
x = runif(100, -1, 1)
y = runif(100, -1, 1)
plot(x, y, asp = 1, xlim = c(-1, 1))
draw.circle(0, 0, 1, nv = 1000, border = NULL, col = NA, lty = 1, lwd = 1)
EDIT: Just a side note, you can make your Monte Carlo function more efficient:
mc.pi = function(n) {
x = runif(n, -1, 1)
y = runif(n, -1, 1)
pin = sum(ifelse(sqrt(x^2 + y^2 <= 1), 1, 0))
4 * pin/n
}
Drawing a series of circles in R
This is basically @Peter's answer but with modifications. Your approach was fine but there is no radius=
argument in DrawCircle
. See the manual page ?DrawCircle
for the arguments:
dev.new(width=12, height=4)
Canvas(xlim = c(0,50), ylim=c(2, 6), asp=1, xpd=TRUE)
DrawCircle(x=plotdat$xcords, y=plotdat$ycords, r.out = 2)
But your example has axes:
plot(NA, xlim = c(0,50), ylim=c(2, 6), xlab="", ylab="", yaxt="n", asp=1, xpd=TRUE)
DrawCircle(x=plotdat$xcords, y=plotdat$ycords, r.out = 2)
Drawing circles in R
As @Baptiste says above, you can use plot(...,asp=1)
. This will only work if your x and y ranges happen to be the same, though (because it sets the physical aspect ratio of your plot to 1). Otherwise, you probably want to use the eqscplot
function from the MASS
package. A similar issue arises whenever you try to do careful plots of geometric objects, e.g. Drawing non-intersecting circles
This plot is produced by substituting MASS::eqscplot
for plot
in your code above:
Note that depending on the details of what R thinks about your monitor configuration etc., the circle may look a bit squashed (even though it goes through the points) when you plot in R's graphics window -- it did for me -- but should look OK in the graphical output.
How to draw multiple centric circles in R
draw.circle
seems to accept a vector of inputs, so use whatever function is convenient to make your sequence of inputs and pass that to the function.
plot(0,0,type = "n", xlim = c(-10,10), ylim = c(-10,10))
draw.circle (0,0,seq(0,4,.5))
How to draw a half circle on a plot?
Do you mean this?
x <- seq(0, pi, length.out = 500)
W <- 3
plot(cos(x) * W, sin(x) * W, type = "l")
plotting a circle inside a square in R
You need to specify asp=1
require(plotrix)
require(grid)
plot(c(-1, 1), c(-1,1), type = "n", asp=1)
rect( -.5, -.5, .5, .5)
draw.circle( 0, 0, .5 )
See also: Drawing non-intersecting circles
This one got me too!
Draw circle with ggplot2 using coordinates
library(ggplot2)
library(ggforce)
dat = read.table(text=" x y sizes
0.95285914 0.06596914 0.8868900
-1.59822942 0.71052036 2.3087498
0.39216559 0.58428603 0.1921204
0.16559318 -0.99303562 1.1586288
-0.43047728 -0.96649463 0.5360174
-0.73746484 -0.21143717 0.5260277
0.58779207 0.08073626 0.5070558
0.74936811 0.54462816 0.2047399
-0.01587290 -0.14835109 0.1324782
-0.06573365 0.33317857 0.3989122", header=TRUE)
ggplot(dat, aes(x0=x, y0=y, r=sizes)) +
geom_circle() + coord_equal() + theme_classic()
How to draw circles around polygon/spider chart, without plotting libraries
I don't know of any functions in base R that do circles for you, but you can concoct them manually.
center <- c(x=2.1, y=2.1) # probably a better way
half <- seq(0, pi, length.out = 51)
for (D in d1) {
Xs <- D * cos(half); Ys <- D * sin(half)
lines(center["x"] + Xs, center["y"] + Ys, col = "gray", xpd = NA)
lines(center["x"] + Xs, center["y"] - Ys, col = "gray", xpd = NA)
}
Notes:
- I don't know off-hand how the center-point should be calculated, I chose that point using
locator(1)
; not being familiar withstars
, there may be a better way to determine this programmatically and more accurately; - The first
lines(.)
draws the upper semi-circle; the second draws the lower. - The
xpd=NA
is to preclude clipping due to the drawing margin. It may not be necessary in your "real" data. See?par
for more details on this. - Though it may be difficult to detect here, the gray circles are drawn on top of the stars plot, which might be an aesthetic compromise. The only way around that is to plot the circles first. To do this, draw the first semicircle first with
plot(..., type="l")
and then add the remainder as expected, and only then runstars(..., add=TRUE)
.
How to draw circles inside each other with ggplot2?
The code you have with coord_polar()
is correct, just the plot limits need adjusting to see both the circles, e.g.
ggplot(data) +
geom_line(aes(x1,y1)) +
geom_line(aes(x1,y2)) +
coord_polar() + ylim(c(0,NA))
The reason for using ylim
is that this is the direction getting transformed to the radius by the coord_polar()
Related Topics
Combine Multiple PDF Plots into One File
How to Calculate Total Least Squares in R? (Orthogonal Regression)
Count Common Words in Two Strings
Convert Table into Matrix by Column Names
Colors Lost in Legend When Using Scale_Shape_Manual
How to Fill Histogram with Color Gradient
How to Convert Unix Timestamp (Milliseconds) and Timezone in R
Create a New Variable Based on the First 7 Characters of Existing Variable
Using Grep to Subset Rows from a Data.Table, Comparing Row Content
Overlapping the Predicted Time Series on the Original Series in R
Update Subset of Values in a Dataframe Column
How to Figure Third Friday of a Month in R
Display Duplicate Records in Data.Frame and Omit Single Ones
Rolling Join Grouped by a Second Variable in Data.Table
How to Manage a Table/Matrix to Obtain Information Using Conditions
Rstudio Calls Source() When Saving Script