[R] ggplot2 : commands on one line vs two lines.

John Kane jrkrideau at yahoo.ca
Fri Jul 24 01:13:28 CEST 2009


I have just started using ggplot2 and I seem to be doing something stupid
in writing ggplot2 commands on more than one line.

In the example below the commands on one line are working fine, but as
soon as I put them on two lines I get an error.  Can any one point out
what I am doing wrong? It must be something blindingly simple.

Thanks

Example
#==============================================================================
# sample data file
provs <- structure(list(Name = structure(c(8L, 11L, 6L, 2L, 9L, 1L, 12L,
3L, 13L, 5L, 4L, 7L, 10L), .Label = c("Alberta", "British Columbia",
"Manitoba", "New Brunswick", "Newfoundland", "Northwest Territories",
"Nova Scotia", "Nunavut", "Ontario", "Prince Edward Island",
"Québec", "Saskatchewan", "Yukon"), class = "factor"), Pcode = structure(c(8L,
11L, 7L, 2L, 9L, 1L, 12L, 3L, 13L, 5L, 4L, 6L, 10L), .Label = c("AB",
"BC", "MB", "NB", "NL", "NS", "NT", "NU", "ON", "PE", "QC", "SK",
"YT"), class = "factor"), Area = c(1936113L, 1365128L, 1183085L,
925186L, 917741L, 642317L, 591670L, 553556L, 474391L, 373872L,
71450L, 53338L, 5660L)), .Names = c("Name", "Pcode", "Area"),
class = "data.frame", row.names = c(NA,
-13L))

library(ggplot2)

p <- ggplot(provs, aes(x = 0, xend = Area,  y = Pcode, yend = Pcode))

# commmand below is on one line
pa <- p + geom_point(aes(Area, Pcode)) + geom_segment() + xlab("Area")

pa

pa1 <- p + geom_point(aes(Area, Pcode)) + geom_segment()
           + xlab("Area")
# Error in +xlab("Area") : invalid argument to unary operator


# commmand below is on one line
pb <- p + geom_point(aes(Area, Pcode)) + geom_segment() + labs(x="Area", y="Povs")
pb


pb1 <- p + geom_point(aes(Area, Pcode)) + geom_segment()
        + labs(x="Area", y="Povs")
        
# Error in +labs(x = "Area", y = "Povs") :
#  invalid argument to unary operator

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

R version 2.9.1 (2009-06-26)
i386-pc-mingw32

locale:
LC_COLLATE=English_Canada.1252;LC_CTYPE=English_Canada.1252;
LC_MONETARY=English_Canada.1252;LC_NUMERIC=C;LC_TIME=English_Canada.1252

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

other attached packages:
[1] ggplot2_0.8.3 reshape_0.8.3 plyr_0.1.9    proto_0.3-8




      __________________________________________________________________
Looking for the perfect gift? Give the gift of Flickr! 

http://www.flickr.com/gift/




More information about the R-help mailing list