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

Kingsford Jones kingsfordjones at gmail.com
Fri Jul 24 08:26:19 CEST 2009


Hi John,

Thank you for the easily cut-and-pastable example.

You are providing syntactically complete code on the first line so the
second line is not seen as a continuation.  Try moving the '+' on the
second line to the first.  E.g,

pb1 <- p + geom_point(aes(Area, Pcode)) + geom_segment() +
         labs(x="Area", y="Povs")
pb1


hth,

Kingsford Jones

On Thu, Jul 23, 2009 at 5:13 PM, John Kane<jrkrideau at yahoo.ca> wrote:
>
> 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/
>
> ______________________________________________
> 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.
>




More information about the R-help mailing list