分组条形图中的等间距条形

时间:2016-02-03 20:42:28

标签: r bar-chart lattice

我很难尝试制作一个分组格子条形图,所有条形图都具有相同的宽度和空间 - 尽管大小不等。从我的示例数据(下面)中,您将看到Sample_4和Sample_5在一个组(Site_2)中,并且由靠近在一起的粗条表示,而Sample_1 / 9/10(在组Site_4中)由相距很远的窄条表示彼此。但是,我希望每组中的条形是等距的。我很确定

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

是正确的参数,但我无法弄清楚要添加到代码中的内容,以使图形看起来像预期的那样。我很感激你的每一个建议。

以下是我的代码:

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

1 个答案:

答案 0 :(得分:0)

您需要指定自定义prepanelpanel函数才能实现此目的。以下是一些符合您要求的代码。请注意,它受到有关"Removing per-panel unused factors in a bar chart with nested factors"的相关问题的启发。另外,请确保'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)

barchart