Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass column names to a function that processes data.frames

Tags:

r

ggplot2

I'm plotting lots of similar graphs so I thought I write a function to simplify the task. I'd like to pass it a data.frame and the name of the column to be plotted. Here is what I have tried:

plot_individual_subjects <- function(var, data)
{
  require(ggplot2)

  ggplot(data, aes(x=Time, y=var, group=Subject, colour=SubjectID)) +
    geom_line() + geom_point() + 
    geom_text(aes(label=Subject), hjust=0, vjust=0)
}

Now if var is a string it will not work. It will not work either if change the aes part of the ggplot command to y=data[,var] and it will complain about not being able to subset a closure.

So what is the correct way/best practice to solve this and similar problems? How can I pass column names easily and safely to functions that would like to do processing on data.frames?

like image 314
AlefSin Avatar asked Mar 02 '12 18:03

AlefSin


People also ask

Which function in R returns the column names of a data frame?

To find the column names and row names in an R data frame based on a condition, we can use row. names and colnames function.

How do I select a column in a function in R?

To select a column in R you can use brackets e.g., YourDataFrame['Column'] will take the column named “Column”. Furthermore, we can also use dplyr and the select() function to get columns by name or index. For instance, select(YourDataFrame, c('A', 'B') will take the columns named “A” and “B” from the dataframe.

How do you call a column in a Dataframe in R?

To access a specific column in a dataframe by name, you use the $ operator in the form df$name where df is the name of the dataframe, and name is the name of the column you are interested in. This operation will then return the column you want as a vector.


1 Answers

Bad Joran, answering in the comments!

You want to use aes_string, which allows you to pass variable names as strings. In your particular case, since you only seem to want to modify the y variable, you probably want to reorganize which aesthetics are mapped in which geoms. For instance, maybe something like this:

ggplot(data, aes_string(y = var)) + 
    geom_line(aes(x = Time,group = Subject,colour = SubjectID)) + 
    geom_point(aes(x = Time,group = Subject,colour = SubjectID)) + 
    geom_text(aes(x = Time,group = Subject,colour = SubjectID,label = Subject),hjust =0,vjust = 0)

or perhaps the other way around, depending on your tastes.

like image 195
joran Avatar answered Oct 17 '22 21:10

joran