覆盖 plot.factor 的技巧? [英] Tricks to override plot.factor?

查看:57
本文介绍了覆盖 plot.factor 的技巧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 plot 已经有一段时间了,我想知道如果类型参数可以覆盖分类预测变量会破坏多少.现在,当 x 变量是一个因子时, plot 总是尝试绘制箱线图.似乎 plot.factor 方法总是被调用.如果 type 可以覆盖该功能,并且我不必将 x 轴设为数值,将其抑制,然后再添加它,那就太好了.我对包装函数不感兴趣,因为我刚刚描述了如何做到这一点.我只是想知道是否有我遗漏的 plot 参数可以解决这个问题.

I've been using plot for quite awhile now and I'm wondering how much would be broken if the type argument could override categorical predictors. Right now plot always attempts boxplots when the x variable is a factor. It seems that the plot.factor method always gets called then. It would be nice if type could override that feature and I didn't have to make the x-axis numerical, suppress it, and then add it later. I'm not interested in a wrapper function since I've pretty much just described how to do that. I'm just wondering if there's a plot argument I've missed that can solve this.

例如,在这种情况下通常会尝试使用箱线图...

For example, typically a boxplot is attempted in this case...

y <- 1:4
x <- factor(letters[1:4])
plot(y ~ x)

我更喜欢它只绘制点并正确标记 x 轴.以下工作,但我希望有一个更简单的版本.

I'd prefer it to just plot the points and label the x-axes correctly. The following works but I was hoping for a simpler version.

nx <- length(x)
plot(y ~ 1:nx, xaxt = 'n')
axis(1, 1:nx, x, xlab = '')

我希望像下面这样的东西可以工作......

I was hoping something like the following might work...

plot(y ~ x, type = 'n')
points(1:nx, y)

但是不行.而 type = 'p' 也不会这样做.

but no go. And type = 'p' doesn't do it either.

我相信最后一次失败是 R 中不一致的另一个例子.设置 type = 'n' 也应该在方法调用中起作用,或者应该强制所有 plot 函数使用它(继承或至少通过 ... 传递).它在 plot.default 中.

I believe this last failure is yet another example of inconsistency in R. Setting type = 'n' should be working in method calls as well or it should be mandatory that all plot functions use it (inherited or at least passed through ...). It's in plot.default.

推荐答案

你可以直接调用 plot.default:

You could just call plot.default:

y<-rnorm(100)
x<-factor(sample(c("a","b","c"),size=100,replace=TRUE))
plot.default(x,y)

将因子级别更改为字符,因此很明显即使在那时也有效.

edit: Changed factor levels to characters so it is clear that this works even then.

这篇关于覆盖 plot.factor 的技巧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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