0

I want to create a function for plotting using ggplot2 because I will have to create several plots using the same fundamental formatting etc. I am creating right now the formula by providing the data frame, x, and y value for the function as arguments. This is pretty much in line with/similar to the ggplot2::ggplot() function.

By providing the function the way I posted it below, I do have to provide the x and y value in form of dataframe$x and dataframe$y. In principal this is fine, but I was interested in how to do this without naming the dataframe again, since this information is already provided by the data frame argument.

scatter_plot = function(tabelle, xwert, ywert) {
  ggplot(data = tabelle, mapping = aes(x = xwert,
                                       y = ywert)) +
    geom_point(aes(fill = base::as.factor(x = internal_id), size = RIN), shape = 21) +
    guides(fill = FALSE) +
    geom_text_repel(mapping = aes(label = internal_id),
                         size = 3, force = 5, box.padding = 0.1,
                         segment.alpha = 1, segment.color = "black", segment.size = 0.5) +
    theme_wrap
}

So... at the end I am only providing the following to the function:

scatter_plot(dataframe, xvalues, yvalues)

and not

scatter_plot(dataframe, dataframe$xvalues, dataframe$yvalues)

Thanks much for your help!

Ben
  • 99
  • 6

1 Answers1

0

One option is using aes_string() instead of aes and having xwert and ywert be strings containing the name of your columns.

Fino
  • 1,774
  • 11
  • 21