Code Listing L2.
Generating a plot of Mplus model-estimated item frequencies from a latent class analysis using output extracted by the readModels function
1 library(MplusAutomation); library(reshape2) 2 library(ggplot2); library(scales); library(cowplot) 3 4 lcacfa <- readModels(“templateExample”, recursive = TRUE) 5 6 p <- as.data.frame(lcacfa[[2]][[“gh5”]][[“means_and_variances_data”]][[ 7 “estimated_probs”]][[“values”]][seq(2, 12, 2), ]) 8 colnames(p) <- paste0(“Class “, 1:2) 9 p <- cbind(Var = paste0(“U”, 1:6), p) 10 pl <- melt(p, id.vars = “Var”) 11 12 ggplot(pl, aes(as.integer(Var), value, 13 shape = variable, colour = variable)) + 14 geom_point(size = 3) + 15 scale_x_continuous(“”, breaks = 1:6, labels = p$Var) + 16 scale_y_continuous(“Percent of 1s (vs 0s)”, labels = percent) + 17 scale_colour_manual(values = c(“Class 1”=“black”, “Class 2”=“grey50”)) + 18 theme_cowplot() + 19 theme(legend.key.width = unit(1.5, “cm”), 20 legend.title = element_blank(), 21 legend.position = “bottom”) + 22 coord_cartesian(xlim = c(.9, 6.1), ylim = c(0, 1), expand = FALSE) |
Note. Lines 1 and 2 load several R packages for data management and graphing. The code for the graph (lines 12–22) is the most complex, although a basic graph could be generated with just lines 12 to 14. The remaining lines customize the color, shapes, labels, and axis range of the graph.