如何合并aes()和aes_string()选项 [英] How do I combine aes() and aes_string() options
问题描述
假设我有一个这样的情节
Let's say I have a plot like this
library(ggplot2)
ggplot(mtcars, aes(x=wt)) + ylab("") +
geom_line(aes(y=mpg, color="one")) +
geom_line(aes(y=qsec, color="two")) +
scale_color_manual(name="Val", values=c(one="#105B63",two="#BD4932"))
在这里我绘制两条线,并为每条线指定一个颜色组.现在,假设我想动态地将变量名称指定为字符值,这意味着如果需要尝试,我将需要使用aes_string().
where I am plotting two lines and specifying a color group for each. Now let's say I want to specify the variables names dynamically as character values which means I'll need to use aes_string().
If I try
v1<-"mpg"
v2<-"qsec"
ggplot(mtcars, aes(x=wt)) + ylab("") +
geom_line(aes_string(y=v1, color="one")) +
geom_line(aes_string(y=v2, color="two")) +
scale_color_manual(name="Val", values=c(one="#105B63",two="#BD4932"))
我得到了错误
Error in eval(expr, envir, enclos) : object 'one' not found
因为现在aes_string()
试图解析颜色值,当我只想要文字字符值时.如果我尝试
because now aes_string()
is trying to parse the color value when I just want a literal character value. And if I try
ggplot(mtcars, aes(x=wt)) + ylab("") +
geom_line(aes_string(y=v1), aes(color="one")) +
geom_line(aes_string(y=v2), aes(color="two")) +
scale_color_manual(name="Val", values=c(one="#105B63",two="#BD4932"))
我知道
Error: ggplot2 doesn't know how to deal with data of class uneval
大概是因为该层不知道如何处理两个美学指令.
presumably because the layer doesn't know how to handle two aesthetic directives.
如何结合aes()
和aes_string()
美学,或者如何为aes_string()
指定文字字符值?
How can I combine aes()
and aes_string()
aesthetics or how can I specify literal character values for aes_string()
?
推荐答案
如果要在aes_string()
中指定字符文字值,最简单的方法是使用shQuote()
引用该值.例如
If you want to specify a character literal value in aes_string()
, the easiest thing would be to use shQuote()
to quote the value. For example
ggplot(mtcars, aes(x=wt)) + ylab("") +
geom_line(aes_string(y=v1, color=shQuote("one"))) +
geom_line(aes_string(y=v2, color=shQuote("two"))) +
scale_color_manual(name="Val", values=c(one="#105B63",two="#BD4932"))
之所以起作用,是因为aes_string()
实际上在每个参数值上运行parse(text=)
. shQuote()
函数在字符值周围添加引号,以便在解析该值时,您可以返回一个字符值,而不是符号/名称.比较这两个电话
This works because aes_string()
actually runs parse(text=)
on each of the parameter values. The shQuote()
function adds quotes around your character value so that when you parse the value, you get a character value back rather than a symbol/name. Compare these two calls
class(parse(text="a")[[1]])
# [1] "name"
class(parse(text=shQuote("a"))[[1]])
# [1] "character"
或者,您可能会考虑合并aes()
指令. aes()
函数实际上只是返回具有uneval
类的列表.我们可以定义一个函数来合并/合并这些列表.例如,我们可以定义加法
Alternatively, you might think of merging aes()
directives. The aes()
functions really just return a list with a class of uneval
. We could define a function to combine/merge these list. For example we can define addition
`+.uneval` <- function(a,b) {
`class<-`(modifyList(a,b), "uneval")
}
现在我们可以做
ggplot(mtcars, aes(x=wt)) + ylab("") +
geom_line(aes_string(y=v1) + aes(color="one")) +
geom_line(aes_string(y=v2) + aes(color="two")) +
scale_color_manual(name="Val", values=c(one="#105B63",two="#BD4932"))
这篇关于如何合并aes()和aes_string()选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!