I want to create an user defined function to eliminate code redundancies/repetition.
I want to plot multiple plots like this for multiple col2
(which has several levels) and GROUP by GROUP
column.
Sample images of the desired plots:
fn.colplot <- function(data, col2, GROUP) {
dt_PCT <- data[, .N, by = .(col2, GROUP)]
dt_PCT[, PCT := round(100*N/sum(N)), by = .(GROUP)]
dt_plot <- ggplot(dt_PCT, aes(GROUP, PCT, fill = factor(col2))) +
geom_col(position = "fill") +
geom_text(aes(label = paste0(PCT,"%")), position = position_fill(vjust = 0.5))
return(dt_plot)
}
When I run it with my data return an error that col2 is not found while DISTRIBUTORS
column exists in my data.
Error in eval(bysub, x, parent.frame()) : object 'DISTRIBUTORS' not found
May I ask some one know how to call the column names after by =
inside a function defined by user like this?
If I bring the script out and plug in real names of columns, it worked well.
Thanks all for your advice, I have solved the problem by calling the function with specific arguments like this fn.colplot(data = dt_all, col2 = "DISTRIBUTORS", GROUP = "GROUP")
. I could run the function smoothly and plot a lot of plots. Thank you @RonakShah