简体   繁体   English

ggiraph plot不会调整大小以适应页面

[英]ggiraph plot doesn't resize to fit the page

Here is my code: 这是我的代码:

library(shiny)
library(ggplot2)
library(ggiraph)

df <- data.frame(achseX = LETTERS[1:24], achseY = 1:24, facetX = as.factor(rep(1:4, each = 6)))

server <- function(input, output) {
  output$ggplot <- renderPlot({
    ggplot(data = df) + geom_bar_interactive(aes(tooltip = achseY, x = achseX, y = achseY), stat = "identity") +
      theme_minimal() + facet_grid(.~ facetX, scales = "free_x")
  })


  output$plot <- renderggiraph({
    gg <- ggplot(data = df) + geom_bar_interactive(aes(tooltip = achseY, x = achseX, y = achseY), stat = "identity") +
      theme_minimal() + facet_grid(.~ facetX, scales = "free_x")
    return(ggiraph(code = print(gg), selection_type = "multiple", zoom_max = 4,
                   hover_css = "fill:#FF3333;stroke:black;cursor:pointer;",
                   selected_css = "fill:#FF3333;stroke:black;"))
  })
}

ui <- fluidPage(
  "GGPLOT2:",
  plotOutput("ggplot"),
  "GGIRAPH:",
  ggiraphOutput("plot", width = "500px", height = "1000px")
)

shinyApp(ui = ui, server = server)

The result looks like: 结果如下: 在此输入图像描述

As you can see in the code, the first barplot is a ggplot that works the way it should. 正如您在代码中看到的那样,第一个条形图是一个ggplot的方式工作的ggplot It is responsive to the site and has a rectangular format. 它响应网站并具有矩形格式。 The ggiraph stays in a square format instead and doesn't fit the page. ggiraph保持方形格式而不适合页面。

How can I get the ggiraph to look like the ggplot? 我怎样才能让ggiraph看起来像ggplot?

I tried several combinations of the width and height argument, also including width = "auto" and height = "auto" . 我尝试了width和height参数的几种组合,还包括width = "auto"height = "auto" This made the ggiraph fit the page, but still in a square format. 这使得ggiraph适合页面,但仍然是方格式。

You can make the ui responsive with some amount of js code in it. 你可以让ui响应一些js代码。 Something along the lines of this answer. 这个答案的一些方面。

The difference is that the ggiraph function wants the input in inches so we will need to convert pixels into inches. 区别在于ggiraph函数需要以英寸为单位的输入,因此我们需要将像素转换为英寸。 The formula for that is inches = pixels/dpi . 其公式为inches = pixels/dpi So, the js code in the ui passes the window height and with along with the dpi of the screen from which we can calculate the length in inches which can be then passed to the ggiraph function and hence make the plot responsive to the ui. 因此,ui中的js代码通过窗口高度并与屏幕的dpi一起从中我们可以计算出以英寸为单位的长度,然后可以将其传递给ggiraph函数,从而使绘图响应于ui。

I have modified your example to do exactly that. 我修改了你的例子来做到这一点。 Hope it helps! 希望能帮助到你!

 library(shiny)
 library(ggplot2)
 library(ggiraph)

 df <- data.frame(achseX = LETTERS[1:24], achseY = 1:24, facetX = as.factor(rep(1:4, each = 6)))

 server <- function(input, output, session) {
   output$ggplot <- renderPlot({
     ggplot(data = df) + geom_bar_interactive(aes(tooltip = achseY, x = achseX, y = achseY), stat = "identity") +
       theme_minimal() + facet_grid(.~ facetX, scales = "free_x")
   })


   output$plot <- renderggiraph({
     gg <- ggplot(data = df) + geom_bar_interactive(aes(tooltip = achseY, x = achseX, y = achseY), stat = "identity") +
       theme_minimal() + facet_grid(.~ facetX, scales = "free_x")
     return(ggiraph(code = print(gg), selection_type = "multiple", zoom_max = 4,
                    hover_css = "fill:#FF3333;stroke:black;cursor:pointer;",
                    selected_css = "fill:#FF3333;stroke:black;", 
                    width_svg = (0.8*input$pltChange$width/input$pltChange$dpi),
                    height_svg = (0.5*input$pltChange$height/input$pltChange$dpi)
                    ))
   })
 }

 ui <- fluidPage(

   tags$body(tags$div(id="ppitest", style="width:1in;visible:hidden;padding:0px")),

   tags$script('$(document).on("shiny:connected", function(e) {
                                    var w = window.innerWidth;
                                    var h = window.innerHeight;
                                    var d =  document.getElementById("ppitest").offsetWidth;
                                    var obj = {width: w, height: h, dpi: d};
                                    Shiny.onInputChange("pltChange", obj);
                                });
                                $(window).resize(function(e) {
                                    var w = $(this).width();
                                    var h = $(this).height();
                                    var d =  document.getElementById("ppitest").offsetWidth;
                                    var obj = {width: w, height: h, dpi: d};
                                    Shiny.onInputChange("pltChange", obj);
                                });
                            '),


   "GGPLOT2:",
   plotOutput("ggplot"),
   "GGIRAPH:",
   ggiraphOutput("plot")
 )

 shinyApp(ui = ui, server = server) 

There is an official statement to this now. 现在有一个官方声明。

See: https://github.com/davidgohel/ggiraph/issues/71 请参阅: https//github.com/davidgohel/ggiraph/issues/71

It is not possible to fix this issue! 无法解决此问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM