Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R脚本中将参数传递给ggplot时出现问题_R_Ggplot2_Arguments - Fatal编程技术网

在R脚本中将参数传递给ggplot时出现问题

在R脚本中将参数传递给ggplot时出现问题,r,ggplot2,arguments,R,Ggplot2,Arguments,我试图找出我把参数传递给ggplot的错误之处。我已经用现有的帖子走了很长一段路,但在这里遇到了一堵墙。可能有些愚蠢的事情,但这里是这样的(我省略了一些绘图格式,因为这不是问题所在): 融化的数据集“lagres”在两种情况下是相同的 > str(lagres) 'data.frame': 30 obs. of 4 variables: $ ST : Factor w/ 3 levels $ year : Factor w/ 6 levels $ va

我试图找出我把参数传递给ggplot的错误之处。我已经用现有的帖子走了很长一段路,但在这里遇到了一堵墙。可能有些愚蠢的事情,但这里是这样的(我省略了一些绘图格式,因为这不是问题所在):

融化的数据集“lagres”在两种情况下是相同的

> str(lagres)
 'data.frame':  30 obs. of  4 variables:
  $ ST      : Factor w/ 3 levels 
  $ year    : Factor w/ 6 levels 
  $ variable: Factor w/ 2 levels 
  $ value   : num  
第一次调用非常有效:

ggplot(lagres, aes(quarter, value, group = interaction(ERTp, variable), linetype = variable, color = ERTp, shape = variable ))

尝试将其转换为接受参数并在for循环脚本中重新使用是无效的,即使结构实际上是相同的:

timevar <- "quarter"
grpvar  <- "ERTp"
fplot       <- function(lagres, timevar, grpvar, ylb, tlb){
plot <- ggplot(lagres, aes_string(x=timevar, y="value", group = interaction("variable", grpvar), linetype = "variable", color = grpvar, shape = "variable")) +
  geom_line(size = 0.5) + geom_point(size = 3) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) + labs(y = ylb) +
  ggtitle(paste(tlb, grpvar, today, sep = ", ")) +
  theme(plot.title = element_text(lineheight = .8, face = "bold", hjust = 0.5))

fplot(lagres, timevar, grpvar)
然后,使用引用的参数创建“aes_string()”方法:

> str(aes_string(timevar, "value", group = interaction(grpvar,  "variable"), linetype = "variable", color = grpvar, shape = "variable" ))
List of 6
 $ group   : Factor w/ 1 level "ST.variable": 1
 $ linetype: symbol variable
 $ colour  : symbol ST
 $ shape   : symbol variable
 $ x       : symbol quarter
 $ y       : symbol value
所以,让团队成为“语言互动”或是一个单层次因素,会有不同吗?无法确定如何处理解析问题,以便正确地进行组交互。在某个地方看到可以使用“粘贴()”,但是,不,这不起作用。传递所有参数(因此,aes_string()公式中没有带引号的文本)也没有帮助

> dput(lagres)
structure(list(ST = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 3L, 1L, 2L, 
3L, 2L, 3L, 1L, 3L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label =   c("GeraghtyM", 
"Other", "WeenJ"), class = "factor"), quarter = structure(c(1L, 
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 
6L, 7L, 7L, 7L, 1L, 2L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 
6L, 7L, 7L, 7L), .Label = c("2015-Q2", "2015-Q3", "2015-Q4", 
"2016-Q1", "2016-Q2", "2016-Q3", "2016-Q4"), class = "factor"), 
variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label =         c("ScanLag", 
"TPADoorToLag"), class = "factor"), value = c(45.3333333333333, 
60.2857142857143, 37.6, 0, 51.375, 95.4166666666667, 26.8, 
42.75, 200, 28, 134, 68.2941176470588, 29, 42.8, 140.7, 0, 
49.2222222222222, 103.833333333333, 0, 20.125, 0, 67.75, 
48, 87, 93, 78, 49.5, 55, 65.6, 83, 59, 54, 153, 114, 111, 
83, 8.66666666666667)), .Names = c("ST", "quarter", "variable", 
"value"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 24L, 25L, 
26L, 27L, 29L, 30L, 31L, 33L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 
42L), class = "data.frame", na.action = structure(c(22L, 23L, 
28L, 32L, 34L), .Names = c("22", "23", "28", "32", "34"), class =           "omit"))

aes\u字符串
没有读取您正在使用的
交互
代码。避免这种情况的一种方法是,在绘图之前,在函数内的数据集中创建一个新的“交互”变量

例如:

fplot <- function(lagres, timevar, grpvar){
    lagres$combine = interaction(lagres[["variable"]], lagres[[grpvar]])
    plot <- ggplot(lagres, aes_string(x=timevar, y="value", 
                                      group = "combine", linetype = "variable", 
                                      color = grpvar, shape = "variable")) +
        geom_line(size = 0.5) + 
        geom_point(size = 3) 
    plot
}

fplot argh,试图避免输入错误,但最后一段应该说“livetype”arg,而不是livetype。Sorryauto拼写检查,我猜:“线型”请通过包含更多绘图代码(如
geom_路径
部分代码)和数据集的一个小示例,使您的问题重现。有关如何向问题添加数据的示例,请参阅。添加的代码和数据,HTH,thx!杰出!非常感谢!如果可以的话,给我一个评论/链接,这样我就能更好地理解“语言交互(ERTp,variable)”的含义以及aes如何/为什么以不同的方式解析它。但是,你真的帮我走出了困境。非常感谢。@DocWeen本质上是
aes\u字符串
要求变量名以字符形式写入,因此
aes\u字符串
中的整个
交互
代码需要是字符。但是
交互
需要非字符变量名。有关相关问题和答案,请参阅。此外,如果我的答案帮助您解决了问题,您可以单击它旁边的复选标记。
fplot <- function(lagres, timevar, grpvar){
    lagres$combine = interaction(lagres[["variable"]], lagres[[grpvar]])
    plot <- ggplot(lagres, aes_string(x=timevar, y="value", 
                                      group = "combine", linetype = "variable", 
                                      color = grpvar, shape = "variable")) +
        geom_line(size = 0.5) + 
        geom_point(size = 3) 
    plot
}