Skip to content

Commit

Permalink
solve problem with plot method
Browse files Browse the repository at this point in the history
  • Loading branch information
FloSchuberth committed Feb 7, 2025
1 parent f71e8b6 commit 6ea5289
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 72 deletions.
20 changes: 0 additions & 20 deletions R/helper_plot.cSEMResults.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,26 +184,6 @@ secondOrderMeasurementEdges <- function(
#'
#' Build DOT code from common components.
#'
#'
#'@usage buildDotCode(
#'title,
#'graph_attrs,
#'constructs,
#'r2_values,
#'measurement_edge_fun,
#'path_coefficients,
#'path_p_values,
#'correlations,
#'plot_significances,
#'plot_indicator_correlations,
#'plot_structural_model_only,
#'is_second_order = FALSE
#')
#'
#'
#' @inheritParams csem_arguments
#'
#' @return Character string. .
#'
#' @keywords internal
#'
Expand Down
48 changes: 27 additions & 21 deletions R/plot.cSEMResult.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@
#' Creates a plot of a `cSEMResults` object using the \link[DiagrammeR]{grViz} function.
#'
#' @inheritParams csem_arguments
#' @param x An R object of class `cSEMResults_default` object.
#' @param ... Currently ignored.
#'
#'
#'
#' @seealso [save.cSEMPlot_single()] [csem()], [cSEMResults], \link[DiagrammeR]{grViz}
#' @seealso [savePlot()] [csem()], [cSEMResults], \link[DiagrammeR]{grViz}
#'
#' @example inst/examples/example_plot.cSEMResults.R
#'
#'
#' @export

plot.cSEMResults_default <- function(
.object = NULL,
x = NULL,
.title = args_default()$.title,
.plot_significances = args_default()$.plot_significances,
.plot_indicator_correlations = args_default()$.plot_indicator_correlations,
.plot_structural_model_only = args_default()$.plot_structural_model_only,
.graph_attrs = args_default()$.graph_attrs
.graph_attrs = args_default()$.graph_attrs,
...
){

## Install DiagrammeR if not already installed
Expand All @@ -30,16 +32,16 @@ plot.cSEMResults_default <- function(
"Package `DiagrammeR` is required. Use `install.packages(\"DiagrammeR\")` and rerun.")
}

results <- summarize(.object)
constructs <- .object$Information$Model$construct_type # named vector of construct types
results <- summarize(x)
constructs <- x$Information$Model$construct_type # named vector of construct types
r2_values <- results$Estimates$R2
weights <- as.data.frame(.object$Estimates$Weight_estimates)
loadings <- as.data.frame(.object$Estimates$Loading_estimates)
weights <- as.data.frame(x$Estimates$Weight_estimates)
loadings <- as.data.frame(x$Estimates$Loading_estimates)
weight_p_values <- results$Estimates$Weight_estimates$p_value
names(weight_p_values) <- results$Estimates$Weight_estimates$Name
loading_p_values <- results$Estimates$Loading_estimates$p_value
names(loading_p_values) <- results$Estimates$Loading_estimates$Name
path_coefficients <- as.data.frame(.object$Estimates$Path_estimates)
path_coefficients <- as.data.frame(x$Estimates$Path_estimates)
path_p_values <- results$Estimates$Path_estimates$p_value
ind_corr <- list(names = results$Estimates$Indicator_correlation$Name,
estimates = results$Estimates$Indicator_correlation$Estimate,
Expand Down Expand Up @@ -80,7 +82,8 @@ plot.cSEMResults_default <- function(
#' Creates a plot of a `cSEMResults` object using the \link[DiagrammeR]{grViz} function.
#'
#' @inheritParams csem_arguments
#'
#' @param x An R object of class `cSEMResults_multi` object.
#' @param ... Currently ignored.
#'
#' @seealso [csem()], [cSEMResults], \link[DiagrammeR]{grViz}
#'
Expand All @@ -89,13 +92,14 @@ plot.cSEMResults_default <- function(
#' @export

plot.cSEMResults_multi <- function(
.object = NULL,
x = NULL,
.title = args_default()$.title,
.plot_significances = args_default()$.plot_significances,
.plot_indicator_correlations = args_default()$.plot_indicator_correlations,
.plot_structural_model_only = args_default()$.plot_structural_model_only,
.graph_attrs = args_default()$.graph_attrs
) {
.graph_attrs = args_default()$.graph_attrs,
...
){
plots <- Map(function(group_name, group_object) {
group_title <- if (.title == "") paste0("Group_", group_name) else paste0(.title, " Group_", group_name)
plot(group_object,
Expand All @@ -104,7 +108,7 @@ plot.cSEMResults_multi <- function(
.plot_indicator_correlations = .plot_indicator_correlations,
.plot_structural_model_only = .plot_structural_model_only,
.graph_attrs = .graph_attrs)
}, names(.object), .object)
}, names(x), x)

class(plots) <- c("cSEMPlot_multi",class(plots))
return(plots)
Expand All @@ -118,27 +122,29 @@ plot.cSEMResults_multi <- function(
#' Creates a plot of a `cSEMResults` object using the \link[DiagrammeR]{grViz} function.
#'
#' @inheritParams csem_arguments
#'
#' @param x An R object of class `cSEMResults_2ndorder` object.
#' @param ... Currently ignored.
#'
#' @seealso [csem()], [cSEMResults], \link[DiagrammeR]{grViz}
#'
#' @example inst/examples/example_plot.cSEMResults.R
#'
#' @export
plot.cSEMResults_2ndorder <- function(
.object,
x,
.title = args_default()$.title,
.plot_significances = args_default()$.plot_significances,
.plot_indicator_correlations = args_default()$.plot_indicator_correlations,
.plot_structural_model_only = args_default()$.plot_structural_model_only,
.graph_attrs = args_default()$.graph_attrs
.graph_attrs = args_default()$.graph_attrs,
...
){

# Extract first– and second–stage models and summaries.
fs <- .object$First_stage
ss <- .object$Second_stage
results_fs <- summarize(.object)$First_stage
results_ss <- summarize(.object)$Second_stage
fs <- x$First_stage
ss <- x$Second_stage
results_fs <- summarize(x)$First_stage
results_ss <- summarize(x)$Second_stage

# Merge construct types from first– and second–stage.
ct_first <- fs$Information$Model$construct_type
Expand Down
5 changes: 3 additions & 2 deletions R/postestimate_savePlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
#' .plot_object,
#' .file)
#'
#' @param .plot_object Object returned by the [plotModel()] function.
#' @param .plot_object Object returned by one of the following functions [plot.cSEMResults_default()], [plot.cSEMResults_multi()], or
#' [plot.cSEMResults_2ndorder()].
#' @param .file Character string. The name of the file to save the plot to (supports 'pdf', 'png', 'svg', and 'dot' formats).
#'
#' @return NULL
#'
#' @seealso [plot.cSEM_default()]
#' @seealso [plot.cSEMResults_default()] [plot.cSEMResults_multi()] [plot.cSEMResults_2ndorder()]
#'
#' @example inst/examples/example_plot.cSEMResults.R
#'
Expand Down
18 changes: 1 addition & 17 deletions man/cleanNode.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions man/plot.cSEMResults_2ndorder.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions man/plot.cSEMResults_default.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions man/plot.cSEMResults_multi.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions man/savePlot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6ea5289

Please sign in to comment.