如何调整interaction.plot和lineplot.CI的图例位置?

How to adjust legend position of interaction.plot and lineplot.CI?

我是编码初学者。我试图创建一个交互图。这是我的代码:

数据来自 data of the book "Learning Statistics with R.

的临床试验
library(sciplot)
library(lsr)
library(gplots)
lineplot.CI(x.factor = clin.trial$drug,
             response = clin.trial$mood.gain,
             group = clin.trial$therapy,
             ci.fun = ciMean,
             xlab = "Drug",
             ylab = "Mood Gain")

它生成的图形如下所示:

如图所示,图例框不在我的屏幕内。

我还尝试使用以下代码创建另一个图:

interaction.plot(x.factor = clin.trial$drug,
                 trace.factor = clin.trial$therapy,
                 response = clin.trial$mood.gain,
                 fun = mean,
                 type = "l",
                 lty = 1,  # line type
                 lwd = 2,  # line width
                 legend = T,
                 xlab = "Drug", ylab = "Mood Gain",
                 col = c("#00AFBB", "#E7B800"),
                 xpd = F,
                 trace.label = "Therapy")

对于这段代码,我得到了这样的图表:

在此图中,图例没有标签。

谁能帮我解决这些关于传奇的问题?

您可能计划通过 RStudio GUI 保存绘图。当您使用鼠标调整绘图 window 大小时,您需要再次 运行 代码以刷新图例尺寸。

但是,使用更复杂的方法是有利的,例如将其保存为具有固定尺寸的 png,如下所示:

library("sciplot")
library("lsr")
library("gplots")

png("Plot_1.png", height=400, width=500)
lineplot.CI(x.factor=clin.trial$drug,
            response=clin.trial$mood.gain,
            group=clin.trial$therapy,
            ci.fun=ciMean,
            xlab="Drug",
            ylab="Mood Gain"
)
dev.off()

png("Plot_2.png", height=400, width=500)
interaction.plot(x.factor=clin.trial$drug,
                 trace.factor=clin.trial$therapy,
                 response=clin.trial$mood.gain,
                 fun=mean,
                 type="l",
                 lty=1,  # line type
                 lwd=2,  # line width
                 legend=T,
                 xlab="Drug", ylab="Mood Gain",
                 col=c("#00AFBB", "#E7B800"),
                 xpd=F,
                 trace.label="Therapy")
dev.off()

绘图已保存到您的工作目录中,检查getwd()

编辑

您还可以调整图例位置。

lineplot.CI中你可以使用参数;要么只使用 x 的字符,例如x.leg="topleft" 或两个坐标均为数字 x.leg=.8, y.leg=2.2.

interaction.plot 还没有提供这个功能。我在下面提供了一个被黑的版本。参数称为 xlegyleg,功能同上。

有关详细说明,请参阅 ?legend

interaction.plot <- function (x.factor, trace.factor, response, fun = mean,
                              type = c("l", "p", "b", "o", "c"), legend = TRUE, 
                              trace.label = deparse(substitute(trace.factor)), 
                              fixed = FALSE, xlab = deparse(substitute(x.factor)),
                              ylab = ylabel, ylim = range(cells, na.rm = TRUE), 
                              lty = nc:1, col = 1, pch = c(1L:9, 0, letters), 
                              xpd = NULL, leg.bg = par("bg"), leg.bty = "n", 
                              xtick = FALSE, xaxt = par("xaxt"), axes = TRUE, 
                              xleg=NULL, yleg=NULL, ...) {
  ylabel <- paste(deparse(substitute(fun)), "of ", deparse(substitute(response)))
  type <- match.arg(type)
  cells <- tapply(response, list(x.factor, trace.factor), fun)
  nr <- nrow(cells)
  nc <- ncol(cells)
  xvals <- 1L:nr
  if (is.ordered(x.factor)) {
    wn <- getOption("warn")
    options(warn = -1)
    xnm <- as.numeric(levels(x.factor))
    options(warn = wn)
    if (!anyNA(xnm)) 
      xvals <- xnm
  }
  xlabs <- rownames(cells)
  ylabs <- colnames(cells)
  nch <- max(sapply(ylabs, nchar, type = "width"))
  if (is.null(xlabs)) 
    xlabs <- as.character(xvals)
  if (is.null(ylabs)) 
    ylabs <- as.character(1L:nc)
  xlim <- range(xvals)
  if (is.null(xleg)) {
    xleg <- xlim[2L] + 0.05 * diff(xlim)
    xlim <- xlim + c(-0.2/nr, if (legend) 0.2 + 0.02 * nch else 0.2/nr) * 
      diff(xlim)
  }
  dev.hold()
  on.exit(dev.flush())
  matplot(xvals, cells, ..., type = type, xlim = xlim, ylim = ylim, 
          xlab = xlab, ylab = ylab, axes = axes, xaxt = "n", 
          col = col, lty = lty, pch = pch)
  if (axes && xaxt != "n") {
    axisInt <- function(x, main, sub, lwd, bg, log, asp, 
                        ...) axis(1, x, ...)
    mgp. <- par("mgp")
    if (!xtick) 
      mgp.[2L] <- 0
    axisInt(1, at = xvals, labels = xlabs, tick = xtick, 
            mgp = mgp., xaxt = xaxt, ...)
  }
  if (legend) {
    yrng <- diff(ylim)
    if (is.null(yleg))
      yleg <- ylim[2L] - 0.1 * yrng
    if (!is.null(xpd) || {
      xpd. <- par("xpd")
      !is.na(xpd.) && !xpd. && (xpd <- TRUE)
    }) {
      op <- par(xpd = xpd)
      on.exit(par(op), add = TRUE)
    }
    # text(xleg, ylim[2L] - 0.05 * yrng, paste("  ", 
    #                                          trace.label), adj = 0)
    if (!fixed) {
      ord <- sort.list(cells[nr, ], decreasing = TRUE)
      ylabs <- ylabs[ord]
      lty <- lty[1 + (ord - 1)%%length(lty)]
      col <- col[1 + (ord - 1)%%length(col)]
      pch <- pch[ord]
    }
    legend(xleg, yleg, legend = ylabs, col = col, 
           title = if (trace.label == "") NULL else trace.label,
           pch = if (type %in% c("p", "b")) 
             pch, lty = if (type %in% c("l", "b")) 
               lty, bty = leg.bty, bg = leg.bg)
  }
  invisible()
}

数据:

lk <- "https://learningstatisticswithr.com/data.zip"
tmp <- tempfile()
tmp.dir <- tempdir()
download.file(lk, tmp)
unzip(tmp, exdir=tmp.dir)
load("data/clinicaltrial.Rdata")