关于在R包中修改文献计量图为黑白配色(含背景与文本)的技术求助
I get it—tweaking the color scheme of bibliometric plots from the bibliometrix package for black-and-white use (like for publications or formal presentations) can feel tricky when you don’t know the right syntax. Let’s break down two straightforward approaches to adjust your plot’s body, background, and text colors using your existing code.
Key Context
The conceptualStructure() function relies on the factoextra package under the hood to generate its visualizations. This means we can use both its built-in parameters and ggplot2’s theming tools to fully customize the color palette.
Approach 1: Return the Plot Object & Customize with ggplot2
First, we’ll tell conceptualStructure() to return the plot object instead of rendering it immediately, then modify it with ggplot2 for granular control:
# Load required packages (if not already loaded) library(bibliometrix) library(ggplot2) # Run conceptualStructure() and store the plot object instead of plotting it p <- conceptualStructure(M, field = "ID", method = "MCA", quali.supp = NULL, quanti.supp = NULL, minDegree = 2, clust = "auto", k.max = 5, stemming = FALSE, labelsize = 10, documents = 2, graph = FALSE) # Critical: set graph=FALSE to get the plot object # Customize to black-and-white p_custom <- p + # Apply grayscale to cluster points and fill areas scale_color_grey(start = 0, end = 0.8) + scale_fill_grey(start = 0, end = 0.8) + # Switch to a clean black-and-white base theme theme_bw() + # Fine-tune text and background details theme( text = element_text(color = "black"), # Ensure all text (labels, axes) stays black panel.background = element_rect(fill = "white"), # White panel background plot.background = element_rect(fill = "white"), # White overall plot background panel.grid = element_line(color = "#e0e0e0") # Soft gray grid lines (optional) ) # Render the customized plot print(p_custom)
Approach 2: Pass Color Parameters Directly to conceptualStructure()
If you prefer a one-step solution, you can pass factoextra-compatible color parameters directly into your original conceptualStructure() call:
conceptualStructure(M, field = "ID", method = "MCA", quali.supp = NULL, quanti.supp = NULL, minDegree = 2, clust = "auto", k.max = 5, stemming = FALSE, labelsize = 10, documents = 2, graph = TRUE, # Add these color customization parameters palette = "Greys", # Use a built-in grayscale palette theme = theme_bw()) # Apply black-and-white base theme
For even more control over cluster shades, replace palette = "Greys" with a vector of custom gray hex codes (matching your k.max value):
palette = c("#000000", "#333333", "#666666", "#999999", "#CCCCCC") # 5 distinct grays for k.max=5
Quick Tips
- Adjust the
startandendvalues inscale_color_grey()/scale_fill_grey()to make grayscale contrast stronger (lower end value) or softer (higher end value). - The
theme_bw()function sets a default white background with subtle gray grid lines, which is optimal for black-and-white readability. - Make sure you have the latest versions of
bibliometrix,factoextra, andggplot2installed to avoid compatibility issues.
内容的提问来源于stack exchange,提问作者Mhd. Sholihin

