[R] Lattice stacked barplot vertical bars

Duncan Mackay dulcalma at bigpond.com
Fri Sep 22 15:48:56 CEST 2017


Hi Luigi

will this do

barchart(
    D ~ A|E,
    DF,
    groups = C,
    stack = TRUE,
    horizontal = F,
    main = "Comparison of test results",
    xlab = "Count",
    col = c("yellow", "orange"),
    par.settings = list(
        strip.background = list(col="light grey"),
        superpose.polygon=list(col= c("yellow", "orange"))
    ),
    scales = list(
        alternating = FALSE
    ),
    key = list(
        space="top",
        columns=2,
        text=list(c("Single infections", "Multiple infections"),
col="black"),
        rectangles=list(col=c("yellow", "orange"))
    ),
    strip = strip.custom(factor.levels = c("Cases","Controls"),
                         strip.levels=TRUE,
                         strip.names=FALSE,
                         par.strip.text = list(cex = 1)
    )
)

Too late to do anything more

Regards 

Duncan Macka

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Luigi
Marongiu
Sent: Friday, 22 September 2017 21:53
To: r-help
Subject: [R] Lattice stacked barplot vertical bars

Dear all,
I have made a barplot using lattice in which the bars are stacked
horizontally. I would like to stack them vertically, but if I use the
parameter 'horizontal = FALSE' I get the error: 'Error in FUN(X[[i]],
...) : invalid 'type' (character) of argument'. The problem I face is
that, in drawing the bars vertically, the y variable becomes numeric
rather than categorical and vice-versa for the x variable.
How can I solve this issue?
Thank you
Luigi

>>>

A <- c('a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'b', 'c', 'd', 'b', 'c', 'd')
B <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
C <- c(0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1)
D <- c(4, 120, 7, 23, 4, 24, 3, 12, 7, 1, 1, 5, 0, 0)
E <- c(0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1)
DF <- data.frame(A, B, C, D, E, stringsAsFactors = FALSE)
library(lattice)
barchart(
    A ~ D|E,
    DF,
    groups = C,
    stack = TRUE,
    horizontal = TRUE,
    main = "Comparison of test results",
    xlab = "Count",
    col = c("yellow", "orange"),
    par.settings = list(
        strip.background = list(col="light grey"),
        superpose.polygon=list(col= c("yellow", "orange"))
    ),
    scales = list(
        alternating = FALSE
    ),
    key = list(
        space="top",
        columns=2,
        text=list(c("Single infections", "Multiple infections"),
col="black"),
        rectangles=list(col=c("yellow", "orange"))
    ),
    strip = strip.custom(factor.levels = c("Cases","Controls"),
                         strip.levels=TRUE,
                         strip.names=FALSE,
                         par.strip.text = list(cex = 1)
    )
)

______________________________________________
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.



More information about the R-help mailing list