【问题标题】:mschart: How do I pass mschart a dynamic list of column names and colorsmschart:如何向 mschart 传递列名和颜色的动态列表
【发布时间】:2019-12-22 03:01:16
【问题描述】:

在下面的代码中,我生成了一些随机数据。我想将它传递给一个函数进行绘图,但希望能够通过参数控制外观。例如我想控制线条颜色,但 mschart 输出红色和蓝色线条。这不是我想要的。我希望它像图表的其余部分(标题、x 轴等)一样以编程方式设置。

####  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

【问题讨论】:

    标签: r mschart officer


    【解决方案1】:

    颜色列表l 的元素需要正确的名称。它们必须是归属于这两行的名称(在您的示例中为12)。将names(l) &lt;- 1:2 添加到您的代码中,现在可以以编程方式设置线条颜色。

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-31
      • 1970-01-01
      相关资源
      最近更新 更多