ggplot2和Shiny:如何用图形大小来缩放图例的大小?

时间:2023-02-09 21:42:50

In ggplot2 the size of elements are specified separately. When the size of figure changes, the elements, for example the legend does not change. This could be a problem in Shiny when the size of output ggplot2 figures changes with browser window. Below is the code of a dummy Shiny app and two output figures at different browser window size. The smaller figure is ugly as part of its legend has been cut off.

在ggplot2中,元素的大小是单独指定的。当图形的大小发生变化时,元素(例如图例)不会改变。当输出ggplot2数字的大小随浏览器窗口而变化时,这可能是Shiny中的问题。下面是虚拟Shiny应用程序的代码和不同浏览器窗口大小的两个输出数字。较小的数字是丑陋的,因为其传奇的一部分已被切断。

Is there a method to scale the legend size with the figure size directly in ggplot2 without pre-saving the figure as an image file for Shiny apps?

有没有一种方法可以直接在ggplot2中使用图形大小缩放图例大小,而无需将图形预先保存为Shiny应用程序的图像文件?

library(shiny)
library(ggplot2)

ui <- fluidPage(
    br(), br(), br(),
    plotOutput("test", height = "auto")
)

server <- function(input, output, session) {
    output$test <- renderPlot(
        height = function() {
            0.8 * session$clientData$output_test_width
        },
        expr = {
            aaa <- ggplot(mtcars, aes(wt, mpg, color = cyl)) + 
                geom_point() + 
                theme(legend.position = c(0.9, 0.9))
            print(aaa)
        }
    )
}

shinyApp(ui, server)

The figure in a larger browser window looks good: ggplot2和Shiny:如何用图形大小来缩放图例的大小?

较大的浏览器窗口中的数字看起来很好:

But in small browser window, the top of the legend does not show up:

但是在小浏览器窗口中,图例顶部没有显示:

ggplot2和Shiny:如何用图形大小来缩放图例的大小?

1 个解决方案

#1


8  

Here's a way to anchor the top of the legend so that it doesn't run off the top of the plot area. You just add legend.justification(0.5, 1) to the ggplot theme. The first value centers the x position of the legend. The second value "top justifies" the y position of the legend. (You can right-justify the legend by changing 0.5 to 1 for the first value, which will keep the legend from running off the right edge of the graph, if that's a problem.) This doesn't solve the relative sizing issue, but the full legend will always be visible and in the same location.

这是一种锚定图例顶部的方法,使其不会在绘图区域的顶部运行。您只需将legend.justification(0.5,1)添加到ggplot主题即可。第一个值以图例的x位置为中心。第二个值“顶部证明”图例的y位置。 (您可以通过将第一个值更改为0.5来对图例进行右对齐,这将使图例不会在图表的右边缘运行,如果这是一个问题。)这不能解决相对大小问题,但是完整的图例将始终显示在同一位置。

server <- function(input, output, session) {
  output$test <- renderPlot(
    height = function() {
      0.8 * session$clientData$output_test_width
    },
    expr = {
      aaa <- ggplot(mtcars, aes(wt, mpg, color = cyl)) + 
        geom_point() + 
        theme(legend.position = c(0.9, 0.98),
              legend.justification=c(0.5, 1))
      print(aaa)
    }
  )
}

Below I've inserted images of what this looks like in "small" and "large" browser windows.

下面我在“小”和“大”浏览器窗口中插入了这个样子的图像。

ggplot2和Shiny:如何用图形大小来缩放图例的大小?

ggplot2和Shiny:如何用图形大小来缩放图例的大小?

#1


8  

Here's a way to anchor the top of the legend so that it doesn't run off the top of the plot area. You just add legend.justification(0.5, 1) to the ggplot theme. The first value centers the x position of the legend. The second value "top justifies" the y position of the legend. (You can right-justify the legend by changing 0.5 to 1 for the first value, which will keep the legend from running off the right edge of the graph, if that's a problem.) This doesn't solve the relative sizing issue, but the full legend will always be visible and in the same location.

这是一种锚定图例顶部的方法,使其不会在绘图区域的顶部运行。您只需将legend.justification(0.5,1)添加到ggplot主题即可。第一个值以图例的x位置为中心。第二个值“顶部证明”图例的y位置。 (您可以通过将第一个值更改为0.5来对图例进行右对齐,这将使图例不会在图表的右边缘运行,如果这是一个问题。)这不能解决相对大小问题,但是完整的图例将始终显示在同一位置。

server <- function(input, output, session) {
  output$test <- renderPlot(
    height = function() {
      0.8 * session$clientData$output_test_width
    },
    expr = {
      aaa <- ggplot(mtcars, aes(wt, mpg, color = cyl)) + 
        geom_point() + 
        theme(legend.position = c(0.9, 0.98),
              legend.justification=c(0.5, 1))
      print(aaa)
    }
  )
}

Below I've inserted images of what this looks like in "small" and "large" browser windows.

下面我在“小”和“大”浏览器窗口中插入了这个样子的图像。

ggplot2和Shiny:如何用图形大小来缩放图例的大小?

ggplot2和Shiny:如何用图形大小来缩放图例的大小?