Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 reduce space between geom_point() factors

Tags:

r

ggplot2

I'm trying to make vertical scatter plots. Similar to what you see in many box plots. However, whereas in bar charts I can just increase the width of the bar to close the gap between the bars of different factors, this does not work for geom_point (and would not look attractive anyway). I am at a loss here.

Example:

df <- data.frame('facs' = sample(1:5, 50, replace = TRUE), 
                 'var' = sample(c('boy','girl'), 50, replace = TRUE),
                 'val' = rnorm(50)
                 )
ggplot(df, aes(facs, val)) + geom_point(aes(color = as.factor(facs), size = 4)) + facet_wrap(~var)

The output looks like this, but I really want to shorten all that empty space in between: enter image description here

like image 903
Brandon Avatar asked Dec 24 '22 06:12

Brandon


1 Answers

You could try adding + coord_fixed(ratio = 3), or any number > 1, which will fix the axis ratios.

As an aside, moving size = 4 out of aes() will get rid of the unnecessary part of the legend. i.e geom_point(aes(color = as.factor(facs)), size = 4)

like image 127
Scott Warchal Avatar answered Dec 26 '22 21:12

Scott Warchal