remove space on left of plot

214 views
Skip to first unread message

John Kane

unread,
Mar 2, 2012, 8:59:57 AM3/2/12
to ggplot2
I am trying to create a simple dot-plot in ggplot2 with a solid line
from the y-axis to the dot, something I first saw in Cleveland's 1984
book

What I would like is to have the graph start at zero on the x-axis but
leave some space on the right side of the plot area.

I assumed that I should be able to do this with expand() in
scale_x_continuous() but either I'm wrong or just don't understand
what expand() is doing. So far, I see that
scale_x_continuous(expand=c(0,0)) removes the space on the right and
left. but I don't understand how to just get the left only to
disappear.

Any help would be appreciated

Code
#================================================
library(ggplot2)
xx<- structure(list(Food = c("Milk", "Bread", "Potatoes", "Rice"),
Expense = c(25L, 49L, 34L, 15L)), .Names = c("Food", "Expense"
), class = "data.frame", row.names = c(NA, -4L))

xx$Food <- as.factor(xx$Food)

p <- ggplot(xx, aes(Expense, reorder(Food, Expense)))

pa <- p + geom_point(aes(colour=Food)) +
geom_segment(aes(x = 0, xend = Expense,
y = Food, yend = Food,colour=Food)) +
opts(legend.position = "none")

pa # so far so good

p1 <- pa + scale_x_continuous(expand=c(0,0)) # Nope.

#=============================================

sessionInfo
version 2.13.1 (2011-07-08)
Platform: i686-pc-linux-gnu (32-bit)

locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=C LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] grid stats graphics grDevices utils datasets
methods
[8] base

other attached packages:
[1] ggplot2_0.8.9 proto_0.3-9.2 reshape_0.8.4 plyr_1.7

Hadley Wickham

unread,
Mar 2, 2012, 1:00:06 PM3/2/12
to John Kane, ggplot2
You'll need to manually set the limits and use expand=c(0,0).

Hadley

> --
> 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

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

John Kane

unread,
Mar 2, 2012, 7:22:57 PM3/2/12
to ggp...@googlegroups.com, John Kane
Thanks Hadley.

 I never thought of combing the two commands. 

p1 <- pa + scale_x_continuous(limits=c(0, 52),expand=c(0,0))
works very nicely. 

> To unsubscribe: email ggplot2+unsubscribe@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

Reply all
Reply to author
Forward
0 new messages