我正试图在一个闪亮的应用程序中渲染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"))}}
**
如下图所示更改服务器代码,并确保要绘制名为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")
})
}