R 中的 car::scatter3d - 更好地标记轴

发布于 2024-12-17 14:47:49 字数 63 浏览 1 评论 0原文

我正在使用 scatter3d,3 个轴只有两个端点值。如何在整个轴上获得标签,就像普通的plot()函数一样?

I'm using scatter3d and the 3 axes just have two endpoint values. how can I get labels throughout the entire axis, just like the normal plot() function does?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇纵 2024-12-24 14:47:49

那好吧。我把它当作一个挑战。

显然您需要:

require(rgl)
require(car)
require(mgcv) # for the example

复制 car:::scatter3d.default 代码并将其粘贴回来,并将其分配给 scatter3d.default

scatter3d.default 代码的早期添加这些行:

 showLabels3d <- car:::showLabels3d  
 nice <- car:::nice  
  # since you will be losing their connection to the unexposed fns in car

然后在第二个 if(axis.scales){ ...} 后面的代码块中,替换以下代码:

 if (axis.scales) {
   x.labels <-  seq(lab.min.x, lab.max.x, 
                       by=diff(range(lab.min.x, lab.max.x))/4)
   x.at <- seq(min.x, max.x, by=nice(diff(range(min.x, max.x))/4))
      rgl.texts(x.at, -0.05, 0, x.labels, col = axis.col[1])

   z.labels <-  seq(lab.min.z, lab.max.z, 
                       by=diff(range(lab.min.z, lab.max.z))/4)
   z.at <- seq(min.z, max.z, by=diff(range(min.z, max.z))/4)
      rgl.texts(0, -0.1, z.at, z.labels, col = axis.col[3])

   y.labels <-  seq(lab.min.y, lab.max.y, 
                       by=diff(range(lab.min.y, lab.max.y))/4)
   y.at <- seq(min.y, max.y, by=diff(range(min.y, max.y))/4)
      rgl.texts(-0.05, y.at, -0.05, y.labels, col = axis.col[2])
                }

(您可能需要替换 scatter3d.formula 的代码,以便不会在 car NAMESPACE 中查找例行分派的 scatter 方法。我只是简单地更换了scatter3dcar:::scatter3d.formula 中调用“scatter3d.default”,这样解释器会首先查看新定义的函数。)

编辑:比 mucking 更好的方法使用 scatter3d.formula 的方法是使用以下代码将 car 命名空间/环境分配给新函数:

environment(scatter3d.default) <- environment(car:::scatter3d.formula)

然后如果您这样做:

scatter3d(prestige ~ income + education, data=Duncan)

您会得到这个(使用屏幕截图程序拍摄)
在此处输入图像描述

Oh well. I took it as a challenge.

Obviously you need to:

require(rgl)
require(car)
require(mgcv) # for the example

Copy the car:::scatter3d.default code and paste it back, assigning it to scatter3d.default.

Add these lines early in the code for scatter3d.default:

 showLabels3d <- car:::showLabels3d  
 nice <- car:::nice  
  # since you will be losing their connection to the unexposed fns in car

Then in the code block following the second if(axis.scales){ ...}, substitute this code:

 if (axis.scales) {
   x.labels <-  seq(lab.min.x, lab.max.x, 
                       by=diff(range(lab.min.x, lab.max.x))/4)
   x.at <- seq(min.x, max.x, by=nice(diff(range(min.x, max.x))/4))
      rgl.texts(x.at, -0.05, 0, x.labels, col = axis.col[1])

   z.labels <-  seq(lab.min.z, lab.max.z, 
                       by=diff(range(lab.min.z, lab.max.z))/4)
   z.at <- seq(min.z, max.z, by=diff(range(min.z, max.z))/4)
      rgl.texts(0, -0.1, z.at, z.labels, col = axis.col[3])

   y.labels <-  seq(lab.min.y, lab.max.y, 
                       by=diff(range(lab.min.y, lab.max.y))/4)
   y.at <- seq(min.y, max.y, by=diff(range(min.y, max.y))/4)
      rgl.texts(-0.05, y.at, -0.05, y.labels, col = axis.col[2])
                }

(You may need to replace the code for scatter3d.formula so that doesn't look in the car NAMESPACE for the routinely dispatched scatter method. I simply replaced the scatter3d call inside car:::scatter3d.formula with "scatter3d.default" so the interpreter would first look at the newly defined function.)

Edit: a better method than mucking with scatter3d.formula is to assign the car namespace/environment to the new function with this code:

environment(scatter3d.default) <- environment(car:::scatter3d.formula)

Then if you do this:

scatter3d(prestige ~ income + education, data=Duncan)

You get this (taken with a screenshot program)
enter image description here

岛徒 2024-12-24 14:47:49

在 scatter3d 的帮助下,有一个新选项可以添加内部轴标签:

轴.ticks

如果为 TRUE,则打印内部轴“tick”标签;默认值为 FALSE。 (此选项的代码由 David Winsemius 提供。)

There is a new option that adds interior axis labels, from the help of scatter3d:

axis.ticks

if TRUE, print interior axis-“tick” labels; the default is FALSE. (The code for this option was provided by David Winsemius.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文