Spacing between groups on discrete axis

7,142 views
Skip to first unread message

Emily

unread,
Sep 18, 2011, 12:28:45 AM9/18/11
to ggplot2
Hello all,

I am a big fan of ggplot2 but I am currently stuck :( I'd like to make
a plot similar a box plot (i.e. showing the distribution of values
across several discrete groups). Except, instead of box plots, I would
like to show individual data points. This are the type of plots I am
talking about:

data(mtcars)
a <- ggplot(data=mtcars, aes(x = as.factor(cyl), y = mpg))
a + geom_point(size=4, alpha=0.3)

My question is: How do I change the spacing between different
categories on the discrete x-axis? As you can see, there is a big
space between the tick marks for each cylinder level. How do I make
the groups closer together?

Thank you very much,
Emily

Joshua Wiley

unread,
Sep 18, 2011, 12:01:30 PM9/18/11
to Emily, ggplot2
Hi Emily,

Here are two approaches. The first changes the device region, and so
the plot is drawn differently. The second changes the aspect ratio in
ggplot2. It is possible to change the underlying numbers too, but
this will not necessarily change the appearance.

data(mtcars)

a <- ggplot(data = mtcars, aes(x = as.factor(cyl), y = mpg)) +

geom_point(size = 4, alpha = 0.3)


dev.new(width = 3, height = 10)
a

dev.new()
a + coord_equal(ratio = 1)

HTH,

Josh

P.S. Sorry for the repeat, forgot to copy the group (my reply to all
button moved and my poor brain has yet to adapt)

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

--
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, ATS Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/

Reply all
Reply to author
Forward
0 new messages