[R] ggplot2 dot chart-start at zero

Robert Baer rbaer at atsu.edu
Thu Mar 1 01:06:15 CET 2012


Not sure I'm interpreting what you want to do correctly, but how about:
> p2 <- pa + scale_x_continuous(limits=c(0,80))
> p2


-----Original Message----- 
From: John Kane
Sent: Wednesday, February 29, 2012 2:32 PM
To: r-help at r-project.org
Subject: [R] ggplot2 dot chart-start at zero

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 below
#========================================
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))


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

pa <- p + geom_point(aes(Expense, Food, 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))
#=======================================

R 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


John Kane
Kingston ON Canada

____________________________________________________________
FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your 
desktop!

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


------------------------------------------
Robert W. Baer, Ph.D.
Professor of Physiology
Kirksville College of Osteopathic Medicine
A. T. Still University of Health Sciences
800 W. Jefferson St.
Kirksville, MO 63501
660-626-2322
FAX 660-626-2965



More information about the R-help mailing list