0

I have a dataset with two columns. One is the data category and one is my samples. I am using mtcars as an example

library(circlize)
library(data.table)
mtCARS<- mtcars
setDT(mtCARS, keep.rownames = TRUE)[]
mtCARS1<- mtCARS[,c(1,11)]
colnames(mtCARS1)<- c("Brand", "gear")

now lets say I'm interested in the car names (now called Brand) as being my samples and gear being the category. I want two things

1. The chords emanating from the category, in this case the gear to be three colours, corresponding to the category number.

2. The grid colour for all the samples (in this case brand) to be the same colour.

If you where to execute

chordDiagram(as.data.frame(mtCARS1)

you get random colour assignment for everything and chords emanating from the categories (Brand) would be multiply coloured.

Now the above command needs to be executed (I think) as below

chordDiagram(as.data.frame(mtCARS1), col=col, grid.col = grid.col)

note that I have defined the col and grid.col function, which I believe need to be defined to address my points 1 and 2 respectively this is where I'm having problems.

so far for the chord colour (point 1) I have tried the below but it didn't work

library(RColorBrewer)    
col<- colorRampPalette(c("red", "green", "blue"), space= "rgb")(30)

for the grid colour (point 2) I have tried

othercol<- as.character(mtCARS1[,1])
grid.col = c("4" = "red", "3" = "green", "5" = "yellow", othercol = "blue")

This gets me the category (gear) colour which I want, but for the sample colours (Brand), the grid colours are not blue.

please note that my actual data is loaded as a data frame and not a matrix.

NOTE: The solution to point one is to reverse the order of columns since it is the first column that defines the number of colours used. Hence do

mtCARS1<- mtCARS[,c(11,1)]

1 Answers1

3

If you see the value of othercol, you will know what was going on:

> othercol
[1] "c(\"Mazda RX4\", \"Mazda RX4 Wag\", \"Datsun 710\", \"Hornet 4 Drive\", \"Hornet Sportabout\", \"Valiant\", \"Duster 360\", \"Merc 240D\", \"Merc 230\", \"Merc 280\", \"Merc 280C\", \"Merc 450SE\", \"Merc 450SL\", \"Merc 450SLC\", \"Cadillac Fleetwood\", \"Lincoln Continental\", \"Chrysler Imperial\", \"Fiat 128\", \"Honda Civic\", \"Toyota Corolla\", \"Toyota Corona\", \"Dodge Challenger\", \"AMC Javelin\", \"Camaro Z28\", \"Pontiac Firebird\", \"Fiat X1-9\", \"Porsche 914-2\", \"Lotus Europa\", \"Ford Pantera L\", \"Ferrari Dino\", \"Maserati Bora\", \"Volvo 142E\"\n)"

To get the name of all brands, you can simply do (using [[ returns a "pure" vector):

> brand = mtCARS1[[1]]
> brand
 [1] "Mazda RX4"           "Mazda RX4 Wag"       "Datsun 710"
 [4] "Hornet 4 Drive"      "Hornet Sportabout"   "Valiant"
 [7] "Duster 360"          "Merc 240D"           "Merc 230"
[10] "Merc 280"            "Merc 280C"           "Merc 450SE"
[13] "Merc 450SL"          "Merc 450SLC"         "Cadillac Fleetwood"
[16] "Lincoln Continental" "Chrysler Imperial"   "Fiat 128"
[19] "Honda Civic"         "Toyota Corolla"      "Toyota Corona"
[22] "Dodge Challenger"    "AMC Javelin"         "Camaro Z28"
[25] "Pontiac Firebird"    "Fiat X1-9"           "Porsche 914-2"
[28] "Lotus Europa"        "Ford Pantera L"      "Ferrari Dino"
[31] "Maserati Bora"       "Volvo 142E"

Then you can assign blue to all these brands and construct the grid.col vector:

othercol = structure(rep("blue", length(brand)), names = brand)
grid.col = c("4" = "red", "3" = "green", "5" = "yellow", othercol)
chordDiagram(as.data.frame(mtCARS1), grid.col = grid.col, 
    col = grid.col[as.character(mtCARS1[[2]])])

enter image description here

Zuguang Gu
  • 1,301
  • 7
  • 11