1
votes

I'm plotting some Q-Q plots using the qqplot function. It's very convenient to use, except that I want to color the data points based on their IDs. For example:

library(qualityTools)
n=(rnorm(n=500, m=1, sd=1) )
id=c(rep(1,250),rep(2,250))
myData=data.frame(x=n,y=id)
qqPlot(myData$x, "normal",confbounds = FALSE)

So the plot looks like: enter image description here

I need to color the dots based on their "id" values, for example blue for the ones with id=1, and red for the ones with id=2. I would greatly appreciate your help.

2
check out inside-r.org/packages/cran/car/docs/qqPlot palette and par and col of course (color for points) - Rachel Gallen

2 Answers

3
votes

You can try setting col = myData$y. I'm not sure how the qqPlot function works from that package, but if you're not stuck with using that function, you can do this in base R.

Using base R functions, it would look something like this:

# The example data, as generated in the question
n <- rnorm(n=500, m=1, sd=1)
id <- c(rep(1,250), rep(2,250))
myData <- data.frame(x=n,y=id)

# The plot
qqnorm(myData$x, col = myData$y)
qqline(myData$x, lty = 2)

enter image description here

Not sure how helpful the colors will be due to the overplotting in this particular example.

2
votes

Not used qqPlot before, but it you want to use it, there is a way to achieve what you want. It looks like the function invisibly passes back the data used in the plot. That means we can do something like this:

# Use qqPlot - it generates a graph, but ignore that for now
plotData <- qqPlot(myData$x, "normal",confbounds = FALSE, col = sample(colors(), nrow(myData)))

# Given that you have the data generated, you can create your own plot instead ...
with(plotData, {
  plot(x, y, col = ifelse(id == 1, "red", "blue"))
  abline(int, slope)
})

Hope that helps.