Biopy
Biopy

Reputation: 167

Color and linewidth in radarchart with R

I'm using the package radarchart in R to make radar plots and I would like to change the color and the linewith of the data's lines. But I don't know how to do. Maybe there is an argument calling "colMatrix", but I don't know how to use it.

Here a short sample of the code :

labels = c("1", "2", "3", "4", "5")
data1 = data.frame("A" = c(0.6, 0.5, 0.2, 0.9, 0.2))
data2 = data.frame("B" = c(0.5, 0.1, 0.9, 0.8, 0.3))
data3 = data.frame("C" = c(0.6, 0.6, 0.5, 0.7, 0.5))
chartJSRadar(scores=c(data1,data2, data3), labs=labels, polyAlpha=0, showLegend=F, maxScale=1)

When running the last line, the plot automatically uses 3 differents color for each data.

enter image description here

But when plotting only one like this :

chartJSRadar(scores=data1, labs=labels)

It uses red by default and I want to use another color (for others radar plots) like this :

https://i.sstatic.net/h4B1A.png

Hope someone knows. Same for linewidth.

Upvotes: 0

Views: 1522

Answers (2)

LuckySeedling
LuckySeedling

Reputation: 425

For what it's worth.. I find the col2rgb function quite handy for converting to RGB colours.

library(radarchart)    

c <- grDevices::col2rgb(c("orange","green", "black"))

labels = c("1", "2", "3", "4", "5")
data1 = data.frame("A" = c(0.6, 0.5, 0.2, 0.9, 0.2))
data2 = data.frame("B" = c(0.5, 0.1, 0.9, 0.8, 0.3))
data3 = data.frame("C" = c(0.6, 0.6, 0.5, 0.7, 0.5))
chartJSRadar(scores=c(data1,data2, data3), labs=labels, polyAlpha=0, showLegend=F, maxScale=1, colMatrix = c)

Same rules apply though, if there are more data series than colours they will get re-used.

I'm not sure about the line width though. After checking the chartjs docs ( http://www.chartjs.org/docs/#radar-chart-chart-options), I thought it would be the borderWidth option but I couldn't get it to work.

Upvotes: 0

GGamba
GGamba

Reputation: 13680

You are indeed correct that the argument to use is colMatrix.
colMatrix accept as input a matrix with 3 rows, first for red, second for green and last for blue, and n columns, where n is the number of colors you need

cols <- matrix(c(0,0,255), nrow = 3)

Produces a single color: a full blue.

cols <- matrix(c(c(0,0,255), c(255,0,0)), nrow = 3)

Produce two colors: a full blue and a full red.


Once you have the colors you need you just pass the matrix to the colMatrix argument:

chartJSRadar(scores=data1, labs=labels, colMatrix = cols)

Note that if there are more data series than color they will be re-used, restarting from the first.

Upvotes: 1

Related Questions