【问题标题】:Align vertically and horizontally plot R ggplot2垂直和水平对齐绘图R ggplot2
【发布时间】:2017-03-06 15:21:46
【问题描述】:

我有一个特定的问题(至少我喜欢这样认为:))。我想在两列和两行中对齐三个图。对齐应该在中心图之后(见图;图 C),其中图 A 应该在图 C 的 x 轴之后对齐,图 D 应该在图 C 的 y 轴之后对齐。注意,没有图 B,这应该留空。

数据:

a <- data.frame(
  id = 1:15,
  GO = c(
    "phosphoglycerate kinase", "phosphoglycerate kinase", 
    "phosphoglycerate kinase", "phosphoglycerate kinase", "phosphoglycerate kinase", 
    "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", 
    "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", "allantoicase", 
    "allantoicase", "allantoicase", "allantoicase", "allantoicase"),
  variable = c(
    "d64", "d31", "d16", "d9", "d0", "d64", "d31", "d16", "d9", "d0", "d64", "d31", "d16", "d9", "d0"),
  value = c(
    154.28239, 226.04355, 245.67728, 271.82375, 270.83519, 289.01809, 491.66461,
    485.28291, 351.3759, 510.96043, 22.75253, 31.66546, 129.50564, 206.6651, 32.43769),
  relAbundByGO = c(
    13.201624, 19.342078, 21.022096, 23.259395, 23.174806, 13.57975, 23.101262,
    22.801413, 16.509683, 24.007892, 5.378513, 7.485456, 30.614078, 48.853948, 7.668005),
  GOd = c(
    "phosphoglycerate kinase", "phosphoglycerate kinase", 
    "phosphoglycerate kinase", "phosphoglycerate kinase", "phosphoglycerate kinase", 
    "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", 
    "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", "allantoicase", 
    "allantoicase", "allantoicase", "allantoicase", "allantoicase"
  ))

b <- data.frame(
  id = 1:15,
  Compound = c(
    "C5-C10", "C5-C10", "C5-C10", 
    "C5-C10", "C5-C10", "C10-C20", "C10-C20", "C10-C20", "C10-C20", 
    "C10-C20", "BTEX", "BTEX", "BTEX", "BTEX", "BTEX"),
  Degradation = c(
    100, 100, 23.5, 5.6, 0, 100, 100, 67.2, 19, 0.6, 100, 100, 88.7, 43.3, 0.1),
  st ()dev = c(
    0, 0, 35, 12.4, 0, 0, 0, 19.3, 13.1, 0.6, 0, 0, 33.4, 43.4, 0.2),
  day = c(
    "NSWOD-0", "NSWOD-64", "NSOD-9", 
    "NSOD-16", "NSOD-31", "NSWOD-0", "NSWOD-64", "NSOD-9", "NSOD-16", 
    "NSOD-31", "NSWOD-0", "NSWOD-64", "NSOD-9", "NSOD-16", "NSOD-31"))

这是cowplot的尝试:

a$GO <- factor(a$GO, levels = a$GO) #keep the same order as in table
a$variable <- factor(a$variable, levels = c("d0", "d64", "d9", "d16", "d31"))
p1 <- ggplot(data = a, aes(x=variable, y=GO)) + 
  geom_tile(aes(fill=relAbundByGO), colour = "white") + ylab("Gene ontology") + 
  scale_fill_gradient(name="Relative\nabundance of TPM", low = "green", high = "red", limits=c(0, 100), na.value="transparent") +
  scale_x_discrete("Sample", 
                   breaks = c("d0", "d64", "d9", "d16", "d31"),
                   labels = c("CTRL-0", "CTRL-64", "CEWAF-9","CEWAF-16","CEWAF-31")) + 
  theme(legend.position="left")
p1

a$GO <- factor(a$GO, levels = a$GO) #keep the same order as in table
p2 <- ggplot(data = a, aes(x=GO, y=value)) +
  geom_bar(stat = "identity") +
  theme(axis.text.x = element_text(angle = 0)) +
  ylab("Cumulative TPM abundance") +
  theme(axis.text.y=element_blank(),
        axis.title.y=element_blank()) +
  coord_flip()
p2

b$day <- factor(b$day, levels = b$day) #keep the same order as in table
p3 <- ggplot(b, aes(x=day, y=Degradation, color=Compound, group=Compound)) + 
  geom_point(size=4, shape=21, fill="white") + 
  geom_line(size=0.7) + 
  ylab("Hydrocarbon content (%)") + 
  geom_errorbar(aes(ymax=Degradation + stdev, ymin=Degradation-stdev), linetype="dashed", lwd=.6, width=.4) +
  theme_bw(base_size = 12, base_family = "Helvetica") +
  theme(axis.text.x=element_blank()
        ,axis.ticks=element_blank()
        ,axis.title.x=element_blank(),
        legend.position="left") + 
  scale_color_discrete(name="Hydrocarbon\ngroup",
                       breaks=c("C5-C10", "C10-C20", "BTEX", "PAHs"))
p3

legend_p1 <- get_legend(p1)
legend_p3 <- get_legend(p3)

p1 <- p1 + theme(legend.position='none')
p3 <- p3 + theme(legend.position='none')

cowplot::plot_grid(
  cowplot::plot_grid(legend_p3, legend_p1, ncol = 1),
  cowplot::plot_grid(p3, NULL, p1, p2,  ncol = 2, nrow = 2, rel_widths = c(1, 0.75, 1, 0.75), labels = c('A', '', 'C', 'D'), align = "hv"),
  rel_widths = c(0.16, 1))

我想让情节 D 非常接近情节 C

否认

【问题讨论】:

  • A 和 C 的 x 轴是否使用相同的值?看这个例子,一切都很好:library(ggplot2); library(cowplot); a &lt;- ggplot(cars, aes(x = speed)) + geom_bar(); c &lt;- ggplot(cars, aes(x = speed, y = dist)) + geom_point(); d &lt;- ggplot(cars, aes(x = dist, y = speed)) + geom_point(); plot_grid(a, NULL, c, d, ncol = 2, nrow = 2, rel_widths = c(10/16, 6/16, 10/16, 6/16), labels = c('A', '', 'C', 'D'), align = "hv")
  • “希望有人可以尝试重现一些东西”,嗯,我认为您应该举一个可重现的例子。您是否收到有关对齐的消息或警告?
  • @m-dz,无论 x 轴值如何,绘图区域都将/应该对齐。不过,传说可能会带来问题。
  • @Axeman,看起来我误解了cowplot 对齐的工作原理,感谢您的纠正。从图中看起来它正在对齐整个图,包括图例和轴标签。拥有a reproducible example, as described here 会更容易。
  • @m-dz 是的,这些是相同的轴值,并且 C 和 D 具有相同的 y 轴值。您只是采用了相同的方法,但在我的情况下对齐不起作用。

标签: r ggplot2 alignment figure


【解决方案1】:

正如@Axeman 提到的,它是由传说引起的,cowplot::get_legend() 可以解决这个问题(具体情况请参见?cowplot::get_legend()):

legend_p1 <- get_legend(p1)
legend_p3 <- get_legend(p3)

p1 <- p1 + theme(legend.position='none')
p3 <- p3 + theme(legend.position='none')

cowplot::plot_grid(
  cowplot::plot_grid(legend_p1, legend_p3, ncol = 1),
  cowplot::plot_grid(p3, NULL, p1, p2,  ncol = 2, nrow = 2, rel_widths = c(1, 0.75, 1, 0.75), labels = c('A', '', 'C', 'D'), align = "hv"),
  rel_widths = c(0.1, 1))

但是要让它“可读”还需要做很多工作。

数据(“原始”,应用 OP postt 中的所有转换):

a <- data.frame(
  id = 1:15,
  GO = c(
    "phosphoglycerate kinase", "phosphoglycerate kinase", 
    "phosphoglycerate kinase", "phosphoglycerate kinase", "phosphoglycerate kinase", 
    "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", 
    "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", "allantoicase", 
    "allantoicase", "allantoicase", "allantoicase", "allantoicase"),
  variable = c(
    "d64", "d31", "d16", "d9", "d0", "d64", "d31", "d16", "d9", "d0", "d64", "d31", "d16", "d9", "d0"),
  value = c(
    154.28239, 226.04355, 245.67728, 271.82375, 270.83519, 289.01809, 491.66461,
    485.28291, 351.3759, 510.96043, 22.75253, 31.66546, 129.50564, 206.6651, 32.43769),
  relAbundByGO = c(
    13.201624, 19.342078, 21.022096, 23.259395, 23.174806, 13.57975, 23.101262,
    22.801413, 16.509683, 24.007892, 5.378513, 7.485456, 30.614078, 48.853948, 7.668005),
  GOd = c(
    "phosphoglycerate kinase", "phosphoglycerate kinase", 
    "phosphoglycerate kinase", "phosphoglycerate kinase", "phosphoglycerate kinase", 
    "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", 
    "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", "allantoicase", 
    "allantoicase", "allantoicase", "allantoicase", "allantoicase"
  ))
b <- data.frame(
  id = 1:15,
  Compound = c(
    "C5-C10", "C5-C10", "C5-C10", 
    "C5-C10", "C5-C10", "C10-C20", "C10-C20", "C10-C20", "C10-C20", 
    "C10-C20", "BTEX", "BTEX", "BTEX", "BTEX", "BTEX"),
  Degradation = c(
    100, 100, 23.5, 5.6, 0, 100, 100, 67.2, 19, 0.6, 100, 100, 88.7, 43.3, 0.1),
  st ()dev = c(
    0, 0, 35, 12.4, 0, 0, 0, 19.3, 13.1, 0.6, 0, 0, 33.4, 43.4, 0.2),
  day = c(
    "NSWOD-0", "NSWOD-64", "NSOD-9", 
    "NSOD-16", "NSOD-31", "NSWOD-0", "NSWOD-64", "NSOD-9", "NSOD-16", 
    "NSOD-31", "NSWOD-0", "NSWOD-64", "NSOD-9", "NSOD-16", "NSOD-31"))

【讨论】:

  • 非常感谢!这现在看起来更像我真正想要的。您能否指出进行相应数据转换的最佳方法?
  • 没问题!我不确定您所说的“相应的数据转换”是什么意思,您能否解释一下,或者在上面的问题中添加一个示例?
  • 我的意思是有没有一种巧妙的方法可以将原始帖子中的数据调整为您为“可读”绘图创建的数据?
  • 并非如此,通过使绘图“可读”我的意思是调整rel_widths 等,您还可以缩短GOGOd,这样它们就不会占用太多空间。还有什么...如果 plot D 只是为了显示体积比较,您可以尝试删除 x 轴。这只是为了修复情节的“视觉”方面。
  • 感谢您的帮助!还有一件事,实际上是两件事:) 在绘图时,我想让情节 D 尽可能接近情节 C,可以这样做吗?现在他们几乎分开了。在情节 A 之后还有一个“GOd”标签悬停,我想将其删除。再次感谢。
猜你喜欢
  • 2013-06-26
  • 1970-01-01
  • 2012-10-10
  • 2013-11-25
  • 2014-12-05
  • 1970-01-01
  • 2018-06-23
  • 1970-01-01
相关资源
最近更新 更多