Mikhail Spivakov
2008-Apr-29 14:53 UTC
[R] ggplot2: labels and breaks order does not match and I can't use scale_fill_identity
Hi, I'm plotting a bar chart like this: ggplot() + geom_bar(data=res,aes(fill=f1,x=f2,y=y),stat="identity",position="dodge") f1 contains quite a few levels and the plot is really quite difficult to read when the order of bars on the graph and on the legend does not match. This problem has been discussed recently here: http://www.nabble.com/ggplot2---legend-for-fill-coulours-td16235166.html and the solution was to use scale_fill_identity to manually define the order of both the breaks and the labels. This is however unsuitable for me (at least in the unmodified form), as this is part of a script that does not know in advance how many levels there arae in f1. Is there a way round this problem? Many thanks Mikhail Spivakov Mikhail Spivakov PhD Postdoctoral Fellow EMBL/EBI Germany/UK [[alternative HTML version deleted]]
ONKELINX, Thierry
2008-Apr-30 10:07 UTC
[R] ggplot2: labels and breaks order does not match and I can't usescale_fill_identity
Mikhail, Have you tried sorting your dataframe according to the levels on the x-axis? HTH, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 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-bounces op r-project.org [mailto:r-help-bounces op r-project.org] Namens Mikhail Spivakov Verzonden: dinsdag 29 april 2008 16:53 Aan: r-help op r-project.org Onderwerp: [R] ggplot2: labels and breaks order does not match and I can't usescale_fill_identity Hi, I'm plotting a bar chart like this: ggplot() + geom_bar(data=res,aes(fill=f1,x=f2,y=y),stat="identity",position="dodge" ) f1 contains quite a few levels and the plot is really quite difficult to read when the order of bars on the graph and on the legend does not match. This problem has been discussed recently here: http://www.nabble.com/ggplot2---legend-for-fill-coulours-td16235166.html and the solution was to use scale_fill_identity to manually define the order of both the breaks and the labels. This is however unsuitable for me (at least in the unmodified form), as this is part of a script that does not know in advance how many levels there arae in f1. Is there a way round this problem? Many thanks Mikhail Spivakov Mikhail Spivakov PhD Postdoctoral Fellow EMBL/EBI Germany/UK [[alternative HTML version deleted]] ______________________________________________ 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.
Xavier Chardon
2008-Apr-30 11:16 UTC
[R] ggplot2: labels and breaks order does not match and I can't use scale_fill_identity
The solution to this problem is in the mail below. I initially forgot to send a copy to the mailing list, and I'm doing it now for the record, in case someone bumps into this problem. Xavier -------- Message original -------- Sujet: Re: [R] ggplot2: labels and breaks order does not match and I can't use scale_fill_identity Date: Tue, 29 Apr 2008 17:29:13 +0200 De: Xavier Chardon <xavier.chardon at rennes.inra.fr> R?pondre ? :: xavier.chardon at rennes.inra.fr Pour :: Mikhail Spivakov <ensdev.box at googlemail.com> R?f?rences: <84ebf4fb0804290753k3ae4aacbm3415a9cc5bec60d1 at mail.gmail.com> Hi, I had a similar problem. I don't know if the solution will apply to your case but you might want to give it a try. It consists in including the following line before the plot is created: res$f1 <- factor(res$f1, levels = unique(res$f1) ) with f1 sorted in the order you want it to appear. Xavier Mikhail Spivakov a ?crit :> Hi, > > I'm plotting a bar chart like this: > > ggplot() + > geom_bar(data=res,aes(fill=f1,x=f2,y=y),stat="identity",position="dodge") > > f1 contains quite a few levels and the plot is really quite difficult to > read when the order of bars on the graph and on the legend does not match. > > This problem has been discussed recently here: > http://www.nabble.com/ggplot2---legend-for-fill-coulours-td16235166.html > and the solution was to use scale_fill_identity to manually define the order > of both the breaks and the labels. > This is however unsuitable for me (at least in the unmodified form), as this > is part of a script that does not know in advance how many levels there arae > in f1. > > Is there a way round this problem? > > Many thanks > Mikhail Spivakov > > Mikhail Spivakov PhD > Postdoctoral Fellow > EMBL/EBI > Germany/UK > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. > >-- Xavier Chardon Th?sard Institut de l'?levage / INRA Projet ACTA "mod?lisation environnementale des syst?mes bovins et porcins" xavier.chardon at inst-elevage.asso.fr xavier.chardon at rennes.inra.fr 02 23 48 50 91 -- Xavier Chardon Th?sard Institut de l'?levage / INRA Projet ACTA "mod?lisation environnementale des syst?mes bovins et porcins" xavier.chardon at inst-elevage.asso.fr xavier.chardon at rennes.inra.fr 02 23 48 50 91