1

How can I show the dots colored using the mosaic package to do a dotplot?

library(mosaic)
n=500
r =rnorm(n)
d = data.frame( x = sample(r ,n= 1,size = n, replace = TRUE), color = c(rep("red",n/2), rep("green",n/2)))
dotPlot(d$x,breaks = seq(min(d$x)-.1,max(d$x)+.1,.1))

right now all the dots are blue but I would like them to be colored according to the color column inthe data table

Jaap
  • 81,064
  • 34
  • 182
  • 193
user3022875
  • 8,598
  • 26
  • 103
  • 167

2 Answers2

3

If you are still interested in a mosaic/lattice solution rather than a ggplot2 solution, here you go.

dotPlot( ~ x, data = d, width = 0.1, groups = color, 
  par.settings=list(superpose.symbol = list(pch = 16, col=c("green", "red"))))

resulting plot

Notice also

  • as with ggplot2, the colors are not determined by the values in your color variable but by the theme. You can use par.settings to modify this on the level of a plot or trellis.par.set() to change the defaults.
  • it is preferable to use a formula and data = and to avoid the $ operator.
  • you can use the width argument rather than breaks if you want to set the bin width. (You can use the center argument to control the centers of the bins if that matters to you. By default, 0 will be the center of a bin.)
rpruim
  • 320
  • 2
  • 6
1

You need to add stackgroups=TRUE so that the two different colors aren't plotted on top of each other.

n=20
set.seed(15)
d = data.frame(x = sample(seq(1,10,1), n, replace = TRUE), 
               color = c(rep("red",n/2), rep("green",n/2)))
table(d$x[order(d$x)])
length(d$x[order(d$x)])
binwidth= 1

ggplot(d, aes(x = x)) + 
  geom_dotplot(breaks = seq(0.5,10.5,1), binwidth = binwidth, 
               method="histodot", aes(fill = color),
               stackgroups=TRUE) +
  scale_x_continuous(breaks=1:10)

Also, ggplot uses its internal color palette for the fill aesthetic. You'd get the same colors regardless of what you called the values of the "color" column in your data. Add scale_fill_manual(values=c("green","red")) if you want to set the colors manually.

enter image description here

eipi10
  • 91,525
  • 24
  • 209
  • 285
  • Thank you that seems to have solved problem 1. Can you help with the other 2 issues? If I change n = 200 the dots don't appear and the y axis is not the count. I need to dynamically change the yaxis limit I think – user3022875 Sep 01 '15 at 22:26
  • Interesting. I'm not sure why that's happening, but I see now that the y-axis ticks are not actually counts. I'll see if I can figure out what's going on. – eipi10 Sep 01 '15 at 22:41
  • The y-scale is a [known issue](http://stackoverflow.com/a/16216666/496488) with `geom_dotplot`. – eipi10 Sep 01 '15 at 22:43
  • yes it is known issue but you can scale it by using ylim() to see the counts. se how they use ylim here http://stackoverflow.com/questions/16216312/how-to-plot-stacked-point-histograms-in-ggplot2-in-r – user3022875 Sep 01 '15 at 22:51
  • `ylim` (or `scale_y_continuous`) changes the labels on the y-axis, but those labels don't correspond to the number of dots. – eipi10 Sep 01 '15 at 22:55
  • I think there has to be a combination of the dotsize parameter, binwidth and ylim that will make it look good – user3022875 Sep 01 '15 at 23:11
  • It looks like the only way to fit all the dots is to decrease the dot size. This is essentially what ggplot would have to do to fit all the dots even if `ylim()` worked as expected. I suggest you post a question to the ggplot2 googlegroup and see if anyone there has a better approach. – eipi10 Sep 01 '15 at 23:21