繁体   English   中英

分组条形图中等距的条

[英]Equally spaced bars in grouped barchart

我很难制作一个分组的格子条形图,其中所有条形具有相同的宽度和空间-尽管大小不等的组。 从下面的示例数据中,您将看到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"))
     )

prepanel ,您需要指定自定义prepanelpanel功能。 这是一些应该满足您要求的代码。 请注意,它的灵感来自一个有关“用嵌套因子删除条形图中的每面板未使用因子”的相关问题。 另外,请确保“ 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