[英]Is it possible to plot stacked bar plots with 2 vertical y-axes?
使用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.