用ggplot2绘制一个圆 [英] Draw a circle with ggplot2
本文介绍了用ggplot2绘制一个圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
也许这是一个愚蠢的问题,但我无法在ggplot2手册中找到答案,也无法在auntgoogle中找到答案...
Maybe it is a silly question, but I couldn't find the answer in the handbook of ggplot2 nor with "aunt" google...
我如何如果我有一个中间点和一个直径,用ggplot2作为附加图层绘制一个圆圈?
感谢您的帮助。
How do I plot a circle with ggplot2 as an additional layer if I have a middle point and a diameter? Thanks for your help.
推荐答案
更新,更好的选项利用名为 ggforce ,它定义了一个显式 geom_circle
。
A newer, better option leverages an extension package called ggforce that defines an explicity geom_circle
.
但为了后人的缘故,这里有一个简单的循环函数:
But for posterity's sake, here's a simple circle function:
circleFun <- function(center = c(0,0),diameter = 1, npoints = 100){
r = diameter / 2
tt <- seq(0,2*pi,length.out = npoints)
xx <- center[1] + r * cos(tt)
yy <- center[2] + r * sin(tt)
return(data.frame(x = xx, y = yy))
}
并演示它的用法:
And a demonstration of it's use:
dat <- circleFun(c(1,-1),2.3,npoints = 100)
#geom_path will do open circles, geom_polygon will do filled circles
ggplot(dat,aes(x,y)) + geom_path()
这篇关于用ggplot2绘制一个圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文