闪亮-``ggsave''打印的图看起来与闪亮的应用程序中显示的图不同

时间:2018-09-08 16:59:39

标签: r shiny

我制作了一个很棒的Shiny应用程序,它允许用户调整其图的大小并将其在画布上移动。

问题是,有时下载的图与视图面板中显示的图有很大不同-它显示为方形图形,如果您更改比例和位置,则该图可能会被切掉。

如果以0.5比例绘制,则在浏览器和下载的png中外观相同。 -但是,如果将其缩放为0.8,甚至只是缩放为1.0,它在视图面板中看起来将是完美的,但是在使用ggsave()打印时却很难看并且没有对齐。

也许这是关于将参数调整为ggsave的问题?还是我的背景图像尺寸异常(1920 x 1028像素)?还是反应性专家小组以某种方式将其抛弃了……?在这一点上,我感到困惑。

无论如何,我在绘图区后面绘制了灰色背景,以便您可以轻松地从面板上分辨画布。

这是一个最小的可复制示例。

library(shiny)
library(tidyverse)
library(magick)
library(cowplot)


picture <- 'https://content.halocdn.com/media/Default/games/halo-5-guardians/map-images/arena/arena_maps_sustain_array04-bddc574f8e3445d08f24ef859fb96941.jpg'

background <- image_read(picture)



ui <- fluidPage(

   # Application title
   titlePanel("Old Faithful Geyser Data"),


   sidebarLayout(
      sidebarPanel(
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30),


      sliderInput("xposition",
                  "X-Axis Position",
                  min = -1,
                  max = 1,
                  value = 0,
                  step = 0.1),

      sliderInput("yposition",
                  "Y-Axis Position",
                  min = -1,
                  max = 1,
                  value = 0,
                  step = 0.1),

      sliderInput("scale",
                  "Chart Scale",
                  min = .25,
                  max = 1,
                  value = .5,
                  step = 0.1)),


      # Show a plot over grey background to see how canvas shifts
      mainPanel(style = "background:grey",

         plotOutput("distPlot"),
         downloadButton("downloadPlot", "Download")
      )
   )
)


server <- function(input, output) {

  xposition <- reactive({input$xposition})
  yposition <- reactive({input$yposition})
  scale <- reactive({input$scale})


  plot <- reactive({

    base_plot <- ggplot(faithful, aes(x = waiting), bins = input$bin) +
    geom_histogram()

    ggdraw() +
      draw_image(background) +
      draw_plot(base_plot, x = xposition(), y = yposition(), scale = scale())
    })

  ## Use Cowplot package to alter scale and position


  output$distPlot <- renderPlot({

      # draw the histogram with the specified number of bins
    plot()

   })

  output$downloadPlot <- downloadHandler(

    filename = "my_awesome_plot.png",
    content = function(file) {
      ggsave(file, plot())       

    }


  )
}

# Run the application 
shinyApp(ui = ui, server = server)

如果尝试0.5比例,0.8比例和1.0比例,您会明白我的意思!

有帮助吗??

注意:上面的可重现示例应该足以找到问题,但是如果您想查看更多“深度”示例,则这是我的真实应用,其中包含更多图像以及更多反应式缩放和定位。

https://jjohn987.shinyapps.io/Halo5_Stats/

输入玩家的标签名称(例如,这些作品=> hi,k,hairball9000或其他用户使用的游戏标签,但不包含逗号或空格),您会看到看起来像是“底部”该图与下载底部的图有很大不同...请记住,我没有从Microsoft申请任何重要的api带宽,因此,根据有多少人尝试提供帮助,调用可能会超时!

0 个答案:

没有答案
相关问题