簡體   English   中英

ggplot2:在分面條形圖中刪除未使用的因子,但在構面之間沒有不同的條寬

[英]ggplot2: Drop unused factors in a faceted bar plot but not have differing bar widths between facets

df <- structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 
5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L), .Label = c("1", 
"2", "3", "4", "5", "6", "7"), class = "factor"), TYPE = structure(c(1L, 
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 
1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 
5L, 6L, 1L, 2L, 3L), .Label = c("1", "2", "3", "4", "5", "6", 
"7", "8"), class = "factor"), TIME = structure(c(2L, 2L, 2L, 
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
1L, 1L, 1L), .Label = c("1", "5", "15"), class = "factor"), VAL = c(0.937377670081332, 
0.522220720537007, 0.278690102742985, 0.967633064137772, 0.116124767344445, 
0.0544306698720902, 0.470229141646996, 0.62017166428268, 0.195459847105667, 
0.732876230962574, 0.996336271753535, 0.983087373664603, 0.666449476964772, 
0.291554537601769, 0.167933790013194, 0.860138458199799, 0.172361251665279, 
0.833266809117049, 0.620465772924945, 0.786503327777609, 0.761877260869369, 
0.425386636285111, 0.612077651312575, 0.178726130630821, 0.528709076810628, 
0.492527724476531, 0.472576208412647, 0.0702785139437765, 0.696220921119675, 
0.230852259788662, 0.359884874196723, 0.518227979075164, 0.259466265095398, 
0.149970305617899, 0.00682218233123422, 0.463400925742462, 0.924704828299582, 
0.229068386601284)), .Names = c("ID", "TYPE", "TIME", "VAL"), row.names = c(NA, 
-38L), class = "data.frame")

如果我創建以下圖:

ggplot(df, aes(x=ID, y=VAL, fill=TYPE)) +
  facet_wrap(~ TIME, ncol=1) +
  geom_bar(position="stack") +
  coord_flip()

情節

然后,我決定理想的是,我希望將任何因素都顯示在他們沒有任何數據的方面。 我已經引用了各種問題和答案,說scale="free"方法是要采用的方法(而不是drop=TRUE ,它會丟棄與TIME未使用的值相對應的空面),所以接下來:

ggplot(df, aes(x=ID, y=VAL, fill=TYPE)) +
  facet_wrap(~TIME, ncol=1, scale="free") +
  geom_bar(position="stack") +
  coord_flip()

情節

我的問題是如何防止對於具有4個小節的小平面與具有3個小節的小平面發生的小節的重新縮放。 在這個人為的例子中效果是微妙的,更糟糕​​的是我的實際數據。 理想的輸出將具有在垂直軸上具有ID因子1,4和6的底面,其中條與頂面具有相同的寬度,因此小面的整體垂直尺寸將減小。

如果你可以幫我解決為什么計算堆積而不是數值(現在修復)的加分點

賞金更新:

正如我在后續問題中提到的,看起來更好的解決方案可能涉及使用ggplot_buildggplot_table並修改gtable對象。 我很確定我能算出時間,但我希望賞金可能會激勵別人幫助我。 Koshke發布了一些這方面的例子。

這個怎么樣:

df$w <- 0.9
df$w[df$TIME == 5] <- 0.9 * 3/4
ggplot(df, aes(x=ID, y=VAL, fill=TYPE)) +
   facet_wrap(~TIME, ncol=1, scale="free") +
   geom_bar(position="stack",aes(width = w),stat = "identity") +
   coord_flip()

在此輸入圖像描述

不確定我是否在那里得到算術,但你明白了。

如果您對垂直條紋沒問題,那么facet_grid可以完美地運行:

ggplot(df, aes(x=ID, y=VAL, fill=TYPE)) +
  facet_grid(.~TIME, scale="free_x", space = "free_x") +
  geom_bar(position="stack")

在此輸入圖像描述

為了更進一步,您可以旋轉圖表的所有元素。 如果你把頭轉90°,你現在有了你想要的東西:-)

ggplot(df, aes(x=ID, y=VAL, fill=TYPE)) +
  facet_grid(.~TIME, scale="free_x", space = "free_x") +
  geom_bar(position="stack") +
  opts(legend.text = theme_text(angle=90),
       legend.title = theme_text(angle=90),
       strip.text.x = theme_text(angle=90),
       axis.text.x = theme_text(angle=90),
       axis.text.y = theme_text(angle=90),
       axis.title.x = theme_text(angle=90)
       )

在此輸入圖像描述

自原始問題起五年多以來,所以認為它應該提供更新,更清潔的版本。 這大致遵循這里給出的建議: http//ggplot2.tidyverse.org/reference/facet_grid.html

兩個關鍵參數是space="free_y" ,它允許每個面板的高度與scales="free_y"的長度成比例, scales="free_y"允許每個列的刻度變化。

coord_flip()允許水平繪制條形圖,並且作為個人偏好,在theme選項內旋轉小平面的標簽。

ggplot(df, aes(x=ID, y=VAL, fill=TYPE)) +
  geom_bar(stat="identity") +
  coord_flip() +
  facet_grid(TIME~., scales = "free_y", space = "free_y") +
  theme(strip.text.y = element_text(angle = 0))

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM