【问题标题】:Automated plotting, labeling and image saving自动绘图、标记和图像保存
【发布时间】:2020-04-27 09:40:03
【问题描述】:

我写了一个绘图的小函数。 (有关“Schritt1(Int)”的示例结果,请参见图 1)

现在我的问题:

  1. 如何在图片中添加“y”的 Varname(在本例中为“Int”)而不是“y 的平均值”?

  2. 如何将绘图保存为图片作为函数的一部分?

  3. 如果 2.) 有效,如何使 Varname 成为图像名称的一部分?

  4. 请注意,我想用一个函数创建 2 个绘图和 2 个图片。如果这太难了,我会做两个函数。

感谢任何问题的解决方案和建议。

Schritt1 <- function(y) { 


  interaction.plot(x$`Single Posture`, x$Sex, y)
  interaction.plot(x$Person, x$Pos, y)


}

Schritt1 (Int)
Schritt1 (Con)
Schritt1 (Car)
Schritt1 (Pow)


Schritt1(CoP)
Schritt1(Com)
Schritt1(Eth)
Schritt1(Tea)
Schritt1(Adv)

Schritt1(EBD)
Schritt1(Ask)
Schritt1(PP)
Schritt1(PC)
Schritt1(Der)

Schritt1(Ser)

Schritt1(Med)
Schritt1(Lea)
```


【问题讨论】:

    标签: r plot statistics


    【解决方案1】:

    要获取 y 的变量名,您可以使用

    deparse(substitute(y))
    

    按照 Sven Hohenstein (https://stackoverflow.com/a/14577878/11611246) 的建议。

    如果要导出图像,您可能需要给 R 一个目录来保存图像。您可以设置工作目录的标准路径,如下所示:

    Schritt1 <- function(y, dir=F){
    if(!dir){
    directory <- getwd()
    }else{
    directory <- dir
    }
    name <- deparse(substitute(y))
    pdf(file=paste(dir, "/", name, ".pdf", sep = ""))
    interaction.plot(x$`Single Posture`, x$Sex, y)
    dev.off()
    }
    

    如果函数参数中没有设置目录,则在这种情况下,输出图的 pdf 将保存在工作目录中。 其他设备如 jpeg 应该可以像 pdf 设备一样使用。

    为了创建两个图像,您只需将“图像部分”放入您的函数中两次:

    Schritt1 <- function(y, dir=F){
    if(!dir){
    directory <- getwd()
    }else{
    directory <- dir
    }
    name <- deparse(substitute(y))
    #image one
    pdf(file=paste(dir, "/", name, "1.pdf", sep = ""))
    interaction.plot(x$`Single Posture`, x$Sex, y)
    dev.off()
    #image two
    pdf(file=paste(dir, "/", name, "2.pdf", sep = ""))
    interaction.plot(x$Person, x$Pos, y)
    dev.off()
    }
    

    关于交互图,我不确定是否可以简单地编辑轴名称,您可以尝试包含一个 ylab

    Schritt1 <- function(y, dir=F){
    if(!dir){
    directory <- getwd()
    }else{
    directory <- dir
    }
    name <- deparse(substitute(y))
    #image one
    pdf(file=paste(dir, "/", name, "1.pdf", sep = ""))
    interaction.plot(x$`Single Posture`, x$Sex, y, ylab= paste("mean of", name, sep= " "))
    dev.off()
    #image two
    pdf(file=paste(dir, "/", name, "2.pdf", sep = ""))
    interaction.plot(x$Person, x$Pos, y, ylab= paste("mean of", name, sep= " "))
    dev.off()
    }
    

    【讨论】:

      猜你喜欢
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-28
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多