r - 在 Shiny 的范围内将多个图在 x 轴上对齐,而不合并图

标签 r plot ggplot2 shiny vertical-alignment

我有一个 Shiny 的应用程序,它有多个具有相同 x 轴的图,图需要垂直对齐而不合并,即:图必须是彼此独立的 Shiny 对象。请参阅下面的可重现示例:

library(ggplot2)
library(shiny)

# example data
df1 <- mtcars

# my custom theme, tried many variants, seems impossible?
myTheme <- function(){
  theme(legend.position = "none",
        plot.background = element_rect(fill = "lightblue"),
        axis.text.y = element_text(margin = margin(t = 0, r = 0, b = 0, l = 2, unit = "cm")),
        axis.title.y = element_text(margin = margin(t = 0, r = 0, b = 0, l = 1, unit = "cm")),
        plot.margin = unit(c(0, 0, 0, 1), "cm")
  )}


# The App with 2 plots
runApp(
  shinyApp(
    ui = bootstrapPage(
      plotOutput('plot1', width = 800, height = 200),
      plotOutput('plot2', width = 800, height = 100)
    ),
    server = function(input, output) {
      output$plot1 <- renderPlot({
        ggplot(df1, aes(mpg, gear)) +
          geom_point() +
          coord_cartesian(xlim = c(15, 25)) +
          myTheme()
      })
      output$plot2 <- renderPlot({ 
        ggplot(df1[ df1$gear %in% c(3, 5), ], aes(mpg, gear)) +
          scale_y_continuous(name = "longName", breaks = c(3, 5), labels = c("myLongThreeeee", "My Longggg FIVE")) +
          geom_area() +
          coord_cartesian(xlim = c(15, 25)) +
          myTheme()
      })
      
    }
  ))

enter image description here

我尝试使用不同的边距设置等设置我自己的 myTheme() ,似乎这是不可能的?

如果不可能,那么 suggested here 不是一个很好的解决方案。 ,我将使用单宽度字体填充 Y 刻度标签,例如:

scale_y_continuous(labels = function(label) sprintf('%15.2f', label)) +

还有其他选择吗?


注意:我知道许多软件包(grid、cowplot、egg、patchwork 等)将绘图合并到一个 ggplot 对象中并对齐 x 轴,然后打印。就我而言,它们需要分开,因为用户可以更改一些用户界面设置,并且只有受影响的绘图才应该刷新。


TL;DR:是否可以以厘米、点为单位设置“轴标题”和“轴刻度标签”空间的固定大小,例如:1cm、4cm?


关于plotly的相关帖子:Shiny - align plots axis

最佳答案

大量借用@Lyngbakr的答案。

我们可以手动将宽度设置为固定大小。这将始终对齐绘图,但不考虑轴标签的长度。我不明白如何在不使绘图相互依赖的情况下考虑两组标签。这样,您就可以删除主题中的边距内容。

library(ggplot2)
library(shiny)

# example data
df1 <- mtcars

# my custom theme, tried many variants, seems impossible?
myTheme <- function(){
  theme(legend.position = "none",
        plot.background = element_rect(fill = "lightblue")
  )}


# The App with 2 plots
runApp(
  shinyApp(
    ui = bootstrapPage(
      plotOutput('plot1', width = 800, height = 200),
      plotOutput('plot2', width = 800, height = 100)
    ),
    server = function(input, output) {
      g1 <- ggplot(df1, aes(mpg, gear)) +
        geom_point() +
        coord_cartesian(xlim = c(15, 25)) +
        myTheme()

      t1 <- ggplot_gtable(ggplot_build(g1))

      g2 <- ggplot(df1[ df1$gear %in% c(3, 5), ], aes(mpg, gear)) +
        scale_y_continuous(name = "longName", breaks = c(3, 5), labels = c("myLongThreeeee", "My Longggg FIVE")) +
        geom_area() +
        coord_cartesian(xlim = c(15, 25)) +
        myTheme()

      t2 <- ggplot_gtable(ggplot_build(g2))

      fixed_width <- unit(4, 'cm')
      t1$widths[3] <- fixed_width
      t2$widths[3] <- fixed_width

      output$plot1 <- renderPlot(plot(t1))
      output$plot2 <- renderPlot(plot(t2))
    }
  ))

enter image description here

关于r - 在 Shiny 的范围内将多个图在 x 轴上对齐,而不合并图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48871690/

相关文章:

r - 计算网络中的周期

r - 两个选项卡中的传单输出 : LeafletProxy() doesn't render initially in 2nd tab

r - 每个值每年的百分比

r - BinaryTree (ctree, party) 的绘图忽略了 par() 的绘图选项

r - geom_col 以错误的方式绘制数据

r - 将时间序列绘制为热图

r - ggplot2错误:手动刻度中的值不足

r - 在 R 中从 opensubtitles.org 网页抓取字幕

r - 如何使用 ggplot2 在连续轴上指示组(例如染色体)?

plot - 如何设置绘图窗口的窗口大小?