R pass variable column indices to ggplot2
41,929
You can use the aes_string
in stead of aes
to pass string in stead of using objects, i.e.:
myplot = function(df, x_string, y_string) {
ggplot(df, aes_string(x = x_string, y = y_string)) + geom_point()
}
myplot(df, "A", "B")
myplot(df, "B", "A")
![N8TRO](https://i.stack.imgur.com/PtwZq.png?s=256&g=1)
Comments
-
N8TRO almost 2 years
I'm attempting to pass the column indices to ggplot as part of a function I'll be using repeatedly. like:
myplot <- function(df){ ggplot(df, aes(df[, 1], df[, 2])) + geom_point() }
I'll always be using the first column as my x variable and the second column as my y-variable, but the column names change between data sets. I've searched all over.. Any ideas?
EDIT:
This is the answer I used:
require(ggplot2) myplot <- function(df){ ggplot(df, aes_string(colnames(df)[1], colnames(df)[2])) + geom_point() }
-
N8TRO over 11 yearsThanks, it works, though not ideal because I'd have to manually give the column names. Any way to get around this?
-
baptiste over 11 years
aes_string(colnames(df)[1], colnames(df)[2])
-
Paul Hiemstra over 11 yearsIn general, in
ggplot2
you do not provide vectors inaes
. Inaes
you provide a mapping of the aesthetics of the plot to columns in the data, with no need to hardcode the data inaes
. -
Arun over 11 yearsIf you column name is
a-b
for example, then this gives the errorError in eval(expr, envir, enclos) : object 'a-b' not found
. Usingenvironment = environment()
withaes
is another fix as linked above under OP's post. -
Paul Hiemstra over 11 yearsI think there is no way, other than passing vectors, in
ggplot2
to make a plot with this kind of column name. I never encountered this, but I follow Hadley's style and always use_
;). -
Arun over 11 years@PaulHiemstra, I already linked one way of doing this (without using column names at all). If you insist on using column names, then try this instead:
set.seed(45); df <- data.frame(x=gl(5,5), y=runif(25)); myplot2 = function(df, col1, col2) { ggplot(df, aes(x = get(names(df)[col1]), y = get(names(df)[col2])), environment = environment()) + geom_point() }
. From this it is straightforward to change this function to take column names as arguments. -
Paul Hiemstra over 11 yearsNice, thanks for the code Arun.
-
vruizext over 9 yearsjust was i was looking for, you saved me a good time, thanks :)
-
shadow about 9 years@PaulHiemstra, @Arun: Using
aes_q
is another way of passing non-standard column names toggplot
. Why I think that's the preferable solution see here. -
Bryan Shalloway almost 3 yearsaes_string() is now soft deprecated. I think current tidyverse practice would be to use .data calls, as I show at my solution on this related post: stackoverflow.com/a/69286410/9059865