R ggplot内部功能不工作,尽管已解除保护(替换
我在此页面上使用了search for Gender:Name中的示例和命令。我尝试将该命令放在函数中,并尝试在变量名周围不加引号的情况下调用该函数,但尽管使用了substitutedparse…,但该命令仍不起作用:R ggplot内部功能不工作,尽管已解除保护(替换,r,plot,ggplot2,R,Plot,Ggplot2,我在此页面上使用了search for Gender:Name中的示例和命令。我尝试将该命令放在函数中,并尝试在变量名周围不加引号的情况下调用该函数,但尽管使用了substitutedparse…,但该命令仍不起作用: phil <- structure(list(Name = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3
phil <- structure(list(Name = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L), .Label = c("Bucks County", "Delaware County", "Montgomery County",
"Philadelphia County"), class = "factor"), Gender = structure(c(1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("Female", "Male"), class = "factor"),
value = c(15288L, 43054L, 25788L, 62853L, 33622L, 50792L,
27184L, 71321L, 43593L, 94877L, 32442L, 54872L, 43751L, 18346L,
25545L, 87732L, 46656L, 63640L, 39675L, 25468L, 43636L, 34558L,
59923L, 26979L, 17550L, 27492L, 71404L, 39946L, 50107L, 96580L,
24957L, 17433L, 31468L, 40585L, 53239L, 21899L, 62542L, 38352L,
47008L, 31485L), Level = structure(c(1L, 4L, 2L, 5L, 3L,
3L, 1L, 4L, 2L, 5L, 3L, 5L, 4L, 1L, 2L, 5L, 3L, 4L, 2L, 1L,
4L, 3L, 5L, 2L, 1L, 1L, 4L, 2L, 3L, 5L, 2L, 1L, 3L, 4L, 5L,
1L, 5L, 3L, 4L, 2L), .Label = c("LessHigh", "High", "SomeAssoc",
"Bachelors", "GradProf"), class = "factor")), .Names = c("Name",
"Gender", "value", "Level"), class = "data.frame", row.names = c(3463L,
3465L, 3466L, 3467L, 3468L, 3471L, 3473L, 3475L, 3478L, 3479L,
8741L, 8742L, 8743L, 8746L, 8750L, 8751L, 8752L, 8754L, 8756L,
8757L, 22925L, 22926L, 22927L, 22928L, 22929L, 22933L, 22937L,
22938L, 22939L, 22940L, 25844L, 25845L, 25846L, 25847L, 25849L,
25854L, 25855L, 25856L, 25858L, 25860L))
testfn = function(gdf, Level, value, Gender, Name){
print(
ggplot(gdf, aes(deparse(substitute(Level)), deparse(substitute(value)),
color = deparse(substitute(Name)),
linetype = deparse(substitute(Gender)),
group = deparse(substitute(Gender)):deparse(substitute(Name))))+
geom_point()+
geom_line()
)
}
testfn(phil, Level, value, Gender, Name)
Error in deparse(substitute(Gender)):deparse(substitute(Name)) :
NA/NaN argument
In addition: Warning messages:
1: In deparse(substitute(Gender)):deparse(substitute(Name)) :
numerical expression has 4 elements: only the first used
2: In deparse(substitute(Gender)):deparse(substitute(Name)) :
numerical expression has 5 elements: only the first used
3: In eval(expr, envir, enclos) : NAs introduced by coercion
4: In eval(expr, envir, enclos) : NAs introduced by coercion
5: In deparse(substitute(Gender)):deparse(substitute(Name)) :
numerical expression has 4 elements: only the first used
6: In deparse(substitute(Gender)):deparse(substitute(Name)) :
numerical expression has 5 elements: only the first used
7: In eval(expr, envir, enclos) : NAs introduced by coercion
8: In eval(expr, envir, enclos) : NAs introduced by coercion
>
哪里可能有问题。谢谢你的帮助。你确定吗?看起来移除deparse使一些我认为可能不起作用的事情,实际上也起作用了:-
testfn = function(gdf, Level, value, Gender, Name) {
gg <- ggplot(gdf, aes(x = substitute(Level),
y = substitute(value),
color = substitute(Name),
linetype = substitute(Gender),
group = substitute(Gender):substitute(Name)
))
gg <- gg + geom_point()
gg <- gg + geom_line()
print(gg)
}
testfn(phil, Level, value, Gender, Name)
在testfn中,您使用gdf作为参数,但不使用它。请用它替换phil。您是否尝试删除deparse调用?此外,group=line prbly不起作用,而且ggplot不会喜欢这样的线型切换,即使使用正确的语法。感谢您指出这一点。我已在上面的函数中对其进行了更正。但错误仍然存在这完全一样。它必须在访问gdf之前发生。我实际上已经按照本页上的建议加入了deparse部分:有什么特别的原因不使用aes_字符串吗?只是因为不使用引号更容易键入。