View source: R/xlsx_add_chart.R
| sheet_add_drawing.ms_chart | R Documentation |
Add an ms_chart object to a sheet in an xlsx workbook
created with officer::read_xlsx(). The chart data is written into
the sheet and the chart is displayed at the specified position.
## S3 method for class 'ms_chart'
sheet_add_drawing(
x,
value,
sheet,
start_col = 1L,
start_row = 1L,
write_data = TRUE,
left = 1,
top = 1,
width = 6,
height = 4,
anchor = NULL,
edit_as = c("twoCell", "oneCell", "absolute"),
...
)
x |
an rxlsx object (created by |
value |
an |
sheet |
sheet name where the chart and its data will be placed.
The sheet must already exist (see |
start_col |
column index where chart data will be written
(default 1, i.e. column A). When |
start_row |
row index where chart data will be written
(default 1). Same semantics with |
write_data |
if |
left, top |
top-left anchor of the chart, in inches.
Defaults to |
width, height |
size of the chart, in inches. Defaults
to |
anchor |
optional Excel cell-based anchor. Either |
edit_as |
one of |
... |
unused |
the rxlsx object (invisibly)
library(officer)
library(mschart)
my_chart <- ms_barchart(
data = data.frame(
x = c("A", "B", "C"),
y = c(1, 3, 2),
group = rep("serie1", 3)
),
x = "x", y = "y", group = "group"
)
x <- read_xlsx()
x <- add_sheet(x, label = "chart_sheet")
x <- sheet_add_drawing(x, value = my_chart, sheet = "chart_sheet")
print(x, target = tempfile(fileext = ".xlsx"))
# Sharing one dataset between several charts on the same sheet:
# write the data once, then add each chart with write_data = FALSE.
shared <- data.frame(
x = c("A", "B", "C"),
y = c(1, 3, 2),
group = rep("serie1", 3)
)
chart_a <- ms_barchart(shared, x = "x", y = "y", group = "group")
chart_b <- ms_linechart(shared, x = "x", y = "y", group = "group")
x <- read_xlsx()
x <- add_sheet(x, label = "multi")
x <- sheet_write_data(x, value = chart_a$data_series, sheet = "multi")
x <- sheet_add_drawing(x, value = chart_a, sheet = "multi",
write_data = FALSE,
left = 3, top = 0.5, width = 5, height = 3.5)
x <- sheet_add_drawing(x, value = chart_b, sheet = "multi",
write_data = FALSE,
left = 9, top = 0.5, width = 5, height = 3.5)
print(x, target = tempfile(fileext = ".xlsx"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.