Dustin
Dustin

Reputation: 91010

How can I plot a stars in color?

I have a 5x7 matrix I want to plot as a radar or spider chart? For example:

stars(mtcars[, 1:7], locations = c(0,0), radius = FALSE,
      key.loc=c(0,0), main="Motor Trend Cars", lty = 2)

stars plot

I'd like each of these lines to be a different color (and/or different style) so I can make out what I'm looking at.

Upvotes: 1

Views: 3983

Answers (1)

nullglob
nullglob

Reputation: 7023

The following suggestion is a bit of a hack. I'm sure a more elegant solution is possible.

  1. Get the function source of stars, and make a copy: let's call it stars2
  2. Add an extra argument in the stars2 argument list: col.lines = NULL
  3. Change the following of code from: polygon(s.x[i, ], s.y[i, ], lwd = lwd, lty = lty, col = col.stars[i]) to polygon(s.x[i, ], s.y[i, ], lwd = lwd, lty = lty, border = col.lines[i], col = col.stars[i])
  4. Call stars2 providing a color for each of your stars.

The sample call and output (left = before, right = after) are below.

stars2(mtcars[, 1:7], locations = c(0,0), radius = FALSE,key.loc=c(0,0), 
       main="Motor Trend Cars", lty = 2,col.lines = 1:nrow(mtcars))

enter image description here

Upvotes: 6

Related Questions