ggplotly在闪亮的应用程序中不断崩溃(Rstudio)

时间:2020-10-01 05:23:04

标签: r ggplot2 shiny shinyapps ggplotly

我正在尝试在闪亮的应用程序中渲染ggplotly,但是我的代码甚至无法运行。任何帮助深表感谢!我在哪里弄错了?我对R非常陌生。请参见下面的代码。

**

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

server <- function(input, output) { output$plot2 <- renderPlotly(print(
  ggplotly(
    q<-ggplot(plotdata,aes(x=DateValue, y=Count, col=Country, 
                                 group=1
                                 
                                 ))+
  geom_line(aes(text=paste
                ( '<br>Number of Launches:', Count, 
                  '<br>Country:', Country)
                
                   )) 
    +
  scale_colour_manual(values = c('yellow','orange','turquoise1','mediumspringgreen','tan3','indianred4','plum1','slateblue2','violetred2','greenyellow','blue1','black','firebrick2'))+
  theme( axis.line = element_line(colour = "black", 
                      size = 1, linetype = "solid"))+
scale_x_continuous(breaks=c(1945,1955,1965,1975,1985,1995,2005,2015,2025))+
scale_y_continuous(breaks=c(0,20,40,60,80,100,120))+
theme_bw()+
guides(colour = guide_legend(override.aes = list(shape = 3))) +
labs(title="Space Launches per Year",
subtitle="Colored by country",
caption="Source: ...",
 y="Number of Launches",
x="Year")),
ggplotly(q, tooltip = "text"))}}

**

1 个答案:

答案 0 :(得分:0)

如下所示更改服务器代码,并确保要绘制一个名为plotdata的数据框。最初的print语句是不必要的。另外,您在ggplotly内部调用了ggplotly。

server <- function(input, output) { 
  output$plot2 <- renderPlotly({ 
    q<-ggplot(plotdata,aes(x=DateValue, y=Count, col=Country, group=1))+
      geom_line(aes(text=paste
                    ( '<br>Number of Launches:', Count, 
                      '<br>Country:', Country))) +
    
      scale_colour_manual(values = c('yellow','orange','turquoise1','mediumspringgreen','tan3','indianred4','plum1','slateblue2','violetred2','greenyellow','blue1','black','firebrick2'))+
      theme( axis.line = element_line(colour = "black", 
                                      size = 1, linetype = "solid"))+
      scale_x_continuous(breaks=c(1945,1955,1965,1975,1985,1995,2005,2015,2025))+
      scale_y_continuous(breaks=c(0,20,40,60,80,100,120))+
      theme_bw()+
      guides(colour = guide_legend(override.aes = list(shape = 3))) +
      labs(title="Space Launches per Year",
           subtitle="Colored by country",
           caption="Source: ...",
           y="Number of Launches",
           x="Year")
    
  ggplotly(q, tooltip = "text")
  })
}