r - create function to ggplot with data.table -
r - create function to ggplot with data.table -
i trying minimise code using functions regularly repeated pieces of code. have need create important number of charts wanting simplify readability.
pretty of info in data.table structures wanting pass function create plot.
reproducible example
library(ggplot2) library(gtable) library(grid) grid.newpage() dt.diamonds <- as.data.table(diamonds) d1 <- dt.diamonds[,list(revenue = sum(price), stones = length(price)), by=clarity] setkey(d1, clarity) mybarchart <- function(data, xdata, ydata, xlabel="", ylabel="", myfill="", myscale="comma") { ggplot(data, aes(x=xdata,y=ydata, fill=myfill)) + geom_bar(stat="identity") + labs(x=xlabel, y=ylabel) + scale_y_continuous(labels=myscale, expand=c(0,0)) + scale_fill_identity(name="") + theme_bw() } mybarchart(d1, "clarity", "revenue") #or mybarchart(d1, "clarity", "revenue", "clarity", "revenue", "red", "dollar")
the error error in eval(expr, envir, enclos) : object 'xdata' not found
, suspect because using data.table rather data.frame.
my approach kind of function has been take examples this , checking other posts on this. yet haven't found else demonstrating how data.table rather data.frame
any ideas how create work data.table?
r function ggplot2 data.table
Comments
Post a Comment