Dear all,
I am plotting some values with lattice barchart: the y-axis is
automatically ordered alphabetically; is it possible to order the
entries by number, so that the 'larger' histograms would be at the top
of the plot?
This is a working example
```
library(lattice)
Family = c("Adenoviridae", "Baculoviridae",
"Herpesviridae", "Mimiviridae",
"Myoviridae", "Pandoraviridae",
"Phycodnaviridae", "Podoviridae",
"Polydnaviridae", "Retroviridae", "Siphoviridae",
"Unassigned")
Normal = c(7, 15, 24, 8, 65, 24, 17, 16, 8, 15, 49 , 9)
Tumour =c( 17, 75, 94, 14, 242, 28, 41, 69, 12, 11, 305, 51)
Metastasis =c(41, 66, 95, 3, 173, 22, 33, 101, 12, 12, 552, 57)
df = data.frame(Family, Normal, Tumour, Metastasis, stringsAsFactors = FALSE)
COLS = c("darkolivegreen3", "brown3",
"darkorchid3")
barchart(Family ~ Normal+Tumour+Metastasis, data = df, stack = TRUE,
xlim=c(1,1000),
main = "Alphabetical order",
xlab = expression(bold("Number of species")),
ylab = expression(bold("Families")),
auto.key = list(space = "top", columns=3),
par.settings = list(superpose.polygon = list(col = COLS)))
```
--
Best regards,
Luigi
On 04/11/2019 8:31 a.m., Luigi Marongiu wrote:> Dear all, > I am plotting some values with lattice barchart: the y-axis is > automatically ordered alphabetically; is it possible to order the > entries by number, so that the 'larger' histograms would be at the top > of the plot? > This is a working example > > ``` > library(lattice) > Family = c("Adenoviridae", "Baculoviridae", "Herpesviridae", "Mimiviridae", > "Myoviridae", "Pandoraviridae", "Phycodnaviridae", "Podoviridae", > "Polydnaviridae", "Retroviridae", "Siphoviridae", "Unassigned") > Normal = c(7, 15, 24, 8, 65, 24, 17, 16, 8, 15, 49 , 9) > Tumour =c( 17, 75, 94, 14, 242, 28, 41, 69, 12, 11, 305, 51) > Metastasis =c(41, 66, 95, 3, 173, 22, 33, 101, 12, 12, 552, 57) > df = data.frame(Family, Normal, Tumour, Metastasis, stringsAsFactors = FALSE) > COLS = c("darkolivegreen3", "brown3", "darkorchid3") > barchart(Family ~ Normal+Tumour+Metastasis, data = df, stack = TRUE, > xlim=c(1,1000), > main = "Alphabetical order", > xlab = expression(bold("Number of species")), > ylab = expression(bold("Families")), > auto.key = list(space = "top", columns=3), > par.settings = list(superpose.polygon = list(col = COLS))) > ``` > >You could do it by using an ordered factor. For example, o <- order(Normal + Tumour + Metastasis) df$Ordered <- ordered(Family, levels = Family[o]) barchart(Ordered ~ Normal+Tumour+Metastasis, data = df, stack = TRUE, xlim=c(1,1000), main = "Ordered by total", xlab = expression(bold("Number of species")), ylab = expression(bold("Families")), auto.key = list(space = "top", columns=3), par.settings = list(superpose.polygon = list(col = COLS))) Duncan Murdoch
On 04/11/2019 9:25 a.m., Duncan Murdoch wrote:> On 04/11/2019 8:31 a.m., Luigi Marongiu wrote: >> Dear all, >> I am plotting some values with lattice barchart: the y-axis is >> automatically ordered alphabetically; is it possible to order the >> entries by number, so that the 'larger' histograms would be at the top >> of the plot? >> This is a working example >> >> ``` >> library(lattice) >> Family = c("Adenoviridae", "Baculoviridae", "Herpesviridae", "Mimiviridae", >> "Myoviridae", "Pandoraviridae", "Phycodnaviridae", "Podoviridae", >> "Polydnaviridae", "Retroviridae", "Siphoviridae", "Unassigned") >> Normal = c(7, 15, 24, 8, 65, 24, 17, 16, 8, 15, 49 , 9) >> Tumour =c( 17, 75, 94, 14, 242, 28, 41, 69, 12, 11, 305, 51) >> Metastasis =c(41, 66, 95, 3, 173, 22, 33, 101, 12, 12, 552, 57) >> df = data.frame(Family, Normal, Tumour, Metastasis, stringsAsFactors = FALSE) >> COLS = c("darkolivegreen3", "brown3", "darkorchid3") >> barchart(Family ~ Normal+Tumour+Metastasis, data = df, stack = TRUE, >> xlim=c(1,1000), >> main = "Alphabetical order", >> xlab = expression(bold("Number of species")), >> ylab = expression(bold("Families")), >> auto.key = list(space = "top", columns=3), >> par.settings = list(superpose.polygon = list(col = COLS))) >> ``` >> >> > > You could do it by using an ordered factor. For example, > > o <- order(Normal + Tumour + Metastasis) > df$Ordered <- ordered(Family, levels = Family[o]) > barchart(Ordered ~ Normal+Tumour+Metastasis, data = df, stack = TRUE, > xlim=c(1,1000), > main = "Ordered by total", > xlab = expression(bold("Number of species")), > ylab = expression(bold("Families")), > auto.key = list(space = "top", columns=3), > par.settings = list(superpose.polygon = list(col = COLS))) > > Duncan Murdoch >Sorry, I meant to add: you don't need to use an ordered factor for the plot. A regular factor with the levels in the order you want is fine, i.e. you could use df$Ordered <- factor(Family, levels = Family[o]) However, there are some other operations (e.g. comparison using ">") that do require the ordered factor. Duncan Murdoch
Richard M. Heiberger
2019-Nov-04 17:56 UTC
[R] Order axis by number of entries in lattice plot
## The likert function would work well for this example.
## Continuing from your example
## install.packages("HH") ## if necessary
library(HH)
likert(Family ~ Normal+Tumour+Metastasis, data = df,
main = "likert, data-order, ReferenceZero=0\nDuplicates your
example",
ReferenceZero=0,
as.table=FALSE,
xlab = expression(bold("Number of species")),
ylab = expression(bold("Families")),
auto.key = list(space = "top", columns=3),
col = COLS)
likert(Family ~ Normal+Tumour+Metastasis, data = df,
main = "likert, positive.order=TRUE, ReferenceZero=0\nThis is
what you asked for",
positive.order=TRUE,
ReferenceZero=0,
xlab = expression(bold("Number of species")),
ylab = expression(bold("Families")),
auto.key = list(space = "top", columns=3),
col = COLS)
likert(Family ~ Normal+Tumour+Metastasis, data = df,
main = "likert, positive.order=TRUE, ReferenceZero=1.5\nThis
puts Normal on left and not-Normal on right",
positive.order=TRUE,
ReferenceZero=1.5,
xlab = expression(bold("Number of species")),
ylab = expression(bold("Families")),
auto.key = list(space = "top", columns=3),
col = COLS)
## For information on the likert function
?likert
## For more examples
demo("likert-paper", package="HH")
## for the paper, open
http://www.jstatsoft.org/v57/i05/
## and click on
## Download PDF
On Mon, Nov 4, 2019 at 8:32 AM Luigi Marongiu <marongiu.luigi at
gmail.com> wrote:>
> Dear all,
> I am plotting some values with lattice barchart: the y-axis is
> automatically ordered alphabetically; is it possible to order the
> entries by number, so that the 'larger' histograms would be at the
top
> of the plot?
> This is a working example
>
> ```
> library(lattice)
> Family = c("Adenoviridae", "Baculoviridae",
"Herpesviridae", "Mimiviridae",
> "Myoviridae", "Pandoraviridae",
"Phycodnaviridae", "Podoviridae",
> "Polydnaviridae", "Retroviridae",
"Siphoviridae", "Unassigned")
> Normal = c(7, 15, 24, 8, 65, 24, 17, 16, 8, 15, 49 , 9)
> Tumour =c( 17, 75, 94, 14, 242, 28, 41, 69, 12, 11, 305, 51)
> Metastasis =c(41, 66, 95, 3, 173, 22, 33, 101, 12, 12, 552, 57)
> df = data.frame(Family, Normal, Tumour, Metastasis, stringsAsFactors =
FALSE)
> COLS = c("darkolivegreen3", "brown3",
"darkorchid3")
> barchart(Family ~ Normal+Tumour+Metastasis, data = df, stack = TRUE,
> xlim=c(1,1000),
> main = "Alphabetical order",
> xlab = expression(bold("Number of species")),
> ylab = expression(bold("Families")),
> auto.key = list(space = "top", columns=3),
> par.settings = list(superpose.polygon = list(col = COLS)))
> ```
>
>
> --
> Best regards,
> Luigi
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
Thank you. the factor approach worked all right. Thank you for Likert also: I already used it but here I wanted to run with lattice only. Best regards On Mon, Nov 4, 2019 at 6:57 PM Richard M. Heiberger <rmh at temple.edu> wrote:> > ## The likert function would work well for this example. > ## Continuing from your example > > ## install.packages("HH") ## if necessary > library(HH) > > likert(Family ~ Normal+Tumour+Metastasis, data = df, > main = "likert, data-order, ReferenceZero=0\nDuplicates your example", > ReferenceZero=0, > as.table=FALSE, > xlab = expression(bold("Number of species")), > ylab = expression(bold("Families")), > auto.key = list(space = "top", columns=3), > col = COLS) > > likert(Family ~ Normal+Tumour+Metastasis, data = df, > main = "likert, positive.order=TRUE, ReferenceZero=0\nThis is > what you asked for", > positive.order=TRUE, > ReferenceZero=0, > xlab = expression(bold("Number of species")), > ylab = expression(bold("Families")), > auto.key = list(space = "top", columns=3), > col = COLS) > > likert(Family ~ Normal+Tumour+Metastasis, data = df, > main = "likert, positive.order=TRUE, ReferenceZero=1.5\nThis > puts Normal on left and not-Normal on right", > positive.order=TRUE, > ReferenceZero=1.5, > xlab = expression(bold("Number of species")), > ylab = expression(bold("Families")), > auto.key = list(space = "top", columns=3), > col = COLS) > > ## For information on the likert function > ?likert > > ## For more examples > demo("likert-paper", package="HH") > > ## for the paper, open > http://www.jstatsoft.org/v57/i05/ > ## and click on > ## Download PDF > > On Mon, Nov 4, 2019 at 8:32 AM Luigi Marongiu <marongiu.luigi at gmail.com> wrote: > > > > Dear all, > > I am plotting some values with lattice barchart: the y-axis is > > automatically ordered alphabetically; is it possible to order the > > entries by number, so that the 'larger' histograms would be at the top > > of the plot? > > This is a working example > > > > ``` > > library(lattice) > > Family = c("Adenoviridae", "Baculoviridae", "Herpesviridae", "Mimiviridae", > > "Myoviridae", "Pandoraviridae", "Phycodnaviridae", "Podoviridae", > > "Polydnaviridae", "Retroviridae", "Siphoviridae", "Unassigned") > > Normal = c(7, 15, 24, 8, 65, 24, 17, 16, 8, 15, 49 , 9) > > Tumour =c( 17, 75, 94, 14, 242, 28, 41, 69, 12, 11, 305, 51) > > Metastasis =c(41, 66, 95, 3, 173, 22, 33, 101, 12, 12, 552, 57) > > df = data.frame(Family, Normal, Tumour, Metastasis, stringsAsFactors = FALSE) > > COLS = c("darkolivegreen3", "brown3", "darkorchid3") > > barchart(Family ~ Normal+Tumour+Metastasis, data = df, stack = TRUE, > > xlim=c(1,1000), > > main = "Alphabetical order", > > xlab = expression(bold("Number of species")), > > ylab = expression(bold("Families")), > > auto.key = list(space = "top", columns=3), > > par.settings = list(superpose.polygon = list(col = COLS))) > > ``` > > > > > > -- > > Best regards, > > Luigi > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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.-- Best regards, Luigi