首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GGPlotly: downloadHandler给出空地块

GGPlotly: downloadHandler给出空地块
EN

Stack Overflow用户
提问于 2016-04-25 07:31:50
回答 1查看 479关注 0票数 0

我在plotly上遇到了一些困难。我希望能够下载plotly作为pdf。但是,在我的代码中添加了一些x和y轴参数(因为如果我将ggplot传输到plotly,那么x和y轴的标题就会被剪切)

此代码用于下载pdf文件:

代码语言:javascript
复制
library(shiny)
library(DT)
library(ggplot2)
library(plotly)


shinyApp(
  ui = fluidPage(
    fluidRow(downloadButton('downloadplot',label='Download Plot')),
    plotlyOutput('plot1')
  ),
  server = function(input, output) {

    testplot <- function(){

a <- ggplot(mtcars, aes(x = interaction(cyl, carb, lex.order = T), y = mpg,fill = interaction(cyl, carb, lex.order = T))) + 
  geom_boxplot()

    }

    output$plot1 <- renderPlotly({testplot()})

    output$downloadplot <- downloadHandler(
      filename ="plot.pdf",
      content = function(file) {
        pdf(file, width=12, height=6.3)
        print(testplot())
        dev.off()
      })})

添加此代码以修复ggplotly的标题失败:

代码语言:javascript
复制
a <- ggplot(mtcars, aes(x = interaction(cyl, carb, lex.order = T), y = mpg,fill = interaction(cyl, carb, lex.order = T))) + 
  geom_boxplot()

p <- ggplotly(a + ylab(" ") + xlab(" ")) 
x <- list(
  title = "[x]"
)
y <- list(
  title = "[y]"
)
p %>% layout(xaxis = x, yaxis = y)}

给了一个空的地块..。

谢谢你的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-26 08:20:49

我已经解决了我的问题。解决方案不是优雅的,但它是有效的!

所以诀窍是在renderPlotly中设置x和y标题,而不是在testplot()函数中设置。

然而,x和y轴标题必须在testplot()函数中额外输入-因为这将是我们的输出作为pdf,并查看绘图是用plotly

这是代码:

代码语言:javascript
复制
library(shiny)
library(DT)
library(ggplot2)
library(plotly)


shinyApp(
  ui = fluidPage(
    fluidRow(downloadButton('downloadplot',label='Download Plot')),
    plotlyOutput('plot1')
  ),
  server = function(input, output) {

    testplot <- function(){

      a <- ggplot(mtcars, aes(x = interaction(cyl, carb, lex.order = T), y = mpg,fill = interaction(cyl, carb, lex.order = T))) + 
        geom_boxplot()

    }

    output$plot1 <- renderPlotly({

      p <- ggplotly(testplot() + ylab(" ") + xlab(" ")) 
      x <- list(
        title = "[x]"
      )
      y <- list(
        title = "[y]"
      )
      p %>% layout(xaxis = x, yaxis = y)})

    output$downloadplot <- downloadHandler(
      filename ="plot.pdf",
      content = function(file) {
        pdf(file, width=12, height=6.3)
        print(testplot())
        dev.off()
      })})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36834433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档