繁体   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