4

I would like to define the color of each data point on a stripchart separately to add an extra information layer on the stripchart. I did not find a solution

I am aware of Different coloring of groups in R plot, and the answer suggests it is not possible per data point. Am I right?

A way around it could be to plot it column by column in a for loop, but then the x-positions are messed up

d = c (1,3,4);
e = c (2,3,4)
f = c (2,6,5)
data = list (d, e, f)
stripchart (data, vertical =T, pch =21, bg = 1, col=2)

Lets say, I would like to color primes in red, non-primes in blue. Or even and odd.

The most universal solution would take a color-list of same dimension as input, where each value defines the color of the corresponding data point plotted.

Community
  • 1
  • 1
bud.dugong
  • 689
  • 7
  • 16

3 Answers3

3

Then it is still simpler to plot them 1 by 1, and use bg= parameter:

# generate fake data
datalist = list( rnorm(100), rnorm(100, sd = 1),rnorm(100, sd = 2) )
# generate color value for each data point  (say I want to color by each values sign +/-)
colorlist = lapply(datalist, sign)
plot(0,0,type="n",xlim=c(0.5,3.5), ylim=c(-10,10),  xaxt = 'n', xlab ="", ylab = "value",  main ="Smear")
for (i in 1:3) { stripchart(na.omit(datalist[[i]]), at = i, add = T, bg = colorlist[[i]]+2, vertical = T, pch =21, method = "jitter") }
axis(side=1,at=1:3,labels=3:1)
bud.dugong
  • 689
  • 7
  • 16
2

When using stripchart for several groups, using formula interface, you could just add an extra stripchart to the existing plot. It would not help if you use jitter, though.

mydata <- data.frame (x = rnorm(30), group = gl(3, 10, labels = letters[1:3]))
stripchart(x ~ group, data = mydata)
makered <- mydata$group == "b" & mydata$x < 1
stripchart(x ~ group, data = mydata[makered, ], col = "red", add = TRUE)
Ute
  • 238
  • 1
  • 9
1

Here's a solution that you can use for similar problems. In short, you plot the stripchart, convert the plot to a grob using the gridGraphics package, then alter the graphical primitives by just editing the grob.

library(gridGraphics)
grid.newpage()

# Get some data
sample(x = 1:100, size = 50) -> data

# Grab plot...Now it's a grob you can modify
stripchart (data, vertical =T, pch =21, bg = 1, col=2)
grid.echo()
grid.grab() -> mygrob

# Pull out the values of the data and strip unit class
mygrob$children$`graphics-plot-1-points-1`$y -> myunits
convertUnit(myunits, "native", valueOnly = TRUE) -> myunits

# Some data to reference ours against...You can use a test for if your data is a prime, equal number, odd number, whatever..I'm just going to test if my data is in this vector and specify the color based on the results from this test.
sample(1:100, size = 10) -> sampleVec
ifelse(myunits %in% sampleVec, "red", "green") -> updatedCols

# Now sub in the new color specs for the points
mygrob$children$`graphics-plot-1-points-1`$gp$col <- updatedCols
mygrob$children$`graphics-plot-1-points-1`$gp$fill <- updatedCols

# ...And plot
grid.draw(mygrob) 

enter image description here

Chris
  • 1,575
  • 13
  • 20