简体   繁体   中英

How to do group plot using lattice in R?

在此处输入图片说明

Note: I am trying to plot grouped bargraph to compare 2012, 2013 and 2013 prescriptions data which are the x axis of the plot and the prescription id should be the y axis. I heard that lattice is a useful tool in R to create group however the code that I am using does not presents a suitable graph that I want to.

Please check my code:

require(lattice)
barchart(prep2014~prep2012,data=netTable,groups=prepChemical)
barchart(prep2014~prep2012, groups=prepChemical, netTable, auto.key = list(columns = 3))

The graph I am getting is below which does not make sense [![enter image description here][2]][2]

Dput gives:

 structure(list(formula = prep2014 ~ prep2012, as.table = FALSE, 
aspect.fill = TRUE, legend = structure(list(top = structure(list(
    fun = "drawSimpleKey", args = structure(list(text = c("1", 
    "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", 
    "13", "14", "15", "16", "17", "18", "19", "20", "21"), 
        points = FALSE, rectangles = TRUE, lines = FALSE, 
        columns = 3), .Names = c("text", "points", "rectangles", 
    "lines", "columns"))), .Names = c("fun", "args"))), .Names = "top"), 
panel = "panel.barchart", page = NULL, layout = NULL, skip = FALSE, 
strip = FALSE, strip.left = FALSE, xscale.components = function (lim, 
    packet.number = 0, packet.list = NULL, top = TRUE, ...) 
{
    comps <- calculateAxisComponents(lim, packet.list = packet.list, 
        packet.number = packet.number, ...)
    list(num.limit = comps$num.limit, bottom = list(ticks = list(at = comps$at, 
        tck = 1), labels = list(at = comps$at, labels = comps$labels, 
        check.overlap = comps$check.overlap)), top = top)
}, yscale.components = function (lim, packet.number = 0, 
    packet.list = NULL, right = TRUE, ...) 
{
    comps <- calculateAxisComponents(lim, packet.list = packet.list, 
        packet.number = packet.number, ...)
    list(num.limit = comps$num.limit, left = list(ticks = list(at = comps$at, 
        tck = 1), labels = list(at = comps$at, labels = comps$labels, 
        cex = 1, check.overlap = comps$check.overlap)), right = right)
}, axis = function (side = c("top", "bottom", "left", "right"), 
    scales, components, as.table, labels = c("default", "yes", 
        "no"), ticks = c("default", "yes", "no"), ..., prefix = lattice.getStatus("current.prefix")) 
{
    side <- match.arg(side)
    labels <- match.arg(labels)
    ticks <- match.arg(ticks)
    row <- lattice.getStatus("current.focus.row", prefix = prefix)
    column <- lattice.getStatus("current.focus.column", prefix = prefix)
    panel.layout <- trellis.currentLayout("panel", prefix = prefix)
    layout.dim <- dim(panel.layout)
    determineStatus <- function(x) {
        if (is.null(x) || (is.logical(x) && !x)) 
            FALSE
        else TRUE
    }
    lastPanel <- function() {
        ((pn <- panel.number(prefix = prefix)) > 0 && pn == 
            max(panel.layout))
    }
    atBoundary <- function() {
        switch(side, top = if (as.table) row == 1 else row == 
            layout.dim[1], bottom = if (!as.table) row == 
            1 else row == layout.dim[1], left = column == 
            1, right = column == layout.dim[2] || lastPanel())
    }
    do.ticks <- switch(ticks, yes = TRUE, no = FALSE, default = scales$draw && 
        determineStatus(components[[side]]) && (if (scales$relation == 
        "same") atBoundary() else TRUE))
    do.labels <- switch(labels, yes = TRUE, no = FALSE, default = scales$draw && 
        (if (scales$relation == "same") {
            atBoundary() && switch(side, top = rep(scales$alternating, 
              length.out = column)[column] %in% c(2, 3), 
              bottom = rep(scales$alternating, length.out = column)[column] %in% 
                c(1, 3), left = rep(scales$alternating, length.out = row)[row] %in% 
                c(1, 3), right = rep(scales$alternating, 
                length.out = row)[row] %in% c(2, 3))
        } else TRUE))
    if (do.ticks || do.labels) {
        comp.list <- switch(side, top = if (is.logical(components[["top"]]) && 
            components[["top"]]) components[["bottom"]] else components[["top"]], 
            bottom = components[["bottom"]], left = components[["left"]], 
            right = if (is.logical(components[["right"]]) && 
              components[["right"]]) components[["left"]] else components[["right"]])
        scales.tck <- switch(side, left = , bottom = scales$tck[1], 
            right = , top = scales$tck[2])
        if (!is.logical(comp.list)) {
            if (do.ticks) 
              panel.axis(side = side, at = comp.list$ticks$at, 
                labels = FALSE, draw.labels = FALSE, check.overlap = FALSE, 
                outside = TRUE, ticks = TRUE, tck = scales.tck * 
                  comp.list$ticks$tck, ...)
            if (do.labels) 
              panel.axis(side = side, at = comp.list$labels$at, 
                labels = comp.list$labels$labels, draw.labels = TRUE, 
                check.overlap = comp.list$labels$check.overlap, 
                outside = TRUE, ticks = FALSE, tck = scales.tck * 
                  comp.list$ticks$tck, ...)
        }
    }
}, xlab = "prep2012", ylab = NULL, xlab.default = "prep2012", 
ylab.default = "prep2014", xlab.top = NULL, ylab.right = NULL, 
main = NULL, sub = NULL, x.between = 0, y.between = 0, par.settings = NULL, 
plot.args = NULL, lattice.options = NULL, par.strip.text = NULL, 
index.cond = list(1L), perm.cond = 1L, condlevels = list(
    "1"), call = barchart(prep2014 ~ prep2012, groups = prepChemical, 
    netTable, auto.key = list(columns = 3)), x.scales = structure(list(
    draw = TRUE, axs = "r", tck = c(1, 1), tick.number = 5, 
    at = FALSE, labels = FALSE, log = FALSE, alternating = c(1, 
    2), relation = "same", abbreviate = FALSE, minlength = 4, 
    limits = NULL, format = NULL, equispaced.log = TRUE, 
    lty = FALSE, lwd = FALSE, cex = c(FALSE, FALSE), rot = c(FALSE, 
    FALSE), col = FALSE, col.line = FALSE, alpha = FALSE, 
    alpha.line = FALSE, font = FALSE, fontfamily = FALSE, 
    fontface = FALSE, lineheight = FALSE), .Names = c("draw", 
"axs", "tck", "tick.number", "at", "labels", "log", "alternating", 
"relation", "abbreviate", "minlength", "limits", "format", 
"equispaced.log", "lty", "lwd", "cex", "rot", "col", "col.line", 
"alpha", "alpha.line", "font", "fontfamily", "fontface", 
"lineheight")), y.scales = structure(list(draw = TRUE, axs = "r", 
    tck = c(0, 0), tick.number = 5, at = FALSE, labels = FALSE, 
    log = FALSE, alternating = 1, relation = "same", abbreviate = FALSE, 
    minlength = 4, limits = NULL, format = NULL, equispaced.log = TRUE, 
    lty = FALSE, lwd = FALSE, cex = c(FALSE, FALSE), rot = c(0, 
    0), col = FALSE, col.line = FALSE, alpha = FALSE, alpha.line = FALSE, 
    font = FALSE, fontfamily = FALSE, fontface = FALSE, lineheight = FALSE), .Names = c("draw", 
"axs", "tck", "tick.number", "at", "labels", "log", "alternating", 
"relation", "abbreviate", "minlength", "limits", "format", 
"equispaced.log", "lty", "lwd", "cex", "rot", "col", "col.line", 
"alpha", "alpha.line", "font", "fontfamily", "fontface", 
"lineheight")), panel.args.common = structure(list(box.ratio = 2, 
    horizontal = TRUE, groups = 1:21), .Names = c("box.ratio", 
"horizontal", "groups")), panel.args = list(structure(list(
    x = structure(c(16L, 21L, 1L, 3L, 9L, 2L, 14L, 10L, 18L, 
    8L, 5L, 19L, 13L, 4L, 6L, 20L, 15L, 7L, 12L, 17L, 11L
    ), .Label = c("1,109,277.10", "1,136,072.80", "1,878,291.10", 
    "122,518.00", "137,858.50", "18,552.50", "188,422.40", 
    "203,925.60", "236,988.10", "238,260.70", "247,737.30", 
    "250,406.90", "306,481.30", "367,071.60", "41,854.50", 
    "454,206.40", "50,206.50", "581,181.70", "76,140.70", 
    "9.3", "979,614.00"), class = "factor"), y = structure(c(18L, 
    1L, 2L, 4L, 12L, 3L, 17L, 11L, 20L, 10L, 7L, 21L, 15L, 
    6L, 9L, 5L, 16L, 8L, 14L, 19L, 13L), .Label = c("1,003,463.90", 
    "1,122,447.30", "1,224,148.10", "1,875,179.50", "10.6", 
    "125,543.40", "139,469.50", "193,552.50", "21,001.40", 
    "212,158.60", "231,887.80", "236,020.20", "265,218.00", 
    "285,907.70", "298,125.80", "33,870.00", "340,425.40", 
    "492,199.20", "52,339.30", "621,485.90", "78,120.80"), class = "factor"), 
    subscripts = 1:21), .Names = c("x", "y", "subscripts"
))), packet.sizes = 21, x.limits = c(-0.4, 22.4), y.limits = c("1,003,463.90", 
"1,122,447.30", "1,224,148.10", "1,875,179.50", "10.6", "125,543.40", 
"139,469.50", "193,552.50", "21,001.40", "212,158.60", "231,887.80", 
"236,020.20", "265,218.00", "285,907.70", "298,125.80", "33,870.00", 
"340,425.40", "492,199.20", "52,339.30", "621,485.90", "78,120.80"
), x.used.at = NULL, y.used.at = NULL, x.num.limit = NULL, 
y.num.limit = NULL, aspect.ratio = 1, prepanel.default = "prepanel.default.bwplot", 
prepanel = NULL), .Names = c("formula", "as.table", "aspect.fill", 
  "legend", "panel", "page", "layout", "skip", "strip", "strip.left", 
   "xscale.components", "yscale.components", "axis", "xlab", "ylab", 
  "xlab.default", "ylab.default", "xlab.top", "ylab.right", "main", 
  "sub", "x.between", "y.between", "par.settings", "plot.args", 
  "lattice.options", "par.strip.text", "index.cond", "perm.cond", 
   "condlevels", "call", "x.scales", "y.scales", "panel.args.common", 
   "panel.args", "packet.sizes", "x.limits", "y.limits", "x.used.at", 
    "y.used.at", "x.num.limit", "y.num.limit", "aspect.ratio",     "prepanel.default", 
    "prepanel"), class = "trellis")

I think the answer lies in reshaping the data.

library(reshape2)
#turn data to long format
netTable_m <- melt(netTable,id.var="prepChemical")

barchart(prepChemical~value,data=netTable_m, groups=variable, auto.key=T)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM