在R中画出半圈 [英] Plotting half circles in R
问题描述
我试图用R绘制半个圆圈。我最终的目标是绘制一个圆圈,中间用颜色分开。我找到的唯一方法是绘制两个不同颜色的半圆。
所以我创建了自己的函数:
pre > upper.half.circle < - function(x,y,r,nsteps = 100,...){
rs < - seq(0,pi,len = nsteps)
xc < - x + r * cos(rs)
yc < - y + r * sin(rs)
多边形(xc,yc,...)
}
lower.half.circle < - function(x,y,r,nsteps = 100,...){
rs < - seq(0,pi,len = (rs)
yc < - yr * sin(rs)
多边形(xc,yc,...)
} $
xc < - xr * cos b $ b
但是,由于某些原因,我的半圆最终更像是半椭圆。例如,尝试运行:
plot(1,type =n,axes = F,xlab =,ylab =,xlim = c(0,200),ylim = c(0,200))
upper.half.circle(15,170,10,nsteps = 1000,col ='red')
有人知道我为什么遇到这种麻烦,或者知道更好的方法来做我想做的事情吗?
Thanks!
问题是默认的长宽比不是1:1。 b
为了解决这个问题,请在 plot
中设置 asp = 1
:
受此的启发
I'm trying to plot half circles using R. My final aim is to draw a circle, divided in the middle by color. The only way I have found yet is to draw two half-circles with different colors.
So I have created my own functions:upper.half.circle <- function(x,y,r,nsteps=100,...){ rs <- seq(0,pi,len=nsteps) xc <- x+r*cos(rs) yc <- y+r*sin(rs) polygon(xc,yc,...) } lower.half.circle <- function(x,y,r,nsteps=100,...){ rs <- seq(0,pi,len=nsteps) xc <- x-r*cos(rs) yc <- y-r*sin(rs) polygon(xc,yc,...) }
However, for some reason my half-circles end up more like half-ellipses. For example, try running:
plot(1, type="n",axes=F,xlab="", ylab="",xlim=c(0,200),ylim=c(0,200)) upper.half.circle(15,170,10,nsteps=1000,col='red')
Does anyone know why I'm having this trouble, or alternatively, knows of a better way to do what I want?
Thanks!解决方案The problem is the default aspect ratio is not 1:1.
To fix this, set
asp=1
inplot
:Inspired by this Q & A. You could have sniffed out this was the case by turning on the axes and x/y labels.
这篇关于在R中画出半圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!