R语言circlize包:弦图highlight.sector()宽度与位置调整求助
Got it, let's work through how to fine-tune the highlight.sector() function in circlize for your fishery landing data. I’ll walk you through practical examples tailored to your setup—where you’ve got home ports (PORT_DE), landing ports (PORT_LA), scallop weights (SCALLOP_W), and port metadata tied to regions (Region_lb) and countries (Country_lb).
First, let’s start with a quick setup using simulated data that mirrors your structure, so you can adapt the code directly to your real dataset:
library(circlize) library(reshape2) # Simulate your fishery data set.seed(123) port_data <- data.frame( PORT_DE = sample(c("PortA_FR", "PortB_FR", "PortC_UK", "PortD_UK"), 100, replace = TRUE), PORT_LA = sample(c("PortX_FR", "PortY_UK", "PortZ_ES"), 100, replace = TRUE), SCALLOP_W = rnorm(100, mean = 5, sd = 2) ) # Port metadata (match your Name_short, Region_lb, Country_lb fields) port_meta <- data.frame( Name_short = c("PortA", "PortB", "PortC", "PortD", "PortX", "PortY", "PortZ"), Region_lb = c("Brittany", "Brittany", "Cornwall", "Cornwall", "Normandy", "Scotland", "Galicia"), Country_lb = c("France", "France", "UK", "UK", "France", "UK", "Spain") ) # Build adjacency matrix (sum scallop weights by port pair) adj_matrix <- aggregate(SCALLOP_W ~ PORT_DE + PORT_LA, data = port_data, sum) adj_matrix <- dcast(adj_matrix, PORT_DE ~ PORT_LA, value.var = "SCALLOP_W", fill = 0) rownames(adj_matrix) <- adj_matrix$PORT_DE adj_matrix <- adj_matrix[, -1] # Base chord diagram with annotation track chordDiagram(adj_matrix, annotationTrack = "grid", preAllocateTracks = list(track.height = 0.1)) circos.trackPlotRegion(track.index = 1, panel.fun = function(x, y) { circos.text(CELL_META$xcenter, CELL_META$ylim[1], CELL_META$sector.index, facing = "clockwise", niceFacing = TRUE, adj = c(0, 0.5)) }, bg.border = NA)
1. Adjust Width & Position for Single Sectors
To tweak the highlight size and placement for individual ports (e.g., a key home port), use the padding parameter—it controls the inner margin of the highlight relative to the full sector width (values are proportional to the sector’s angle, 0–1).
For example, to highlight PortA_FR with a narrower, centered highlight:
highlight.sector(sector.index = "PortA_FR", track.index = 1, col = "#FFDDCC", padding = c(0.1, 0, 0.1, 0), # Top/bottom margins shrink the highlight width by 20% text = "French Home Port", text.vjust = -0.5, # Move text above the highlight text.hjust = 0.5, cex = 0.8)
- Smaller
paddingvalues = wider highlight; larger values = narrower highlight. - Use
text.vjust/text.hjustto nudge your label exactly where you want it (positive values move text down/right, negative up/left).
2. Highlight Groups of Ports (By Region/Country)
Since your ports are grouped by Region_lb or Country_lb, you can highlight all ports in a single group and adjust the spacing between them with extend (fills gaps between adjacent sectors in the group):
# Get all French ports present in your adjacency matrix french_ports <- c(paste0(port_meta$Name_short[port_meta$Country_lb == "France"], "_FR"), paste0(port_meta$Name_short[port_meta$Country_lb == "France"], "")) # Match your port naming french_ports <- intersect(french_ports, c(rownames(adj_matrix), colnames(adj_matrix))) # Highlight all French ports with a unified, gap-free highlight highlight.sector(sector.index = french_ports, track.index = 1, col = "#CCEEFF", padding = c(0.05, 0.05, 0.05, 0.05), # Small margins around the entire group extend = 0.02, # Extend highlights 2% of the circle to fill gaps between sectors text = "French Ports", text.vjust = -1, # Move label further outward text.col = "darkblue", cex = 1)
3. Precise Position Control with Angle Ranges
For ultra-specific placement, use xlim to manually define the start/end angles of the highlight (in radians). First, grab the sector’s angle range, then adjust it:
# Get angle range for PortC_UK portc_angle <- get.cell.meta.data("xlim", sector.index = "PortC_UK", track.index = 1) # Shrink the highlight to the middle 80% of the sector highlight.sector(sector.numeric.index = which(get.all.sector.index() == "PortC_UK"), track.index = 1, col = "#DDFFCC", xlim = c(portc_angle[1] + 0.1, portc_angle[2] - 0.1), # Narrow the highlight by 0.1 radians on each side text = "UK Landing Port", text.vjust = 1.5, # Move label inside the sector text.hjust = 0.5)
4. Adjust Highlight Position Across Tracks
If you’ve pre-allocated multiple tracks (with preAllocateTracks), use track.index and ylim to control where the highlight sits vertically in the track:
# Add a second track first (uncomment if you didn't include it earlier) # circos.trackPlotRegion(track.index = 2, bg.border = NA) # Highlight PortZ_ES in the second track, centered vertically highlight.sector(sector.index = "PortZ_ES", track.index = 2, col = "#FFCCEE", ylim = c(0.2, 0.8), # Restrict highlight to the middle 60% of the track height text = "Spanish Port", text.vjust = 0.5, text.hjust = 0.5)
Key Parameters Recap
padding: Controls highlight width relative to the sectorextend: Connects highlights across grouped sectorsxlim: Manually sets highlight start/end angles for precisionylim: Adjusts vertical position in multi-track diagramstext.vjust/text.hjust: Fine-tune label placement
内容的提问来源于stack exchange,提问作者Andronikos K.

