0

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
Pritish Kakodkar
  • 307
  • 1
  • 4
  • 12

1 Answers1

2

The elements of your color list l need proper names. They must be the names attributed to the two lines (1 and 2 in your example). Adding names(l) <- 1:2 to your code now the line colors can be programmatically set.

library(mschart)
library(data.table)
library(dplyr)

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),]  )     
  l <- list()
  for (i in 1:length(groups)) {
    l[[groups[i]]] <- color_list[i]
  }
  # Set Names for elements of the color list
  names(l) <- 1:2
  ###      
  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( values=l ) 
  rr_chart <- set_theme(rr_chart, crm_chart_theme)       
}

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)))
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'))    
print(x, preview=T)

enter image description here

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58