R ggplot aes_字符串在函数中不起作用

R ggplot aes_字符串在函数中不起作用,r,ggplot2,parameter-passing,R,Ggplot2,Parameter Passing,使用本页给出的示例:,我尝试使用aes_字符串,但它不起作用: testfn <- function(gdf, first, second, third, fourth){ print( ggplot(gdf, aes_string(first, second, color = fourth, linetype = third, group = third:fourth))+ geom_point()+

使用本页给出的示例:,我尝试使用aes_字符串,但它不起作用:

testfn <- function(gdf, first, second, third, fourth){
    print(
    ggplot(gdf, aes_string(first, second,
         color = fourth,
         linetype = third, 
         group = third:fourth))+
       geom_point()+
       geom_line() 
    )
}


> 
> testfn(phil, "Level", "value","Gender","Name")
Error in third:fourth : NA/NaN argument
In addition: Warning messages:
1: In aes_string(first, second, color = fourth, linetype = third, group = third:fourth) :
  NAs introduced by coercion
2: In aes_string(first, second, color = fourth, linetype = third, group = third:fourth) :
  NAs introduced by coercion
> 
testfn
>testfn(菲尔,“级别”、“价值”、“性别”、“姓名”)
第三个错误:第四个错误:NA/NaN参数
此外:警告信息:
1:在aes_字符串中(第一、第二、颜色=第四、线型=第三、组=第三:第四):
强制引入的NAs
2:在aes_字符串中(第一,第二,颜色=第四,线型=第三,组=第三:第四):
强制引入的NAs
> 

问题在哪里。谢谢你的帮助

首先,在
aes_字符串
中,您需要使用
x
y
[比较
args(aes)
args(aes_字符串)
]。然后,可以更容易地将交互术语表述为
paste0(“交互(“,第三,”,“,第四,”)”)
。所以,这两者加起来

testfn <- function(gdf, first, second, third, fourth){
  p <- ggplot(gdf, aes_string(x = first, 
                              y = second,
                              color = fourth,
                              linetype = third,
                              group = paste0("interaction(", third,", ",fourth, ")"))) +
    geom_point() +
    geom_line() 
  print(p)
}
testfn(phil, "Level", "value","Gender","Name")

testfn首先,在
aes_字符串
中,您需要使用
x
y
[比较
args(aes)
args(aes_字符串)
]。然后,可以更容易地将交互术语表述为
paste0(“交互(“,第三,”,“,第四,”)”)
。所以,这两者加起来

testfn <- function(gdf, first, second, third, fourth){
  p <- ggplot(gdf, aes_string(x = first, 
                              y = second,
                              color = fourth,
                              linetype = third,
                              group = paste0("interaction(", third,", ",fourth, ")"))) +
    geom_point() +
    geom_line() 
  print(p)
}
testfn(phil, "Level", "value","Gender","Name")

testfn这是重复的Q吗?ggplot无法理解“第三:第四”。如果你在尝试互动,你需要这样做(见哈德利的评论),是的,这似乎与你之前问的问题非常相似?感谢您指出@BondedDustEarlier的问题是将参数传递给aes,但aes不起作用。现在,我试图将参数传递给aes_字符串(尽管使用相同的数据和函数),但遇到了问题。我正在努力遵守论坛规则,并提供所有链接。感谢您的评论。group=paste0(“交互(“,paste0(“”,第三,”:“,第四,”,collapse=“,”)”)不起作用。错误:尝试应用非功能另外:警告消息:在名称(x)[!is.na(full)]中这是重复的Q?ggplot无法理解“第三:第四”。如果你在尝试互动,你需要这样做(见哈德利的评论),是的,这似乎与你之前问的问题非常相似?感谢您指出@BondedDustEarlier的问题是将参数传递给aes,但aes不起作用。现在,我试图将参数传递给aes_字符串(尽管使用相同的数据和函数),但遇到了问题。我正在努力遵守论坛规则,并提供所有链接。感谢您的评论。group=paste0(“交互(“,paste0(“”,第三,”:“,第四,”,collapse=“,”)”)不起作用。错误:尝试应用非函数。此外:警告消息:在名称(x)[!is.na(full)]中是的,它可以工作!这正是我需要的。谢谢你的帮助。是的,很有效!这正是我需要的。谢谢你的帮助。