且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用变量在ggplot中指定列名

更新时间:2022-11-11 18:14:12

你可以使用aes_string:

f <- function( column ) {
    ...
    ggplot( rates.by.groups, aes_string(x="name", y="rate", colour= column,
                                        group=column ) )
}

只要您将列作为字符串传递给函数(f("majr") 而不是 f(majr)).另请注意,我们将其他列 "name""rate" 更改为字符串.

as long as you pass the column to the function as a string (f("majr") rather than f(majr)). Also note that we changed the other columns, "name" and "rate", to be strings.

如果出于某种原因您不想使用 aes_string,您可以将其更改为(稍微麻烦一些):

If for whatever reason you'd rather not use aes_string, you could change it to (the somewhat more cumbersome):

    ggplot( rates.by.groups, aes(x=name, y=rate, colour= get(column),
                                        group=get(column) ) )