3

我一直在使用 Car 包中的 scatterplot 命令来创建我的数据图,并且我正在尝试优化图像以供发布。因此它需要是黑白的,这意味着我需要将彩色线更改为实线和虚线。我认为这lty将是执行此操作的正确命令。在 scatterplot 的帮助下,它有一个名为的函数by.groups,我认为这在某种程度上干扰了我在legend部分代码中使用的 lty = c(1,2) 或 lty = 1:2 的想法。我不知道如何在 ggplot 中执行此操作,因此如果人们愿意,我们将不胜感激。

这是一些示例数据:

structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 32L, 33L, 
33L, 34L, 34L, 34L), .Label = c("F07001", "F07002", "F07003", 
"F07004", "F07005", "F07006", "F07008", "F07009", "F07010", "F07011", 
"F07014", "F07015", "F07017", "F07018", "F07019", "F07020", "F07021", 
"F07022", "F07023", "F07024", "F10001", "F10004", "F10008", "F10009", 
"F10010", "F10012", "F10013", "F98015", "M07007", "M07012", "M07013", 
"M07016", "M10007", "M10011", "M10015"), class = "factor"), Season = structure(c(1L, 
1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L), .Label = c("SUM", "WIN"
), class = "factor"), Time = structure(c(1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L), .Label = c("day", "night"), class = "factor"), 
    Repro = structure(c(2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
    3L), .Label = c("f", "fc", "m"), class = "factor"), Comp1 = c(-0.524557195, 
    -0.794214153, -0.408247216, -0.621285004, -0.238828585, 0.976634392, 
    -0.202405922, -0.633821539, -0.306163898, -0.302261589, 1.218779672
    ), ln1wr = c(0.833126490613386, 0.824526258616325, 0.990730077688989, 
    0.981816265754353, 0.933462450382474, 1.446048015519, 1.13253050687157, 
    1.1349442179155, 1.14965388471562, 1.14879830358128, 1.14055365645628
    )), .Names = c("ID", "Season", "Time", "Repro", "Comp1", 
"ln1wr"), row.names = c(1L, 2L, 3L, 4L, 5L, 220L, 221L, 222L, 
223L, 224L, 225L), class = "data.frame")

这是我迄今为止编写的代码:

par(bty="l",las=1)
scatterplot(Comp1~ln1wr|Season, moose,
            xlab = "Wolf risk", ylab = "Principal component 1",
            labels= row.names(moose),
            by.groups=TRUE, smooth=FALSE, boxplots=FALSE,
            grid=FALSE, lty = 1:2,
            legend.plot=FALSE)
legend("bottomright", title="Season",
       legend=levels(moose$Season), bty="n",
       pch=1:2, col=1:2, lty=c(1,2))
4

2 回答 2

3

这是格子里的东西:

 xyplot(Comp1~ln1wr, 
        data=moose,
        groups=Season,
        xlab = "Wolf risk", ylab = "Principal component 1",
        par.settings=list(superpose.symbol=list(pch=1:2, col=1),
                          superpose.line=list(lty=1:2, col=1)),
        panel = function(x, y, ...) {
          panel.superpose(x, y, ...,
                          panel.groups = function(x,y, col, col.symbol, lty, ...) {
                            panel.xyplot(x, y, col=col.symbol, ...)
                            panel.abline(lm(y~x), col.line=col.symbol, lty=lty)
                          }
          )
        },
        auto.key=list(title='Season', space='inside', 
                      #text=c('Summer', 'Winter'),
                      lines=TRUE)
 )

在此处输入图像描述

于 2013-01-10T14:42:09.390 回答
2

抱歉@BenBolker,在看到您的评论之前,我开始着手处理这个问题:

是的,看起来破解该scatterplot.default功能将是解决此问题的一种方法。

由于它是一个非导出函数,这有点棘手,但这里是

# Get function and create copy to edit:
mySP <- car:::scatterplot.default

# Change part of function that plots lines to index lty by i
body(mySP)[[34]][[4]][[4]][[3]][[3]][[3]] <- 
  quote(reg(reg.line, .x[subs], .y[subs], lty = lty[i], lwd = lwd, log.x = logged("x"), 
    log.y = logged("y"), col = col[i]))

# Assign altered function back to package namespace
assignInNamespace("scatterplot.default", mySP, "car")

现在您的代码应该产生一条实线和一条虚线。

于 2013-01-10T14:48:49.507 回答