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(doc, value = chart, location = ph_location_type(type = "body"))
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
.
my_barchart_01 <- ms_barchart(
data = browser_data, x = "browser",
y = "value", group = "serie"
)
gen_pptx(my_barchart_01, file = "assets/pptx/gallery_bar_01.pptx")
Download file gallery_bar_01.pptx - view with office web viewer
as_bar_stack()
is an helper function.
my_barchart_02 <- as_bar_stack(my_barchart_01)
gen_pptx(my_barchart_02, file = "assets/pptx/gallery_bar_02.pptx")
Download file gallery_bar_02.pptx - view with office web viewer
my_barchart_03 <- as_bar_stack(my_barchart_01, dir = "horizontal")
gen_pptx(my_barchart_03, file = "assets/pptx/gallery_bar_03.pptx")
Download file gallery_bar_03.pptx - view with office web viewer
my_barchart_04 <- as_bar_stack(my_barchart_01, dir = "horizontal",
percent = TRUE)
gen_pptx(my_barchart_04, file = "assets/pptx/gallery_bar_04.pptx")
Download file gallery_bar_04.pptx - view with office web viewer
data <- structure(
list(
supp = structure( c(1L, 1L, 1L, 2L, 2L, 2L),
.Label = c("OJ", "VC"), class = "factor" ),
dose = c(0.5, 1, 2, 0.5, 1, 2),
length = c(13.23, 22.7, 26.06, 7.98, 16.77, 26.14)
),
.Names = c("supp", "dose", "length"),
class = "data.frame", row.names = c(NA,-6L)
)
lc_01 <- ms_linechart(data = data, x = "dose",
y = "length", group = "supp")
gen_pptx(lc_01, file = "assets/pptx/gallery_line_01.pptx")
Download file gallery_line_01.pptx - view with office web viewer
Auto detection of dates is not yet implemented, sorry. You will need to specify the num_fmt
argument.
lc_02 <- ms_linechart(data = browser_ts, x = "date",
y = "freq", group = "browser")
lc_02 <- chart_ax_x(lc_02, num_fmt = "m/d/yy")
gen_pptx(lc_02, file = "assets/pptx/gallery_line_02.pptx")
Download file gallery_line_02.pptx - view with office web viewer
lc_03 <- chart_settings(lc_02, grouping = "percentStacked")
gen_pptx(lc_03, file = "assets/pptx/gallery_line_03.pptx")
Download file gallery_line_03.pptx - view with office web viewer
#### Smoothed lineslinec <- ms_linechart(data = iris, x = "Sepal.Length",
y = "Sepal.Width", group = "Species")
linec_smooth <- chart_data_smooth(linec,
values = c(virginica = 1, versicolor = 0, setosa = 0) )
gen_pptx(linec_smooth, file = "assets/pptx/gallery_line_04.pptx")
Download file gallery_line_04.pptx - view with office web viewer
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
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
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(my_bc_01, location = ph_location_fullsize()) %>%
add_slide(layout, master) %>%
ph_with(my_bc_02, location = ph_location_fullsize()) %>%
add_slide(layout, master) %>%
ph_with(my_sc_01, location = ph_location_fullsize()) %>%
add_slide(layout, master) %>%
ph_with(my_sc_02, location = ph_location_fullsize()) %>%
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.
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
:
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