在闪亮的应用程序中将 ggplot 对象转换为 plotly [英] Convert ggplot object to plotly in shiny application

查看:11
本文介绍了在闪亮的应用程序中将 ggplot 对象转换为 plotly的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am trying to convert a ggplot object to plotly and show it in a shiny application. But I encountered an error "no applicable method for 'plotly_build' applied to an object of class "NULL""

I was able to return the ggplot object to the shiny application successfully,

output$plot1 <- renderplot({
   gp <- ggplot(data = mtcars, aes(x = disp, y = cyl)) + geom_smooth(method = lm, formula = y~x) + geom_point() + theme_gdocs()
})

but somehow plotly cannot convert it.

My code looks like this

output$plot2 <- renderplotly({
   gp <- ggplot(data = mtcars, aes(x = disp, y = cyl)) + geom_smooth(method = lm, formula = y~x) + geom_point() + theme_gdocs()
   ggplotly()
})

解决方案

Try:

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

ui <- fluidPage(  
titlePanel("Plotly"),
sidebarLayout(
sidebarPanel(),
mainPanel(
  plotlyOutput("plot2"))))
  
server <- function(input, output) {

output$plot2 <- renderPlotly({
  ggplotly(
    ggplot(data = mtcars, aes(x = disp, y = cyl)) + 
      geom_smooth(method = lm, formula = y~x) + 
      geom_point() + 
      theme_gdocs())
})
}

shinyApp(ui, server)

这篇关于在闪亮的应用程序中将 ggplot 对象转换为 plotly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆