简体   繁体   中英

Equally spaced bars in grouped barchart

I am having a hard time trying to make a grouped lattice barchart with all bars having the same width and space - despite groups of unequal size. From my example data (below), you will see that Sample_4 and Sample_5 are in one group (Site_2) and represented by thick bars that are close together, while Sample_1/9/10 (in group Site_4) are represented by narrow bars far apart from each other. However, I would like the bars to be equidistant in each group. I am pretty sure that

scales = list(y = list(relation = "free"))

is the right argument to go with, but I cannot figure out what to add to the code to make the figure look as intended. I am grateful for your every suggestion.

Below please find my code:

library(lattice)

combDataLong <- structure(list(Sample_ID = c("Sample_1", "Sample_2", "Sample_3", 
"Sample_4", "Sample_5", "Sample_6", "Sample_7", "Sample_8", "Sample_9", 
"Sample_10", "Sample_1", "Sample_2", "Sample_3", "Sample_4", 
"Sample_5", "Sample_6", "Sample_7", "Sample_8", "Sample_9", "Sample_10", 
"Sample_1", "Sample_2", "Sample_3", "Sample_4", "Sample_5", "Sample_6", 
"Sample_7", "Sample_8", "Sample_9", "Sample_10"), Site = c("Site_4", 
"Site_1", "Site_3", "Site_2", "Site_2", "Site_1", "Site_3", "Site_3", 
"Site_4", "Site_4", "Site_4", "Site_1", "Site_3", "Site_2", "Site_2", 
"Site_1", "Site_3", "Site_3", "Site_4", "Site_4", "Site_4", "Site_1", 
"Site_3", "Site_2", "Site_2", "Site_1", "Site_3", "Site_3", "Site_4", 
"Site_4"), Taxon = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Bacteria", "Viruses", "Archaea"
), class = "factor"), value = c(95.8486352815251, 95.4373825865372, 
93.4063075322181, 87.9417155147605, 98.067368256328, 99.5831411059118, 
91.5688260235708, 96.863504356244, 98.8303413881639, 98.549805848324, 
4.11768505361064, 4.51053698640329, 6.52165212519011, 12.0295057633177, 
1.89937795860882, 0.37731496737626, 8.38220728490824, 3.09777347531462, 
1.07984742664539, 1.41215627228782, 0.0336796648642663, 0.0520804270595019, 
0.0720403425918514, 0.0287787219218128, 0.0332537850631822, 0.0395439267119225, 
0.0489666915209722, 0.0387221684414327, 0.0898111851906639, 0.0380378793882241
)), row.names = c(NA, -30L), .Names = c("Sample_ID", "Site", 
"Taxon", "value"), class = "data.frame")


barchart(Sample_ID ~ value | Site,
     groups=Taxon,
     data=combDataLong,
     stack = T,
     as.table = T,
     scales =list(y = list(relation = "free"))
     )

You need to specify custom prepanel and panel functions in order to achieve this. Here is some code that should meet your requirements. Note that it's heavily inspired by a related question about "Removing per-panel unused factors in a bar chart with nested factors" . Also, make sure that 'Sample_ID' is a factor with its levels ordered correctly to avoid inconsistent axis labeling ( ie , starting with 'Sample_1', 'Sample_10', 'Sample_2', etc.).

## create ordered factor levels
combDataLong$Sample_ID <- factor(combDataLong$Sample_ID)
lvl <- strsplit(levels(combDataLong$Sample_ID), "_")
lvl <- as.integer(sapply(lvl, "[[", 2))
levels(combDataLong$Sample_ID) <- levels(combDataLong$Sample_ID)[order(lvl)]

## display data
barchart(Sample_ID ~ value | Site, groups = Taxon, data = combDataLong,
         scales = list(y = list(relation = "free")),
         # prepare custom y-axis
         prepanel = function(x, y, ...) {
           yy <- y[, drop = TRUE]
           list(ylim = levels(yy),
                yat = sort(unique(as.numeric(yy))))
         }, 
         # drop unneeded factor levels
         panel = function(x, y, ...) {
           yy <- y[, drop = TRUE]
           panel.barchart(x, yy, ...)
         }, stack = TRUE, as.table = TRUE)

条形图

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