-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Second axis with asis
mode
#84
base: master
Are you sure you want to change the base?
Conversation
asis
mode
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
8f17ab5
to
59289f9
Compare
This comment was marked as outdated.
This comment was marked as outdated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor cleanups remain
3144692
to
b87e06a
Compare
Hi @davidgohel , could you review this sometime in the future, especially if you like the general idea? Unfortunately I only got it to work with My approach for this pull request was to integrate everything without touching [edit] anything but [/edit] the Please note:
library(officer)
library(mschart)
t1 <- AirPassengers
t2 <- do.call(cbind, split(t1, cycle(t1)))
dimnames(t2) <- dimnames(.preformat.ts(t1))
t2 <- as.data.frame(t2)
t2$date <- as.Date(paste0(row.names(t2), "-01-01"))
nms <- c("date", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec")
AirPass <- t2[nms]
# bar and line on first and line on second axis
bc <- AirPass |>
ms_barchart(x = "date", y = c("Jan", "Feb"), asis = TRUE) |>
chart_ax_x(num_fmt = "[$-fr-FR]mmm yyyy") |>
chart_labels(title = "Title", xlab = "x axis",
ylab = "y axis")
sec_x <- AirPass |>
ms_linechart(x = "date", y = c("Apr", "Jul", "Oct"), asis = TRUE) |>
chart_ax_y(second_axis = TRUE)
sec_y <- AirPass |>
ms_linechart(x = "date", y = c("May", "Jun", "Sep"), asis = TRUE) |>
chart_labels(ylab = "y axis 2") |>
chart_ax_y(second_axis = TRUE)
sec_y2 <- AirPass |>
ms_barchart(x = "date", y = c("Mar", "Nov", "Dec"), asis = TRUE) |>
chart_ax_y(second_axis = TRUE)
cc <- ms_combochart(bc, sec_x, sec_y, sec_y2)
### create powerpoint
doc <- read_pptx()
doc <- add_slide(doc, layout = "Title and Content", master = "Office Theme")
doc <- ph_with(doc, value = cc, location = ph_location_fullsize())
print(doc, target = "/tmp/example.pptx") ![]() [Edit] Made minor correction above and added the powerpoint example image output. |
This comment was marked as outdated.
This comment was marked as outdated.
Alright the next revision:
I'm waiting for a review. We could add more checks or implement combo charts for the non t1 <- AirPassengers
t2 <- do.call(cbind, split(t1, cycle(t1)))
dimnames(t2) <- dimnames(.preformat.ts(t1))
t2 <- as.data.frame(t2)
t2$date <- as.Date(paste0(row.names(t2), "-01-01"))
nms <- c("date", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec")
AirPass <- t2[nms]
library(openxlsx2)
library(mschart)
### barchart with line
# bar and line on first axis
wb <- wb_workbook()$add_worksheet()$add_data(x = AirPass)
dat <- wb_data(wb, dims = "A1:M13")
bc <- dat %>%
ms_barchart(x = "date", y = c("Jan", "Feb")) %>%
chart_ax_x(num_fmt = "[$-fr-FR]mmm yyyy") %>%
chart_labels(title = "Title", xlab = "x axis",
ylab = "y axis")
sec_x <- dat %>%
ms_linechart(x = "date", y = c("Apr", "Jul", "Oct"))
cc <- ms_combochart(bc, sec_x)
wb$add_mschart(dims = "B20:L35", graph = cc)
# bar on first line on second axis
bc <- dat %>%
ms_barchart(x = "date", y = c("Jan", "Feb")) %>%
chart_ax_x(num_fmt = "[$-fr-FR]mmm yyyy") %>%
chart_labels(title = "Title", xlab = "x axis",
ylab = "y axis")
sec_y <- dat %>%
ms_linechart(x = "date", y = c("May", "Jun", "Sep")) %>%
chart_ax_y(second_axis = TRUE) %>%
chart_labels(ylab = "y axis 2") %>%
chart_theme(grid_major_line_x = FALSE, grid_major_line_y = FALSE)
cc <- ms_combochart(bc, sec_y)
wb$add_mschart(dims = "B3:L18", graph = cc)
# bar and line on first and line on second axis
bc <- dat %>%
ms_barchart(x = "date", y = c("Jan", "Feb")) %>%
chart_ax_x(num_fmt = "[$-fr-FR]mmm yyyy") %>%
chart_labels(title = "Title", xlab = "x axis",
ylab = "y axis")
sec_y2 <- dat %>%
ms_barchart(x = "date", y = c("Mar", "Nov", "Dec")) %>%
chart_ax_y(second_axis = TRUE)
cc <- ms_combochart(bc, sec_x, sec_y, sec_y2)
wb$add_mschart(dims = "B40:L55", graph = cc)
### linechart with bar
wb$add_worksheet()
lc <- dat %>%
ms_linechart(x = "date", y = c("Apr", "Jul", "Oct")) %>%
chart_ax_x(num_fmt = "[$-fr-FR]mmm yyyy") %>%
chart_labels(title = "Title", xlab = "x axis",
ylab = "y axis")
sec <- dat %>%
ms_barchart(x = "date", y = c("Jan", "Feb")) %>%
chart_ax_y(second_axis = TRUE) %>%
chart_labels(ylab = "y axis 2")
cc <- ms_combochart(lc, sec)
wb$add_mschart(dims = "B3:L18", graph = cc)
### scatterplot
wb$add_worksheet()
sc <- dat %>%
ms_scatterchart(x = "Apr", y = c("Apr", "Jul", "Oct")) %>%
chart_labels(title = "Title", xlab = "x axis",
ylab = "y axis")
sec <- dat %>%
ms_barchart(x = "date", y = c("Jan", "Feb")) %>%
chart_ax_y(second_axis = TRUE) %>%
chart_labels(ylab = "y axis 2")
sec_y <- dat %>%
ms_linechart(x = "date", y = c("May", "Jun", "Sep")) %>%
chart_ax_y(second_axis = TRUE) %>%
chart_labels(ylab = "y axis 2")
cc <- ms_combochart(sc, sec, sec_y)
wb$add_mschart(dims = "B3:L18", graph = cc)
### areachart with bar
wb$add_worksheet()
ac <- dat %>%
ms_areachart(x = "date", y = c("Apr", "Jul", "Oct")) %>%
chart_ax_x(num_fmt = "[$-fr-FR]mmm yyyy") %>%
chart_labels(title = "Title", xlab = "x axis",
ylab = "y axis")
sec <- dat %>%
ms_barchart(x = "date", y = c("Jan", "Feb")) %>%
chart_ax_y(second_axis = TRUE) %>%
chart_labels(ylab = "y axis 2")
cc <- ms_combochart(ac, sec)
wb$add_mschart(dims = "B3:L18", graph = cc)
unlink("/tmp/combo_charts.xlsx")
wb$save("/tmp/combo_charts.xlsx") |
### second x axis
wb$add_worksheet()
dat1 <- wb_data(wb, sheet = 1, dims = "A1:M6")
dat2 <- wb_data(wb, sheet = 1, dims = "A1:M1;A7:M13")
ac <- dat1 %>%
ms_areachart(x = "date", y = c("Apr", "Jul", "Oct")) %>%
chart_ax_x(num_fmt = "[$-fr-FR]mmm yyyy") %>%
chart_labels(title = "Title", xlab = "x axis",
ylab = "y axis")
sec <- dat2 %>%
ms_barchart(x = "date", y = c("Jan", "Feb")) %>%
chart_ax_x(num_fmt = "[$-fr-FR]mmm yyyy", second_axis = TRUE) %>%
chart_labels(xlab = "x axis 2")
cc <- ms_combochart(ac, sec)
wb$add_mschart(dims = "B3:L18", graph = cc) |
Pushed examples and documentation. There might still be some cornerstones e.g. secondary x and y axis at the same time does not work as expected, but likely nobody wants a second x axis anyways. library(officer)
library(mschart)
t1 <- AirPassengers
t2 <- do.call(cbind, split(t1, cycle(t1)))
dimnames(t2) <- dimnames(.preformat.ts(t1))
t2 <- as.data.frame(t2)
t2$date <- as.Date(paste0(row.names(t2), "-01-01"))
nms <- c("date", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec")
AirPass <- t2[nms]
# bar and line on first and line on second axis
fst_x <- AirPass |>
ms_barchart(x = "date", y = c("Jan"), asis = TRUE) |>
chart_ax_x(num_fmt = "[$-fr-FR]mmm yyyy") |>
chart_labels(title = "Title", xlab = "x axis",
ylab = "y axis")
fst_x2 <- AirPass |>
ms_linechart(x = "date", y = c("Jan"), asis = TRUE)
sec_x <- AirPass |>
ms_barchart(x = "date", y = c("Feb"), asis = TRUE) |>
chart_ax_x(num_fmt = "[$-fr-FR]mmm yyyy", second_axis = TRUE) |>
chart_labels(xlab = "x axis 2")
sec_x2 <- AirPass |>
ms_linechart(x = "date", y = c("Feb"), asis = TRUE) |>
chart_ax_x(second_axis = TRUE)
sec_y <- AirPass |>
ms_linechart(x = "date", y = c("Mar"), asis = TRUE) |>
chart_labels(ylab = "y axis 2") |>
chart_ax_y(second_axis = TRUE)
sec_y2 <- AirPass |>
ms_barchart(x = "date", y = c("Mar"), asis = TRUE) |>
chart_ax_y(second_axis = TRUE)
# cc <- ms_combochart(fst_x, sec_x, sec_y)
# cc <- ms_combochart(fst_x, fst_x2, sec_x, sec_x2)
cc <- ms_combochart(fst_x, fst_x2, sec_y, sec_y2)
# cc <- ms_combochart(fst_x, fst_x2, sec_x, sec_x2, sec_y, sec_y2)
### create powerpoint
doc <- read_pptx()
doc <- add_slide(doc, layout = "Title and Content", master = "Office Theme")
doc <- ph_with(doc, value = cc, location = ph_location_fullsize())
print(doc, target = "/tmp/example.pptx") |
c21ba59
to
be726a1
Compare
The first birthday of this PR is approaching. I'm in no rush and have been using this branch in production for quite a while, but please let me know if there is anything I can do to speed up the review process. |
Because I have a couple of cases where I need a secondary y axis. This currently only works with
asis
data.And I have tested only this single case down below.This fixes #9Works quite well.
I have added a secondary argument. This way it is possible to treat the secondary plot as if it is a normal plot.This code should only impact cases where a second plot is added. I'm not sure how to implement this with the basicasis = FALSE
mschart output. It might need some glue code to patch everything together, but for now I do not care about this case.