将ggplot对象转换为有光泽的应用程序 [英] Convert ggplot object to plotly in shiny application

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

问题描述

我试图转换一个ggplot对象来绘制并在一个闪亮的应用程序中显示它。但我遇到了一个错误:没有适用于'plotly_build'的方法应用于类NULL的对象。



我能够将ggplot对象返回给闪亮的应用程序

  output $ plot1 < -  renderplot (x = disp,y = cyl))+ geom_smooth(method = lm,formula = y〜x)+ geom_point()+ theme_gdocs()
})
pre>

但以某种方式情节不能转换它。



我的代码看起来像这样

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


解决方案

试试:

 库(闪亮)
库(ggplot2)
库(ggthemes)
图书馆(情节)

ui< - fluidPage(
titlePanel(Plotly),
sidebarLayout(
sidebarPanel(),
mainPanel
plotlyOutput(plot2))))

服务器< - 函数(输入,输出){

输出$ plot2< - renderPlotly({ b $ b print(
ggplotly(
ggplot(data = mtcars,aes(x = disp,y = cyl))+ geom_smooth(method =
lm,formula = y〜x)+ geom_point()+ theme_gdocs()))

))
}

shinyApp(ui,server)


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({
print(
  ggplotly(
    ggplot(data = mtcars, aes(x = disp, y = cyl)) + geom_smooth(method = 
                      lm, formula = y~x) + geom_point() + theme_gdocs()))

})
}

shinyApp(ui, server)

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

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