簡體   English   中英

是否可以使用 2 個垂直 y 軸繪制 plot 堆疊條形圖?

[英]Is it possible to plot stacked bar plots with 2 vertical y-axes?

我正在嘗試 plot 具有 2 個垂直 y 軸和共享 x 軸的堆疊條形圖,類似於 Mashaki 等人的下圖。 RNA-Seq 分析揭示了與 kabuli 鷹嘴豆 (Cicer arietinum L.) 中干旱脅迫反應相關的基因 PLoS ONE 13(6):e0199774。 DOI:10.1371/journal.pone.0199774。

Mashaki 等人。 RNA-Seq 分析揭示了與 kabuli 鷹嘴豆 (Cicer arietinum L.) 中干旱脅迫反應相關的基因 PLoS ONE 13(6):e0199774。 DOI:10.1371/journal.pone.0199774

我可以使用 plotly 關閉:

Plotly-stacked 條形圖

這可能使用 plotly 或 ggplot 嗎?

謝謝!

使用ggplot2實現所需的一種選擇是創建堆疊條形圖並使用labels=abs在 y 軸的“第二”或負部分上獲得正值。

使用一些假的示例數據:

dat <- data.frame(
  cell = rep(LETTERS[1:6], 2),
  type = rep(c("down", "up"), each = 6),
  DEG.DOWN = 1:12
)
dat$DEG.DOWN <- dat$DEG.DOWN * ifelse(dat$type == "up", 1, -1)

library(ggplot2)

dat$type <- factor(dat$type, c("up", "down"))

p <- ggplot(dat, aes(cell, DEG.DOWN, fill = type)) +
  geom_col(width = .6) +
  geom_hline(yintercept = 0) +
  scale_y_continuous(breaks = seq(-6, 12, 2), labels = abs) +
  labs(y = "DEG.DOWN")
p

使用ggplotly ,您可以輕松地將 plot 轉換為plotly圖表:

plotly::ggplotly()

如果您還想要軸的單獨標題,則需要做更多的工作。 在那種情況下,我會 go 使用facet_grid的方法,我使用主題選項擺脫了 facet 外觀,這樣最終它看起來像一個面板或 plot。注意,只是為了書呆子,我使用ggh4x::scale_y_facet來設置expand為每個方面分別論證。

library(ggh4x)

p +
  facet_grid(type~., scales = "free_y", space = "free_y", switch = "y") +
  ggh4x::scale_y_facet(type == "up", breaks = seq(0, 12, 2), expand = c(0, 0, 0, 1)) +
  ggh4x::scale_y_facet(type == "down", expand = c(0, 1, 0, 0), labels = abs) +
  theme(strip.placement = "outside", 
        strip.background.y = element_blank(), panel.spacing.y = unit(0, "pt")) +
  guides(fill = "none")

一個缺點是plotly不支持所有ggplot2選項,因此通過ggplotly進行的轉換並不像第一種情況那樣完美:

plotly::ggplotly()

暫無
暫無

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

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