1

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:

ggplot with two x variables

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 
Z.Lin
  • 28,055
  • 6
  • 54
  • 94
craigh
  • 25
  • 5
  • Please, add your code and a [reproducible example](https://stackoverflow.com/q/5963269/11570343). – cdcarrion Sep 16 '20 at 16:05
  • Thanks for taking a look -- much appreciated! Will add it to my first post, as these comments have character limits. – craigh Sep 16 '20 at 20:49
  • Your example is not reproducible. Please add necessary variables calls and try to run the example in an empty environment before posting (for example, there is no definition for gender_category_JOType in the data you provided) – cdcarrion Sep 16 '20 at 21:13
  • OK, added what I hope is valid code. Checked it in a clean environment. I think my lack of experience is showing badly here. Wish I could take an instructor-led class rather than trying to learn R on LinkedIn Learning ... it's very nice, but some things are more than the videos cover. Thanks for trying to help. – craigh Sep 16 '20 at 22:19

0 Answers0