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:

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:

  • data: the data.frame to be plotted
  • x, y
  • eventually group: colnames for x and y axis and a grouping column

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(doc, value = my_barchart, location = ph_location_fullsize())
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 = "Normal")
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.

  • Global chart settings can be specified with generic function chart_settings(). Each type of chart has its own set of parameters. See vignette details.

The following stack the bars for each group.

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)
  • Titles (main title, axis labels) can be set with function chart_labels():
my_barchart <- chart_data_fill(my_barchart,
  values = c(serie1 = "#003C63", serie2 = "#ED1F24", serie3 = "#F2AA00") )
my_barchart <- chart_data_stroke(my_barchart, values = "transparent" )
  • Global visual settings (e.g. groups fill and stroke colours) can be customised with functions set_theme(). See vignette details.