[英]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.