R语言:修改和弦图(Chord Diagram)的颜色
circlize Hey there! Let's work through how to tweak the colors of your chord diagram using the circlize package. First, let's fix a small issue in your current code, then dive into customizing both the outer sector colors and the connecting chord colors.
Step 1: Clean Up Your Existing Code
Your as.matrix() call has an extra parameter that doesn't belong here—as.matrix() only takes the data object and optional dimnames. Let's adjust that first to get your data in the right shape:
library(circlize) # Read the data (assuming your file includes the header row you shared) table <- read.table('test_circos_real_data.txt', header = TRUE) # Convert to matrix (drop the first column since it's the row index) table_matrix <- as.matrix(table[, -1]) # Assign row names for your transport modes rownames(table_matrix) <- c('car', 'bus','train','bike','walk','subway','taxi') # Match column names to row names (required for symmetric chord diagram logic) colnames(table_matrix) <- rownames(table_matrix)
Step 2: Customize Sector (Outer Ring) Colors
To set unique colors for each outer sector (each representing a transport mode), use the grid.col parameter in chordDiagram(). Create a named vector where each name matches your row/column labels, and the value is your desired color:
# Define sector colors (use hex codes or named colors, add transparency with AA in hex) sector_colors <- c( car = "#FF000080", bus = "#00FF0080", train = "#0000FF80", bike = "#FFA50080", walk = "#80008080", subway = "#00808080", taxi = "#FFFF0080" ) # Draw the diagram with custom sector colors chordDiagram(table_matrix, grid.col = sector_colors)
Step 3: Customize Chord (Connecting Line) Colors
You have flexible options for setting chord colors, depending on how much control you want:
Option 1: Match Chord Color to Its Source Sector
Use a simple function to make each chord inherit the color of its originating sector:
chordDiagram( table_matrix, grid.col = sector_colors, col = function(x) sector_colors[x[1]] # x[1] refers to the source sector name )
Option 2: Match Chord Color to Its Target Sector
Similarly, use x[2] to tie the chord color to its destination sector:
chordDiagram( table_matrix, grid.col = sector_colors, col = function(x) sector_colors[x[2]] )
Option 3: Full Control with a Color Matrix
If you want to set a unique color for every single chord connection, create a color matrix that mirrors the structure of your data matrix:
# Build a color matrix matching your data dimensions and labels chord_colors <- matrix( c( "#FF000080", "#00FF0080", "#0000FF80", "#FFA50080", "#80008080", "#00808080", "#FFFF0080", "#00FF0080", "#00FF0080", "#0000FF80", "#FFA50080", "#80008080", "#00808080", "#FFFF0080", "#0000FF80", "#0000FF80", "#0000FF80", "#FFA50080", "#80008080", "#00808080", "#FFFF0080", "#FFA50080", "#FFA50080", "#FFA50080", "#FFA50080", "#80008080", "#00808080", "#FFFF0080", "#80008080", "#80008080", "#80008080", "#80008080", "#80008080", "#00808080", "#FFFF0080", "#00808080", "#00808080", "#00808080", "#00808080", "#00808080", "#00808080", "#FFFF0080", "#FFFF0080", "#FFFF0080", "#FFFF0080", "#FFFF0080", "#FFFF0080", "#FFFF0080", "#FFFF0080" ), nrow = 7, dimnames = list(rownames(table_matrix), colnames(table_matrix)) ) # Use the custom color matrix for chords chordDiagram( table_matrix, grid.col = sector_colors, col = chord_colors )
Option 4: Simple Repeating Color Pattern
If you want a basic cyclic color scheme, pass a short vector of colors to col—it will repeat automatically:
chordDiagram( table_matrix, grid.col = sector_colors, col = c("#FF000080", "#00FF0080", "#0000FF80") # Colors cycle through this list )
Quick Tip
If you want to add a legend to map colors to transport modes, use the base R legend() function after drawing your chord diagram.
内容的提问来源于stack exchange,提问作者Slowat_Kela

