I am creating function which makes a ggplot. I need this function to add a ggtitle using the column name from which the data used for lotting came from.
the function is for the variable yvar and starts like this: scatter_plotseries <- function(yvar). yvar is essentially a list of values from a specific column: scatter_plots <- lapply(df[, c("D_x", "D_y", "D_z", "D_j", "D_k")], scatter_plotseries) I want the name of each plot to be called D_x or D_y etc.
different options I have tried within my ggplot:
- ggtitle(as.character(colnames(df$yvar)))
- ggtitle(as.character(names(df$yvar)))
- ggtitle(as.character(colnames(df[, yvar])))
- ggtitle(as.character(names(df[,yvar])))
I also tried something like this: names(df)[apply(df, 2, function(i) which(i == "D_x"))]
none of these options give the name of the specific column. they either return NULL or an error.
Please and thank you for the help!!