2017-07-19 73 views
3

这里是我的代码:ggiraph情节不调整大小以适应页面

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) 

结果是这样的: enter image description here

正如你可以在代码中看到,第一barplot是ggplot按照它应该的方式工作。它对网站有响应并具有矩形格式。 ggiraph保持方形格式,而不适合页面。

我怎样才能让ggiraph看起来像ggplot?

我尝试了几种宽度和高度参数的组合,还包括width = "auto"height = "auto"。这使ggiraph适合页面,但仍然以方形格式。

回答

1

你可以让ui响应一些数量的js代码。沿着this的答案。

区别在于ggiraph函数需要以英寸为单位的输入,因此我们需要将像素转换为英寸。公式为inches = pixels/dpi。因此,ui中的js代码通过窗口高度,并与屏幕的dpi一起显示,然后我们可以计算出英寸的长度,然后传递给ggiraph函数,从而使曲线对UI响应。

我修改了你的例子来做到这一点。希望能帮助到你!

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) 
相关问题