[R] ggplot facet and subsetting

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Thu Nov 27 09:29:24 CET 2014


Dear Petr,

You need to use aes_string() instead of aes().

The.cols <- colnames(data)[n:m]
for (i in The.cols) { p<-ggplot(data, aes_string(x="x", y= i, colour="f"))), ...}

Best regards,

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
+ 32 2 525 02 51
+ 32 54 43 61 85
Thierry.Onkelinx op inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: R-help [mailto:r-help-bounces op r-project.org] Namens PIKAL Petr
Verzonden: woensdag 26 november 2014 14:40
Aan: R help
Onderwerp: [R] ggplot facet and subsetting

Dear all

I encountered strange behaviour of ggplot with combination of facet and subsetting. I use for creating plots sometimes a for cycle, something like this

for (i in n:m) { p<-ggplot(data, aes(x=x, y=data[,i], colour=f))), ...}

However I found strange result with this combination

This is OK but only in BW
p<-ggplot(vec.c, aes(x=fi, y=nad1mi))
p+geom_point(size=5)+geom_line()+facet_grid(.~stroj)

this is OK with colour
p<-ggplot(vec.c, aes(x=fi, y=nad1mi, colour=as.factor(cas)))
p+geom_point(size=5)+geom_line()+facet_grid(.~stroj)

Here results in facets are mismatched
p<-ggplot(vec.c, aes(x=fi, y=vec.c[,2], colour=as.factor(cas)))
p+geom_point(size=5)+geom_line()+facet_grid(.~stroj)

and this is mismatched too
p<-ggplot(vec.c, aes(x=fi, y=vec.c[,2]))
p+geom_point(size=5)+geom_line()+facet_grid(.~stroj)

Doeas anybody know what I am doing wrong?

> dput(vec.c)
structure(list(cas = c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 0L, 1L, 2L, 0L, 1L, 2L, 0L, 1L, 2L, 0L, 1L, 2L, 0L, 1L, 2L, 0L, 1L, 2L, 0L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L),
    nad1mi = c(3, 2.7, 0.3, 0.5, 1.9, 5.3, 0.4, 3, 5.4, 0.7,
    20.6, 16.7, 16.6, 20.7, 16.1, 15.2, 20.5, 16.4, 14.8, 24.6,
    19.3, 15.2, 26.9, 21.3, 20.6, 22.6, 16.3, 15.7, 19.3, 16.5,
    15.5, 3.6, 3.4, 5.9, 4.6, 5.4, 4.2, 5.3, 5.6, 5.1, 5), stroj = structure(c(3L,
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L,
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("mastersizer",
    "odstredivka", "zetasizer"), class = "factor"), fi = c(341L,
    341L, 285L, 285L, 401L, 401L, 231L, 231L, 190L, 190L, 341L,
    341L, 341L, 285L, 285L, 285L, 401L, 401L, 401L, 231L, 231L,
    231L, 190L, 190L, 190L, 167L, 167L, 167L, 161L, 161L, 161L,
    341L, 341L, 285L, 285L, 401L, 401L, 231L, 231L, 190L, 190L
    )), .Names = c("cas", "nad1mi", "stroj", "fi"), class = "data.frame", row.names = c(1L, 2L, 6L, 7L, 11L, 12L, 16L, 17L, 21L, 22L, 26L, 27L, 28L, 32L, 33L, 34L, 38L, 39L, 40L, 44L, 45L, 46L, 50L, 51L, 52L, 56L, 57L, 58L, 62L, 63L, 64L, 68L, 69L, 73L, 74L, 78L, 79L, 83L, 84L, 88L,
89L))
>

Regards
Petr

> sessionInfo(package = NULL)
R Under development (unstable) (2014-07-16 r66175)
Platform: i386-w64-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=Czech_Czech Republic.1250  LC_CTYPE=Czech_Czech Republic.1250 [3] LC_MONETARY=Czech_Czech Republic.1250 LC_NUMERIC=C [5] LC_TIME=Czech_Czech Republic.1250

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

other attached packages:
[1] ggplot2_1.0.0   lattice_0.20-29 fun_1.0

loaded via a namespace (and not attached):
 [1] colorspace_1.2-4 digest_0.6.4     grid_3.2.0       gtable_0.1.2
 [5] labeling_0.2     MASS_7.3-33      munsell_0.4.2    plyr_1.8.1
 [9] proto_0.3-10     Rcpp_0.11.2      reshape2_1.4     scales_0.2.4
[13] stringr_0.6.2    tools_3.2.0
>

________________________________
Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
______________________________________________
R-help op 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.
* * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document.
The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.


More information about the R-help mailing list