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.
Names of these high level functions are all prefixed with ms_
. The following charts are the only available from all possible MS charts:
ms_barchart()
ms_linechart()
ms_scatterchart()
ms_areachart()
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 plottedx
, y
group
: colnames for x and y axis and a grouping columnmy_barchart <- ms_barchart(data = browser_data,
x = "browser", y = "value", group = "serie")
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
Charts are generated with default values. Options are available to change charts properties.
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 )
chart_ax_x()
and chart_ax_y()
: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)
chart_labels()
:my_barchart <- chart_labels(my_barchart, title = "A main title",
xlab = "x axis title", ylab = "y axis title")
chart_data_fill()
, chart_data_stroke()
, chart_data_size()
, chart_data_symbol()
and chart_data_line_width()
:my_barchart <- chart_data_fill(my_barchart,
values = c(serie1 = "#003C63", serie2 = "#ED1F24", serie3 = "#F2AA00") )
my_barchart <- chart_data_stroke(my_barchart, values = "transparent" )
set_theme()
. See vignette details.