Introduction to mschart

library(mschart)
library(officer)
library(magrittr)

Package mschart lets R users to create Microsoft Office charts from data, and then add title, legends, and annotations to the chart object.

Available charts

Names of these high level functions are all prefixed with ms_. The following charts are the only available from all possible MS charts:

More chart types will be made available in future versions.

Quick start

Call an high level function

We need to call an high level function (one of those starting with ms_). These functions accept the following arguments:

my_barchart <- ms_barchart(data = browser_data, 
  x = "browser", y = "value", group = "serie")

Write charts in Microsoft documents

Let’s have a look at the result by sending it to a new PowerPoint presentation:

library(officer)
doc <- read_pptx()
doc <- add_slide(doc, layout = "Title and Content", master = "Office Theme")
doc <- ph_with_chart(doc, chart = my_barchart)
print(doc, target = "assets/pptx/barchart_01_stacked.pptx")

Download file barchart_01_stacked.pptx - view with office web viewer

Or in a new Word document:

doc <- read_docx()
doc <- body_add_chart(doc, chart = my_barchart, style = "centered")
print(doc, target = "assets/docx/barchart_01_stacked.docx")

Download file barchart_01_stacked.docx - view with office web viewer

Customise charts

Charts are generated with default values. Options are available to change charts properties.

my_barchart <- chart_settings( my_barchart, grouping = "stacked", gap_width = 50, overlap = 100 )
my_barchart <- chart_ax_x(my_barchart, cross_between = 'between', 
  major_tick_mark = "in", minor_tick_mark = "none")
my_barchart <- chart_ax_y(my_barchart, num_fmt = "0.00", rotation = -90)
my_barchart <- chart_labels(my_barchart, title = "A main title", 
  xlab = "x axis title", ylab = "y axis title")
my_barchart <- chart_data_fill(my_barchart,
  values = c(serie1 = "#003C63", serie2 = "#ED1F24", serie3 = "#F2AA00") )
my_barchart <- chart_data_stroke(my_barchart, values = "transparent" )
title_style <- fp_text(color = "#175FAA", font.size = 14, bold = TRUE)
mytheme <- mschart_theme(main_title = update( title_style, font.size = 18),
  axis_title_x = title_style, axis_title_y = title_style,
  grid_major_line_y = fp_border(width = 1, color = "orange"),
  axis_ticks_y = fp_border(width = 1, color = "orange") )
my_barchart <- set_theme(my_barchart, mytheme)
library(officer)
doc <- read_pptx()
doc <- add_slide(doc, layout = "Title and Content", master = "Office Theme")
doc <- ph_with_chart(doc, chart = my_barchart)
print(doc, target = "assets/pptx/barchart_02_stacked.pptx")

Download file barchart_02_stacked.pptx - view with office web viewer

Area chats

ac_01 <- ms_areachart(data = data, x = "dose",
                      y = "length", group = "supp")
gen_pptx(ac_01, file = "assets/pptx/gallery_area_01.pptx")

Download file gallery_area_01.pptx - view with office web viewer

ac_02 <- ms_areachart(data = browser_ts, x = "date",
                      y = "freq", group = "browser")
ac_02 <- chart_ax_y(ac_02, cross_between = "between", num_fmt = "General")
ac_02 <- chart_ax_x(ac_02, cross_between = "midCat", num_fmt = "m/d/yy")
gen_pptx(ac_02, file = "assets/pptx/gallery_area_02.pptx")

Download file gallery_area_02.pptx - view with office web viewer

ac_03 <- chart_settings(ac_02, grouping = "percentStacked")
gen_pptx(ac_03, file = "assets/pptx/gallery_area_03.pptx")

Download file gallery_area_03.pptx - view with office web viewer

Scatter plots

sc_01 <- ms_scatterchart(data = mtcars, x = "disp",
                         y = "drat", group = "gear")
gen_pptx(sc_01, file = "assets/pptx/gallery_scatter_01.pptx")

Download file gallery_scatter_01.pptx - view with office web viewer

sc_02 <- ms_scatterchart(data = mtcars, x = "disp",
                         y = "drat", group = "gear")
gen_pptx(sc_02, file = "assets/pptx/gallery_scatter_02.pptx")

Download file gallery_scatter_02.pptx - view with office web viewer