Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly 'Download as png' resizing image when used in RShiny application

Tags:

r

shiny

plotly

I'm using plotly package for R to display some large graphs in my shiny application, the graphs display nicely as intended. However, when I click the "Download as png" button, the downloaded .png has been resized. here is a demo of this behavior.

How can I specify the resolution for the exported plot?

Here is a minimal example that demonstates the issue by having a really long title that gets clipped on download

app.R

library(shiny)
library(ggplot2)
library(plotly)

ui <- fluidPage(titlePanel("Plotly Download demo"),

                plotlyOutput("demoPlotly"))


server <- function(input, output) {
  output$demoPlotly <- renderPlotly({
    #make an arbritrary graph with a long title
    p <- iris %>%
      ggplot(aes(x = Petal.Length, y = Petal.Width, color = Species)) +
      geom_point() +
      labs(title = "This is a really long title to demo my problem of plotly resizing my downloaded output")
    ggplotly(p)

  })
}

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

The downloaded graph looks like this: Clipped plotly graph

like image 382
Michael Bird Avatar asked Sep 15 '17 15:09

Michael Bird


1 Answers

Michael,

Take a look at plotly_IMAGE(). [Version 4.7.1]. The function does allow you to specify width and height for the saved image. For example 800 by 600.

Regarding your example, I have provided an example of how you might modify the layout to accommodate the title wrapping. Note the changes the margin and padding. I also inserted a call to plotly_IMAGE to provide a simplified example of its usage - it slows things down as it is in line with the plot generation. I'd recommend you add a button and separate out from image display.

Hope this helps

Take care T.


Pre-requisite - Plotly Credentials:

To use this plotly example you need to register and obtain a plotly api key. Once obtained you need add these entries to your ~/.Renviron file. Ref: https://plot.ly/r/getting-started/

Example .Renviron file entries

plotly_username="xxxxx"
plotly_api_key="xxxxxx"

Example Code

library(shiny)
library(ggplot2)
library(plotly)

wrapper <- function(x, ...) {
  paste(strwrap(x, ...), collapse = "\n")
}

# Example usage wrapper(my_title, width = 20)

my_title <- "This is a really long title to demo my problem of plotly resizing my downloaded output"

ui <- fluidPage(titlePanel("Plotly Download demo"), plotlyOutput("demoPlotly"))

pal <- c("blue", "red", "green")
pal <- setNames(pal, c("virginica", "setosa", "versicolor"))

layout <- list(title = wrapper(my_title, width = 60),
               xaxis = list(title = "Petal Length"),
               yaxis = list(title = "Petal Width"),
               margin = list(l = 50, r = 50, b = 100, t = 100, pad = 4))

server <- function(input, output) {
  output$demoPlotly <- renderPlotly({
    # make an arbitrary graph with a long title
    p <- iris %>%
      plot_ly(x = ~Sepal.Length,
              y = ~Petal.Width,
              color = ~Species,
              colors = pal,
              mode = "markers",
              type = "scatter") %>%
      layout(autosize = F,
             title = layout$title,
             xaxis = layout$xaxis,
             yaxis = layout$yaxis,
             margin = layout$margin)
    p$elementId <- NULL
    # Example usage of plotly image
    plotly_IMAGE(p, 
                 width = 800, 
                 height = 600, 
                 format = "png", 
                 scale = 1, 
                 out_file = "output.png")
    p
  })
}

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

Screen Shot Image - screen capture

Screen Shot Image


Plotly save - screen click

Plot save as png Image


plotly_IMAGE() save image output 800 by 600

enter image description here

like image 200
Technophobe01 Avatar answered Sep 30 '22 13:09

Technophobe01