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")
Share:
41,929
N8TRO
Author by

N8TRO

Experimental Electronic Circuits Engineer

Updated on July 05, 2022

Comments

  • N8TRO
    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
    N8TRO over 11 years
    Thanks, it works, though not ideal because I'd have to manually give the column names. Any way to get around this?
  • baptiste
    baptiste over 11 years
    aes_string(colnames(df)[1], colnames(df)[2])
  • Paul Hiemstra
    Paul Hiemstra over 11 years
    In general, in ggplot2 you do not provide vectors in aes. In aes you provide a mapping of the aesthetics of the plot to columns in the data, with no need to hardcode the data in aes.
  • Arun
    Arun over 11 years
    If you column name is a-b for example, then this gives the error Error in eval(expr, envir, enclos) : object 'a-b' not found. Using environment = environment() with aes is another fix as linked above under OP's post.
  • Paul Hiemstra
    Paul Hiemstra over 11 years
    I 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
    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
    Paul Hiemstra over 11 years
    Nice, thanks for the code Arun.
  • vruizext
    vruizext over 9 years
    just was i was looking for, you saved me a good time, thanks :)
  • shadow
    shadow about 9 years
    @PaulHiemstra, @Arun: Using aes_q is another way of passing non-standard column names to ggplot. Why I think that's the preferable solution see here.
  • Bryan Shalloway
    Bryan Shalloway almost 3 years
    aes_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