R :当 X 轴使用对数刻度时,在 Y 轴上绘制一个点 [英] R : Plotting a point on the Y axis when the X axis is using a log scale

查看:46
本文介绍了R :当 X 轴使用对数刻度时,在 Y 轴上绘制一个点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 R 中绘制一个图,我希望在我的 y 轴上的特定点(例如 alpha)处出现一个符号.我的 x 轴使用对数刻度,所以我认为我不能使用 xy 坐标 (0, alpha) 来绘制点(我可能错了).

我尝试过这样的事情:

W.range <- range(W)Z.range <- range(Z1, Z2)# 绘制空图情节(W.range,Z.range,类型=n",日志=x")# 第一条曲线行(W,Z1,类型=l",lty=1,lwd=1.5,col=1)# 第二曲线线(W,Z2,类型=l",lty=2,lwd=1.5,col=2)# 在水平 alpha 的垂直轴上绘制单点(对于某些常量 alpha)点(x=0,y=alpha,pch=1,col=1)

但是,我收到与 x=0 坐标相关的错误.建议?一如既往,我很欣赏这些建议.

解决方案

这应该可行,适当地替换您自己的数据:

# 示例数据和绘图x <- y <- 1:100阿尔法<- 44情节(x,y,日志=x",类型=l")# 添加点点(x = 10^(par(usr")[1]),y = 阿尔法,pch = 16, cex=1.3, col = "red", las = 1,xpd = TRUE) # 控制跨轴的点是否被剪裁

要了解我在这里做了什么,请查看 ?par,我在这里使用它来查询和设置图形参数.

特别是,这里是 par("usr") 的描述,来自上面链接的帮助文件:

<块引用>

‘usr’ 形式为‘c(x1, x2, y1, y2)’的向量给出了极值绘图区域的用户坐标.当一个使用对数刻度(即‘par("xlog")’为真,见下面),那么 x 极限将是10 ^ par("usr")[1:2]".y 轴类似.

usr 返回的 x 坐标是 x 的对数转换值.因此,要在 y 轴上放置一个点,您需要给它一个 x 坐标 使其对数(以 10 为底) 等于 y 轴的位置,以用户表示坐标.par("usr")[1] 获取 x 轴下限的位置: 10^(par("usr")[1] gets您将绘制到 x 轴的那部分的 x 值.

I'm making a plot in R I'd like to have a symbol appear at a specific point (say, alpha) on my y axis. My x axis uses a log-scale so I don't think I can use the xy coordinates (0, alpha) to plot the point (I may be wrong).

I've tried something like this:

W.range <- range(W)
Z.range <- range(Z1, Z2)
# Draw Empty Graph 
plot(W.range, Z.range, type="n", log="x")
# First Curve
lines(W, Z1, type="l", lty=1, lwd=1.5, col=1)
# Second Curve
lines(W, Z2, type="l", lty=2, lwd=1.5, col=2)
# Plot Single Point on vertical axis at level alpha (for some constant alpha)
points(x=0, y=alpha, pch=1, col=1)

However, I get errors relating to the x=0 coordinate. Advice? As always, I appreciate the suggestions.

解决方案

This should work, subbing in your own data as appropriate:

# Example data and plot
x <- y <- 1:100
alpha <- 44
plot(x,y, log="x", type="l")    

# Add point
points(x = 10^(par("usr")[1]), 
       y = alpha, 
       pch = 16, cex=1.3, col = "red", las = 1,
       xpd = TRUE) # controls whether or not pts straddling the axis are clipped

To understand what I did here, have a look at ?par, which I use here both to query and to set graphical parameters.

In particular, here is the description of par("usr"), from the help file linked above:

‘usr’ A vector of the form ‘c(x1, x2, y1, y2)’ giving the extremes
      of the user coordinates of the plotting region.  When a
      logarithmic scale is in use (i.e., ‘par("xlog")’ is true, see
      below), then the x-limits will be ‘10 ^ par("usr")[1:2]’.
      Similarly for the y-axis.

The x-coordinates returned by usr are in terms of the log-transformed values of x. Thus, to get a point placed on the y-axis, you need to give it an x coordinate such that it's log (base 10) is equal to the location of the y-axis, expressed in user coordinates. par("usr")[1] gets you the location of the lower limit of the x-axis: 10^(par("usr")[1] gets you the x-value that will be plotted to just that part of the x-axis.

这篇关于R :当 X 轴使用对数刻度时,在 Y 轴上绘制一个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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