0
votes

I have a dot plot and I'd like to color the dots so I have a d$color vector that corresponds to the color for a particular dot.

Question 1: When you run the code below you can see the dots are not colored properly? Do you know how to color them properly? The code needs to dynamically handle the situation where the colors change. For example in this case "red" is the first color but that will not always be the case.

Question 2: Do you also know how to make the dots filled instead of transparent?

library(mosaic)
binwidth <- 1
dat <- c(1, 1, 1, 2, 3, 3, 4, 4, 5, 5)
d <- data.frame(x=dat, color=c("red", "green", "blue", "blue", "purple", "red",
                               "red", "blue", "green", "green"))
dotPlot(~x, data=d, groups=color, 
        breaks=seq(min(d$x) - binwidth, max(d$x) + binwidth, binwidth), 
        cex=1, col=as.factor(d$color))

Question 3: Can you run this code? The soltuion does not seem to work here:

n=50
r =rnorm(n)
dat = sample(r ,n= 1,size = n, replace = TRUE)
d = data.frame( x = dat, color = c(rep("red",n/2), rep("green",n/2)))
dotPlot(d$x,  breaks = seq(min(d$x)-.1,max(d$x)+.1,.1)) # this works
dotPlot(d$x,  breaks = seq(min(d$x)-.1,max(d$x)+.1,.1), groups = color,col = levels(d$color) ) # this does not work
2

2 Answers

1
votes

Regarding the Q2, simply changing the type of "point" by default with the argument "pch".

dotPlot(~x, data=d, groups = color, breaks = seq(min(d$x)-binwidth, max(d$x)+binwidth,binwidth), cex = 1, col = d$color, pch = 16)
1
votes

To colour the points as desired, pass a vector of colours that corresponds to the colours that you want for your groups (so here, a vector of 4 colours, not a vector of 10 colours).

dotPlot(~x, data=d, groups=color, col=levels(d$color),
        breaks=seq(min(d$x) - binwidth, max(d$x) + binwidth, binwidth))

enter image description here

To change the symbol, use pch (see ?pch for a list of built-in plotting characters).

dotPlot(~x, data=d, groups=color, col=levels(d$color), pch=20,
        breaks=seq(min(d$x) - binwidth, max(d$x) + binwidth, binwidth))

enter image description here