ggiraph 图不会调整大小以适合页面
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
,它按应有的方式工作。它响应站点并具有矩形格式。 ggiraph
保留为正方形格式,不适合页面。
How can I get the ggiraph to look like the ggplot?
我尝试了几种宽度和高度参数的组合,还包括 width = "auto"
和 height = "auto"
。这使得 ggiraph 适合页面,但仍然是方形格式。
您可以使用一些 js 代码使 ui 响应。与 答案类似的内容。
区别在于 ggiraph
函数需要以英寸为单位输入,因此我们需要将像素转换为英寸。其公式为 inches = pixels/dpi
。因此,ui 中的 js 代码传递 window 高度以及屏幕的 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)
现在有官方声明。
参见:https://github.com/davidgohel/ggiraph/issues/71
It is not possible to fix this issue!
这是我的代码:
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
,它按应有的方式工作。它响应站点并具有矩形格式。 ggiraph
保留为正方形格式,不适合页面。
How can I get the ggiraph to look like the ggplot?
我尝试了几种宽度和高度参数的组合,还包括 width = "auto"
和 height = "auto"
。这使得 ggiraph 适合页面,但仍然是方形格式。
您可以使用一些 js 代码使 ui 响应。与
区别在于 ggiraph
函数需要以英寸为单位输入,因此我们需要将像素转换为英寸。其公式为 inches = pixels/dpi
。因此,ui 中的 js 代码传递 window 高度以及屏幕的 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)
现在有官方声明。
参见:https://github.com/davidgohel/ggiraph/issues/71
It is not possible to fix this issue!