【问题标题】:Borderless merge and adjusted plot size with ggplot2 + gridExtra使用 ggplot2 + gridExtra 进行无边界合并和调整绘图大小
【发布时间】:2020-06-17 05:27:17
【问题描述】:

我想将 3 个地块无缝地合并在一起。我的问题是情节略有偏移,即使我没有边界地绘制它们,它们也不会在情节 1 和 2/情节 2 和 3 之间无缝合并。

library(tidyverse)
library(gridExtra)

plot1 <- ggplot(df) +
  theme_dark() +
  geom_line(aes(y = Price, x = time, color = "#00FFFF"), size = 0.7) +
  geom_area(aes(y = Price, x = time), fill = "#00FFFF", alpha = .1) +
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        legend.position = c(.1, .95),
        legend.justification = c("right", "top"),
        legend.box.just = "right",
        legend.margin = margin(3, 3, 3, 3),
        plot.margin = margin(0,-0.5,-0.5,-0.5),
        plot.background = element_rect(fill = "#808080")) +
  scale_colour_manual(name = "Price", 
                      values = c("#00FFFF" = "#00FFFF"), labels = c("Stock")) +
  labs(y = "")
#  
plot1
#
plot2 <- ggplot(df) +
  theme_dark() +
  geom_bar(aes(x = time, y = B, fill = "green"), stat = "identity") +
  geom_bar(aes(x = time, y = S, fill = "red"), stat = "identity") +
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        panel.border = element_blank(),
        legend.position = c(.1, .95),
        legend.justification = c("right", "top"),
        legend.box.just = "right",
        legend.margin = margin(3, 3, 3, 3),
        plot.margin = margin(-0.75,-0.5,0,-0.5),
        plot.background = element_rect(fill = "#808080")) +
  scale_fill_identity(name = "Volume",
                      guide = "legend", labels = c("B", "S")) +
  labs(y = "")
#
plot2
#
plot3 <- ggplot(df) +
  theme_dark() +
  geom_line(aes(y = EMA_short, x = time, color = "blue"), size = 0.7) +
  geom_area(aes(y = EMA_short, x = time), fill = "blue", alpha = .1) +
  geom_line(aes(y = EMA_long, x = time, color = "yellow"), size = 0.7) +
  geom_area(aes(y = EMA_long, x = time), fill = "yellow", alpha = .1) +
  theme(axis.title.x=element_blank(),
        axis.ticks.x=element_blank(),
        panel.border = element_blank(),
        legend.position = c(.1, .95),
        legend.justification = c("right", "top"),
        legend.box.just = "right",
        legend.margin = margin(3, 3, 3, 3),
        plot.margin = margin(-0.75,-0.5,2,-0.5),
        plot.background = element_rect(fill = "#808080")) +
  scale_colour_manual(name = "EMA", 
                      values = c("blue" = "blue", "yellow" = "yellow"), labels = c("EMA50", "EMA200")) +
  labs(y = "")
#
plot3
#
gA <- ggplotGrob(plot1)
gB <- ggplotGrob(plot2)
gC <- ggplotGrob(plot3)
grid::grid.newpage()
grid::grid.draw(rbind(gA, gB, gC))

在我看来,我希望这 3 个图以类似于此处的方式相互合并:

感谢您的任何建议。

编辑

感谢您提供非常有帮助的答案。我现在只有两个小的光学问题。尽管我将所有图例定位相同,但它们位于其他位置。我应该将它们全部格式化成相同的大小吗?可惜左边有两个小白点,如图。我已经尝试让边缘越来越小,但那里的斑点总是保持白色。我在上面更新了我的新代码。谢谢!

【问题讨论】:

标签: r ggplot2 gridextra


【解决方案1】:

你应该考虑 expand(c(0,0)) 和/或 theme(plot.margin = c(t,r,b,l))

首先,为每个绘图添加 x 和 y 扩展参数,以抑制数据周围的空白区域,这是 ggplot 的默认设置:

plot1 + scale_y_continuous("", expand = c(0,0)) 
# we indicate here 'no label', so you should supress '+ ylab()'
plot1 + scale_x_datetime("", expand = c(0,0)) 
# same, you should supress 'xlab()' from the plots
  1. 这让您可以通过 grid.arrange 在绘图之间添加最小边距,您可以使用 theme(plot.margin = c(t,r,b,l))(例如 plot1 +theme(plot.margin(-0.5,-0.5,-0.5,-0.5)))调整这些边距。在您的情况下,您可能需要处理 2 个地块的顶部和/或底部(如果另外 2 个地块没有边距,请小心中间的一个)。
  2. 有时您必须使用输出大小(grid.arrange 的高度和宽度),通常是在您保存一个排列组合对象时(例如,ggsave( arrangeGrob(plot1, plot2)) 需要处理大小)。
  3. 请注意,您必须将图例放在图的顶部、底部或内部,以便以相同的“数据区域”大小堆叠图。当图例的大小不同时,您不能将带有图例的图堆叠在左侧或右侧位置。因此,为您的 3 个地块添加 +theme(legend.position = 'top'),或添加 bottom 或某个坐标。

【讨论】:

  • 谢谢!我在我的代码中实现它有点麻烦。等我回家再试。
  • 不客气。我建议逐步测试,以了解绘图的哪一部分受到影响:(a)expand() in scale_y 或 scale_x 删除绘图内数据区域和轴之间的空间;和 (b) theme(plot.margin= 让您从绘图的外部(在轴标签之后)裁剪外部边距;和 (c),您想要的输出图例 [黑色主题] 在图中(因此请指示坐标,如 theme(legend.position= c(0.95,0.1))
  • 我明白了。如上所示,我现在只有两个小的光学问题。也许你也有解决方案。
【解决方案2】:

1- 您通过在 theme() 参数中指定参数来制作自定义图例,例如,

plot1 + theme(legend.background = element_blank(), legend.text.align=0, legend.title.align = 0))
#a legend with no white background, left text and title alignment. 

2-图例行带有guides()参数的区域,以表明您非常特殊的键:

# (e.g., remove the little grey area background for the legend):
 plot1 + guides(color=guide_legend(override.aes=list(fill=NA)))

3- 如有必要,请阅读 theme()guides() 函数的一些参数,以了解 - 倍数 - 主题参数并创建您的 personnal-darky-style-func()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多