R / ggplot - use position_jitterdodge without fill

Using geom_point with position_jitterdodge only works when adjusting the fill sketch. I do not understand why this should be!

This team

 library(ggplot2) ggplot(diamonds[ sample(nrow(diamonds), 1000), ], aes(x = cut, y = carat, color = clarity)) + geom_point(shape = 21, position = position_jitterdodge()) 

Gives an error message:

 Error: position_jitterdodge requires the following missing aesthetics: fill 

This works though:

 ggplot(diamonds[ sample(nrow(diamonds), 1000), ], aes(x = cut, y = carat, fill = clarity)) + geom_point(shape = 21, position = position_jitterdodge()) 

enter image description here

Simply filling in the NA value is not a viable solution:

 ggplot(diamonds[ sample(nrow(diamonds), 1000), ], aes(x = cut, y = carat, color = clarity, fill=NA)) + geom_point(shape = 21, position = position_jitterdodge()) > Error in seq.default(h[1], h[2], length = n) : 'to' cannot be NA, NaN or infinite 

Although it works if you specify an arbitrary constant (goodbye to disgusting results):

 ggplot(diamonds[ sample(nrow(diamonds), 1000), ], aes(x = cut, y = carat, color = clarity, fill='constant')) + geom_point(shape = 21, position = position_jitterdodge()) 

enter image description here

Any ideas on how to use jitter / dodge without specifying padding? (i.e. only colored dots)

Edit: next to the @joran comment, I would like to put points on the boxes. Since it is not necessary to use fill to differentiate the boxes, it would be great if geom_point(position=position_jitterdodge()) placed graphs without fill . Perhaps currently impossible, though ...

 #This doesn't work: ggplot(diamonds[ sample(nrow(diamonds), 1000), ], aes(x = cut, y = carat, color = clarity)) + geom_boxplot() + geom_point(shape = 21, position = position_jitterdodge()) #This does, although obviously no one wants a plot like this ggplot(diamonds[ sample(nrow(diamonds), 1000), ], aes(x = cut, y = carat, color = clarity, fill='constant')) + geom_boxplot() + geom_point(shape = 21, position = position_jitterdodge()) #This is way it intended to work, but marries you to 'fill' ggplot(diamonds[ sample(nrow(diamonds), 1000), ], aes(x = cut, y = carat, fill = clarity)) + geom_boxplot() + geom_point(shape = 21, position = position_jitterdodge()) 
+6
source share
1 answer

Ok, here is my workaround. Specify fill along with the aesthetics you really want ( color in my case), then fill in the gap scale_fill_manual

I created another fake dataset that looks more like my actual use case, since diamond data, as stated above, are not really a good candidate for boxing + points

 my_dat <- data.frame(class=factor(rep(1:2, 600)), y=rnorm(1200)), x=rep(letters[1:3], each=400)) ggplot(my_dat, aes(x=x, y=y, fill=class, color=class)) + geom_boxplot(outlier.shape = NA) + geom_point(shape = 21, alpha = 0.5, position=position_jitterdodge()) + scale_fill_manual(values = rep(NA, 2)) 

enter image description here

+4
source

Source: https://habr.com/ru/post/988789/


All Articles