In the code below, I generate some random data. I want to pass it to a function to plot but want to be able to control the looks through parameters. For e.g. I want to control the line colors, but mschart outputs red and blue lines. This is not what I want. I want it programmatically set like the rest of the chart (title , x axis etc).
#### GENERATE RAW AND RANDOM DATA
DT<-data.table(x=1:10,y=1:10, group=c(1))
DT<-rbind(DT,data.table(x=1:10,y=(1:10)**2, group=c(2)))
generate_chart <-
function(DT,x, y, group_by_col, xlab, ylab, title, num_format,color_list)
{
rr_chart <-
ms_linechart(DT,
x = x,
y = y,
group = group_by_col)
groups<-unique(DT[,get(group_by_col),] )
#TRY TO GENERATE LIST FOR COLORS from what is input
l<-list()
for(i in 1:length(groups)){
l[[groups[i]]]<-color_list[i]
}
# Standatd code where the list above is used to color the data
rr_chart <-
chart_ax_x(
rr_chart,
minor_tick_mark = 'none',
major_tick_mark = 'none'
) %>% chart_labels(title = title,
xlab = xlab,
ylab = ylab) %>% chart_ax_y(num_fmt = num_format) %>% chart_data_stroke(
l
)
rr_chart <- set_theme(rr_chart, crm_chart_theme)
}
# call function
x<-generate_chart(DT,x="x", y="y", group_by_col="group", xlab="xxxx", ylab="yyyy", title="Title", num_format="#0.0",color_list=c('red','black'))
#Add to slide
# blank because of missing template