Created
September 27, 2022 14:10
-
-
Save alekrutkowski/28bce679d51fe5b96ab8b3a4042ce4b7 to your computer and use it in GitHub Desktop.
Examples of native Microsoft Office charts (with embedded data editable with Excel) generated programmatically with R and saved within a Word (docx) file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # `mschart` and `officer` R packages are required | |
| # `magrittr` required for the %>% pipe | |
| scatter_plot <- | |
| mschart::ms_scatterchart( | |
| data=iris, x="Sepal.Length", | |
| y="Sepal.Width", group="Species" | |
| ) %>% | |
| mschart::chart_settings(scatterstyle="marker") | |
| my_data <- | |
| data.frame(x=-12:12) %>% | |
| within(y <- 3*x^2 + 2*x - 1) | |
| line_plot <- | |
| mschart::ms_linechart( | |
| data=my_data, | |
| x="x", y="y" | |
| ) %>% | |
| mschart::chart_labels( | |
| title="My main title", | |
| xlab="", ylab="My y values" | |
| ) %>% | |
| mschart::set_theme( | |
| mschart::mschart_theme(legend_position='n') # none | |
| ) | |
| bar_plot <- | |
| mschart::ms_barchart( | |
| data.frame(a=c('1. one','1. one','2. two','2. two','3. three','3. three'), | |
| b=runif(6), | |
| my_group=c('first','second','first','second','first','second')), | |
| x='a', y='b', group='my_group' | |
| ) | |
| officer::read_docx() %>% | |
| mschart::body_add_chart(scatter_plot) %>% | |
| mschart::body_add_chart(line_plot) %>% | |
| mschart::body_add_chart(bar_plot) %>% | |
| print(target="example.docx") |
Author
alekrutkowski
commented
Sep 27, 2022

Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment