从R中的库效果中删除图​​中第3轴和第4轴上的刻度 [英] removing the ticks on the 3rd and 4th axes in plots from library effects in R

查看:42
本文介绍了从R中的库效果中删除图​​中第3轴和第4轴上的刻度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以消除库 effects 生成的图的第3和第4轴上的刻度线(轴),如下所示?

I'm wondering if there is a way to remove the tickmarks (the axes) on the 3rd and 4th axes of the plot generated by library effects as shown below?

library(effects)
m <- lm(Fertility ~ ., data = swiss)
plot(allEffects(m), rug = FALSE)

推荐答案

程序包的作者似乎不愿很轻松地适当地公开它.我们可以编写我们自己的 plot.efflist 版本,该版本在这里完成了大部分工作.这是替代版本

It doesn't look like the package authors choose to expose that propertly very easily. We could write our own version of plot.efflist which is doing most of the work here. Here's the alternative version

plot.efflist <- function (x, selection, rows, cols, graphics = TRUE, 
          lattice, ...) 
{
  lattice <- if (missing(lattice)) 
    list()
  else lattice
  if (!missing(selection)) {
    if (is.character(selection)) 
      selection <- gsub(" ", "", selection)
    pp <- plot(x[[selection]], lattice = lattice, ...)
    pp$x.scales$tck=c(1,0)
    pp$y.scales$tck=c(1,0)
    return(pp)
  }
  effects <- gsub(":", "*", names(x))
  neffects <- length(x)
  mfrow <- mfrow(neffects)
  if (missing(rows) || missing(cols)) {
    rows <- mfrow[1]
    cols <- mfrow[2]
  }
  for (i in 1:rows) {
    for (j in 1:cols) {
      if ((i - 1) * cols + j > neffects) 
        break
      more <- !((i - 1) * cols + j == neffects)
      lattice[["array"]] <- list(row = i, col = j, 
                                 nrow = rows, ncol = cols, more = more)
      pp <- plot(x[[(i - 1) * cols + j]], lattice = lattice, 
                 ...)
      # hack to turn off opposite side tick marks
      pp$x.scales$tck=c(1,0)
      pp$y.scales$tck=c(1,0)
      print(pp)
    }
  }
}
environment(plot.efflist) <- asNamespace("effects")

基本上,我们只是按原样调用 plot.eff 函数,然后修改结果以在绘图前关闭第二组刻度线.

Basically we just call the plot.eff function as is, then modify the result to turn off the second set of ticks before plotting.

这将返回

plot(allEffects(m), rug = FALSE)

您也可以尝试这种方法

plot.eff <- function(...) {
  pp <- effects:::plot.eff(...)
  pp$x.scales$tck=c(1,0)
  pp$y.scales$tck=c(1,0)
  pp
}
environment(plot.eff) <- asNamespace("effects")
helpenv <- new.env(parent = asNamespace("effects"))
helpenv$plot.eff <- plot.eff
plot.efflist <- effects:::plot.efflist
environment(plot.efflist) <- helpenv

在这里,我们不仅更改了 efflist 对象上的运算符的功能,还更改了所有 eff 对象的行为.我们进行了重写,但是还需要更改 efflist 版本以首先找到我们的新版本.这种方法使我们不必重复这些功能的任何逻辑,但这确实意味着我们对环境有些混乱.

Here, rather than changing just the function that operators on efflist objects, we change the behavior for all eff objects. We do the rewrite but then also need to change the efflist version to find our new version first. This method keeps us from having to repeat any logic from these functions, but it does mean we make a bit of a mess with environments.

这篇关于从R中的库效果中删除图​​中第3轴和第4轴上的刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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