Details

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

Chart types

In order to make to code shorter, we will use the following function to generate a PowerPoint document from a chart.

gen_pptx <- function( chart, file ){
  doc <- read_pptx()
  doc <- add_slide(doc, layout = "Title and Content", master = "Office Theme")
  doc <- ph_with_chart(doc, chart = chart)
  print(doc, target = file)
  office_doc_link( url = paste0( "https://ardata-fr.github.io/mschart/articles/", file ) )
}

The following are the chart available in package mschart.

Bar charts

Line charts

Download file gallery_line_01.pptx - view with office web viewer

Area chats

Download file gallery_area_01.pptx - view with office web viewer

Download file gallery_area_02.pptx - view with office web viewer

Download file gallery_area_03.pptx - view with office web viewer

Scatter plots

Download file gallery_scatter_01.pptx - view with office web viewer

Download file gallery_scatter_02.pptx - view with office web viewer

Chart settings

Chart settings are general options relative to the graphic layout, such as grouping options (stacked, clustered, etc.) or scatter plot style (markers only, lines and markers, etc.).

chart_settings() is a generic function, each type of chart has its own method.

The following examples are illustrating existing chart types:

my_bc_01 <- ms_barchart(
    data = browser_data, x = "browser", y = "value", group = "serie") %>% 
  chart_settings( dir = "vertical", grouping = "stacked", overlap = 100 )

my_bc_02 <- ms_barchart(
    data = browser_data, x = "browser", y = "value", group = "serie") %>% 
  chart_settings( dir = "vertical", grouping = "clustered", 
                  gap_width = 400, overlap = -100 )

my_sc_01 <- ms_scatterchart(
  data = mtcars, x = "disp", y = "drat") %>% 
  chart_settings(scatterstyle = "marker")

my_sc_02 <- ms_scatterchart(
  data = mtcars, x = "disp", y = "drat") %>% 
  chart_settings(scatterstyle = "lineMarker")
layout <- "Title and Content"
master <- "Office Theme"
read_pptx() %>% 
  add_slide(layout, master) %>% ph_with_chart(chart = my_bc_01) %>% 
  add_slide(layout, master) %>% ph_with_chart(chart = my_bc_02) %>% 
  add_slide(layout, master) %>% ph_with_chart(chart = my_sc_01) %>% 
  add_slide(layout, master) %>% ph_with_chart(chart = my_sc_02) %>% 
  print(target = "assets/pptx/chart_settings_01.pptx")

Download file theme_01.pptx - view with office web viewer

As bar charts can be complex to configure, helper function barchart_options() is provided.

Theme

A theme is a set of parameters (i.e. colors, fonts) that can be applied to a chart. As for ggplot objects, a theme is controling the appearance of non-data components of the plot.

Let’s first create a chart:

my_bc <- ms_barchart(
  data = browser_data, x = "browser", y = "value", group = "serie") %>% 
  as_bar_stack( ) %>% 
  chart_labels(title = "Title example", xlab = "x label", ylab = "y label")

… and add that chart in a PowerPoint file with officer:

gen_pptx(my_bc, file = "assets/pptx/theme_00.pptx")

mschart is providing an interface to themes with function set_theme(). This function is using an object produced by mschart_theme(). The following are the supported arguments of the function:

mytheme <- mschart_theme(
  axis_title = fp_text(color = "#0D6797", font.size = 20, italic = TRUE),
  main_title = fp_text(color = "#0D6797", font.size = 24, bold = TRUE),
  grid_major_line = fp_border(color = "#AA9961", style = "dashed"),
  axis_ticks = fp_border(color = "#AA9961") 
  )
my_bc <- set_theme(my_bc, mytheme)
gen_pptx(my_bc, file = "assets/pptx/theme_01.pptx")

Download file theme_01.pptx - view with office web viewer

Some elements are inheriting properties from other theme elements. For example, axis_title_x and axis_title_y inherit from axis_title and are all expecting a fp_text; grid_major_line_x and grid_major_line_y inherit from grid_major_line and are all expecting a fp_border.

An helper function is provided to allow modifications of elements of the chart theme:

my_bc <- chart_theme( x = my_bc, 
  axis_title_x = fp_text(color = "red", font.size = 11),
  main_title = fp_text(color = "red", font.size = 15),
  legend_position = "t" )
gen_pptx(my_bc, file = "assets/pptx/theme_02.pptx")

Download file theme_02.pptx - view with office web viewer