[英]ggiraph plot doesn't resize to fit the page
這是我的代碼:
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)
正如您在代碼中看到的那樣,第一個條形圖是一個ggplot
的方式工作的ggplot
。 它響應網站並具有矩形格式。 ggiraph
保持方形格式而不適合頁面。
我怎樣才能讓ggiraph看起來像ggplot?
我嘗試了width和height參數的幾種組合,還包括width = "auto"
和height = "auto"
。 這使得ggiraph適合頁面,但仍然是方格式。
你可以讓ui響應一些js代碼。 這個答案的一些方面。
區別在於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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.