简体   繁体   English

分组条形图中等距的条

[英]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. 从下面的示例数据中,您将看到Sample_4和Sample_5在一个组中(Site_2),并由彼此靠近的粗条表示,而Sample_1 / 9/10(在Site_4组中)由相距较远的窄条表示彼此。 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. prepanel ,您需要指定自定义prepanelpanel功能。 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.). 另外,请确保“ Sample_ID”是其级别正确排序的一个factor ,以避免轴标签不一致( ,以“ Sample_1”,“ Sample_10”,“ Sample_2”等开头)。

## 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)

条形图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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