[英]ggplotly in shiny app keeps crashing (Rstudio)
I am trying to render a ggplotly in a shiny app, but my code doesn't even run.我试图在一个闪亮的应用程序中渲染一个 ggplotly,但我的代码甚至没有运行。 Any help is highly appreciated!
任何帮助都非常感谢! Where am I making a mistake?
我在哪里犯了错误? I am very new to R. See my code below.
我对 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"))}}
** **
Change your server code as shown below and ensure that you have a dataframe named plotdata
to plot.如下所示更改您的服务器代码,并确保您有一个名为
plotdata
的数据plotdata
进行绘图。 Initial print
statement was unnecessary.不需要初始
print
语句。 Also, you were calling ggplotly inside ggplotly.另外,您在 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")
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.