knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 7, warning = FALSE, message = FALSE )
library(fcaR)
Visualization is a fundamental component of Formal Concept Analysis (FCA). The lattice structure reveals hierarchical relationships and implications between data that are difficult to detect in a flat table.
fcaR provides a flexible plotting system based on ggplot2 and ggraph, as well as a powerful tool for exporting to LaTeX (TikZ) to generate high-quality figures ready for academic publication.
In this guide, we will use the classic planets dataset included in the package.
# Load the context and find concepts fc <- FormalContext$new(planets) fc$find_concepts()
The simplest way to visualize the lattice is by using the plot() method on the concepts object.
fc$concepts$plot()
By default, fcaR applies a heuristic to decide the best visualization mode based on the size of the lattice (usually reduced labeling and hierarchical layout).
fcaR implements two main algorithms to position the nodes (concepts) in space:
This is the default value (method = "sugiyama"). It arranges concepts in horizontal layers based on their generality (grade) and minimizes edge crossings. It is ideal for classic Hasse diagrams.
fc$concepts$plot(method = "sugiyama")
The force-directed layout (method = "force") simulates a physical system where nodes repel each other and edges act like springs. It is useful for very large, dense lattices or when the strict hierarchy makes the graph too wide.
fc$concepts$plot(method = "force")
Information density is a challenge in lattice visualization. fcaR offers several modes (mode) to control what information is shown on the nodes.
This is the standard in FCA literature (mode = "reduced"). To avoid redundancy:
This makes the graph easier to read: a concept possesses all attributes labeled on it and its ancestors (above), and possesses all objects labeled on it and its descendants (below).
The color code helps identify the nature of the node:
fc$concepts$plot(mode = "reduced")
For educational purposes or very small lattices, it can be useful to see the full extent (objects) and intent (attributes) in each node.
fc$concepts$plot(mode = "full")
For large lattices (> 50 concepts), text labels can overlap and obscure the structure. The "empty" mode draws only the nodes and edges.
fc$concepts$plot(mode = "empty")
For papers and scientific presentations, raster graphics (PNG, JPEG) often lose quality. fcaR allows exporting the calculated layout directly to TikZ code (vector graphics), which can be pasted into a LaTeX document.
To generate the code, use the argument to_latex = TRUE. This does not draw the plot in R, but returns a special object with the source code.
# Generate TikZ code tikz_code <- fc$concepts$plot(to_latex = TRUE) # The object prints cleanly to the console tikz_code
You can save the code directly to a .tex file using the save_tikz() function and the R pipe operator:
# Save to a local file fc$concepts$plot(to_latex = TRUE) |> save_tikz("lattice_diagram.tex")
To compile the generated code, ensure you include the following packages in your LaTeX document preamble:
\documentclass{article} \usepackage{tikz} \usetikzlibrary{shapes, arrows, positioning} \begin{document} \begin{figure}[h] \centering % You can use \input{lattice_diagram.tex} here % or paste the generated code directly: \begin{tikzpicture}... \end{tikzpicture} \caption{Concept lattice generated with fcaR.} \end{figure} \end{document}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.