如何在R中的对数-对数图中绘制圆? [英] How to draw a circle in a log-log plot in R?

查看:165
本文介绍了如何在R中的对数-对数图中绘制圆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个对数轴的图。我想在情节的某个位置添加一个圆圈。我尝试使用 plotrix ,但这没有提供对数半径选项。

I have a plot with two logarithmic axes. I'd like to add a circle to a certain position of the plot. I tried to use plotrix, but this does not give options for "log-radius".

# data to plot
x = 10^(-1 * c(5:0))
y = x ^-1.5

#install.packages("plotrix", dependencies=T)
# use require() within functions
library("plotrix")

plot (x, y, log="xy", type="o")
draw.circle(x=1e-2, y=1e2, radius=1e1, col=2)

如何在我的对数图中添加一个圆?

How can I add a circle to my log-log plot?

推荐答案

作为krlmlr建议,最简单的解决方案是稍微修改 plotrix :: draw.circle()。对数-对数坐标系使线性比例中给定的圆的坐标变形;为了解决这个问题,您只需要对计算出的坐标取幂,就像我在下面的代码中用 ##<-标记的行中所做的那样:

As krlmlr suggests, the easiest solution is to slightly modify plotrix::draw.circle(). The log-log coordinate system distorts coordinates of a circle given in the linear scale; to counteract that, you just need to exponentiate the calculated coordinates, as I've done in the lines marked with ## <- in the code below:

library("plotrix")

draw.circle.loglog <- 
function (x, y, radius, nv = 100, border = NULL, col = NA, lty = 1,
    lwd = 1)
{
    xylim <- par("usr")
    plotdim <- par("pin")
    ymult <- (xylim[4] - xylim[3])/(xylim[2] - xylim[1]) * plotdim[1]/plotdim[2]
    angle.inc <- 2 * pi/nv
    angles <- seq(0, 2 * pi - angle.inc, by = angle.inc)
    if (length(col) < length(radius))
        col <- rep(col, length.out = length(radius))
    for (circle in 1:length(radius)) {
        xv <- exp(cos(angles) * log(radius[circle])) * x[circle]         ## <-
        yv <- exp(sin(angles) * ymult * log(radius[circle])) * y[circle] ## <-
        polygon(xv, yv, border = border, col = col[circle], lty = lty,
            lwd = lwd)
    }
    invisible(list(x = xv, y = yv))
}

# Try it out 
x = 10^(-1 * c(5:0))
y = x ^-1.5

plot (x, y, log="xy", type="o")
draw.circle.loglog(x = c(1e-2, 1e-3, 1e-4), y = c(1e2, 1e6, 1e2),
                   radius = c(2,4,8), col = 1:3)

这篇关于如何在R中的对数-对数图中绘制圆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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