[R] ggplot2 dot chart-start at zero

John Kane jrkrideau at inbox.com
Wed Feb 29 21:32:21 CET 2012


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!



More information about the R-help mailing list