簡體   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