Plotting in {EGAnet} has become much more flexible by passing on
most arguments to {GGally}’s ggnet2 (some arguments might
not change to keep a consistent {EGAnet} style to the plots). This Wiki
walks through some of the flexibility available in {EGAnet}’s plots.
This demonstration is only the start – you can use these plots as the
foundation for your own creations. These examples focus solely on
EGA plotting but apply to all *EGA plots.
# Load necessary packages
library(EGAnet); library(GGally); library(ggplot2)
# Estimate EGA
ega.wmt <- EGA(wmt2[,7:24], plot.EGA = FALSE)
# Plot
plot(ega.wmt)
plot(ega.wmt, legend.names = c("Dimension 1", "Dimension 2"))
plot(ega.wmt) + theme(legend.position = "none")
Handling legend title and text can be done using the standard
?ggplot2::theme arguments
plot(ega.wmt) + ggtitle("EGA Title")
plot(ega.wmt) +
ggtitle("Better EGA Title") +
theme(plot.title = element_text(size = 14, face = "bold", hjust = 0.5))
For all available layout options, see
?sna::gplot.layout. Remove gplot.layout. to
use the layout (e.g., gplot.layout.circle =
layout = "circle")
plot(ega.wmt, layout = "circle")
plot(ega.wmt, layout = "eigen")
Change Colors Using One of {EGAnet}’s Own Palettes (see
?color_palette_EGA for the full list —
"polychrome", "blue.ridge1",
"blue.ridge2", "rio", "itacare",
"grayscale", and "rainbow")
plot(ega.wmt, color.palette = "blue.ridge2")
Change Colors Using an {RColorBrewer} Palette (any
palette name from RColorBrewer::brewer.pal.info works)
plot(ega.wmt, color.palette = "Set2")
Change Colors Using HEX Codes (one per dimension — handy for matching institutional or journal color schemes)
plot(ega.wmt, node.color = c("#F5815A", "#47BCC9"))
For quick color palette ideas, check out this color palette generator
plot(ega.wmt, node.size = 12)
Change Size based on Node Strength
plot(ega.wmt, node.size = colSums(ega.wmt$network)^2 * 16)
plot(ega.wmt, node.alpha = 0.5)
By default, nodes are labeled with their column names, but any character vector of the same length can be used instead — useful for swapping in full item wording or a different item numbering scheme:
plot(ega.wmt, label = paste0("Item ", 1:18))
plot(ega.wmt, label = FALSE)
plot(ega.wmt, label.size = 6, label.color = "white")
plot(ega.wmt, edge.size = 12)
plot(ega.wmt, edge.alpha = 0.1)
plot(
ega.wmt, edge.alpha = 1,
edge.lty = c( # line type
"dashed", # positive edge first
"solid" # negative edge second
),
edge.color = c( # color
"pink", # positive edges first
"black" # negative edges second
)
)
plot(
ega.wmt,
edge.color = c(
"darkgreen", # positive edges (default color)
"white" # negative edges second
)
)
plot(
ega.wmt, edge.alpha = 1, # used to increase visibility
edge.color = c(
"white", # positive edges first
"red" # negative edges (default color)
)
)
Every argument above is just one more
ggnet2/ggplot2 layer, so they compose freely
into a single call:
plot(
ega.wmt,
color.palette = "itacare",
node.size = 14, node.alpha = 0.9,
label.size = 4, label.color = "white",
edge.alpha = 0.6
) +
ggtitle("WMT-2 Structure") +
theme(plot.title = element_text(hjust = 0.5, face = "bold"))
compare.EGA.plots (used elsewhere to check bootEGA stability) is itself a
plotting function, and takes the same kind of labeling arguments to keep
multiple structures readable side by side:
ega.wmt.spinglass <- EGA(wmt2[,7:24], algorithm = "spinglass", plot.EGA = FALSE)
comparison <- compare.EGA.plots(
ega.wmt, ega.wmt.spinglass,
labels = c("Walktrap", "Spinglass")
)
Every plot above is a ggplot2 object, so
ggsave handles exporting it — to PNG, PDF, SVG, or anything
else it supports — at whatever size and resolution you need for a
manuscript or slide deck:
my_plot <- plot(ega.wmt, color.palette = "blue.ridge2")
ggsave("wmt-structure.png", my_plot, width = 6, height = 5, dpi = 300)