用ggplot2画一个圆圈 [英] Draw a circle with ggplot2

查看:57
本文介绍了用ggplot2画一个圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这是一个愚蠢的问题,但我在 ggplot2 的手册中也找不到答案,也用阿姨"谷歌...

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))
}

以及它的使用演示:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆