I need to convert a ggplot that has two x variables to mschart so that I can work with it in Word. I used to produce these in Excel, but we are moving to R for stats. Here's what it looks like in ggplot2:
In ggplot2, I just pass the x variable as interaction('Gender','JO_Type'). When I try to pass this to mschart, it returns this error message:
x %in% names(data) is not TRUE
I have tried converting the data to an array and vector, but no luck. I am not even sure mscharts can handle a two-dimensional x argument.
Data:
gender_category_JOType = structure(list(JO_Type = c("Standard"  ,"Standard","Standard","Standard","Standard","Standard","Standard","Standard","Continuous","Continuous","Continuous","Continuous","Continuous","Continuous","Continuous","Continuous"), 
               Category = c("FS","FS","P1-P4","P1-P4","P5-P6","P5-P6","D1","D1","FS","FS","P1-P4","P1-P4","P5-P6","P5-P6","D1","D1"), 
               Gender = c("Female","Male","Female","Male","Female","Male","Female","Male","Female","Male","Female","Male","Female","Male","Female","Male"), 
               count = c(76,144,668,697,173,305,61,110,514,1214,264,504,46,130,18,41),
               percentage=c("34.5%","65.5%","48.9%","51.1%","36.2%","63.8%","35.7%","64.3%","29.7%","70.3%","34.4%","65.6%","26.1%","73.9%","30.5%","69.5%")), row.names = c(1:16), class = "data.frame")
Code:
library(tidyverse)
library(magrittr)
library(mschart)
library(officer)
# the data for gender_category_JOType
print(gender_category_JOType)
# ggplot function
myBarChart_3 <- function(data,var1,var2,var3,count,title,xLabel,yLabel){
  ggplot(data, 
         aes_string(x=var1, y=count, fill=var2)) +
    ggtitle(title) +
    geom_bar(stat = 'identity',width=1.15,
             position = position_dodge2(padding=0.15, reverse=FALSE, preserve=c("single"))) +
    geom_text(aes(label=count),
              vjust=-.5, position=position_dodge2(width=1.15), size=2.5) +
    scale_y_continuous(sec.axis=waiver(),
                       expand = expansion(mult = c(0,0.05))) +
    facet_wrap(var3, nrow=1,strip.position="bottom",scales = "free_x")  +
    xlab(xLabel) +
    ylab(yLabel) 
}
myBarChart_3(gender_category_JOType, interaction('Gender','JO_Type'), 'Category',
             c('JO_Type','Gender'), 'count', "Category, Gender, and JO Type", 
             "level", "total")# +
  #geom_text(aes(label = percentage), vjust=-1.75, hjust='center', 
  #          position=position_dodge2(width=1.15), size=2.5)
# the msbarchart version
# add the chart to an existing Word doc
gen_docx <- function(chart,file,file2){
  doc <- read_docx()
  doc <- body_add_par(doc, " ", style = "Normal", pos = "after")
  doc <- body_add_chart(doc, chart = chart, style = "Normal", pos="after")
  doc <- body_add_break(doc, pos="after")
  doc <- body_add_par(doc, " ", style = "Normal", pos = "after")
  doc <- body_add_par(doc, " ", style = "Normal", pos = "after")
  doc <- body_add_docx(doc, src = file2, pos = "before")
  doc <- print(doc, target = file)
}
# create the chart function
my_msbarchart_doc <- function(srcFile, docName, chartName, data, var, count, 
                              grouping, title, xLabel, yLabel){
  chartName <- ms_barchart(data, x = var, y = count, group = grouping) %>%
    chart_labels(title = title, xlab = xLabel, ylab = yLabel)
  chartName <- chart_data_labels(chartName, position="outEnd", show_val = T)
  doc <- gen_docx(chartName, paste0(docName,".docx"), paste0(srcFile,".docx"))
}
# create the chart
lion <- my_msbarchart_doc("myDocument2", "myDocument3", lion, gender_category_JOType,
                          interaction('Gender','JO_Type'), "count", "Category", 
                          "Category, JO Type, and Gender", "Gender and JO Type","count")
# error msg caused by the interaction that works in the ggplot but not in mschart:   x %in% names(data) is not TRUE 

 
    