I am developing my first app using R-shiny. I am using ggplotly to make interactive plots. For some reason, the tooltip shows the numerical value as NA. I am trying to calculate the sum of people who died in civilian aircraft and military aircraft each year.
My UI code is:
aircrash <-read.csv("aircrash_clean_data.csv", header = T)
# Define UI for application that draws a histogram
ui <- fluidPage(
navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
"Aircrash Investigation Analysis", id="nav",
tabPanel("Crashes",
sidebarLayout(
sidebarPanel(
sliderInput("yearInput", "Year", min=1908, max=2020,
value=c(1908, 2020), sep=""),
checkboxGroupInput("aircrafttypeInput", "Select the Aircraft Type:",
choices = c("Civilian",
"Military"),
selected = c("Civilian", "Military"))
),
mainPanel(
plotlyOutput("Aircrafttypecount"),
br(), br(),
plotlyOutput("fatalitiesplot")
)
)
)
)
)
My server code is:
server <- function(input, output) {
d1 <- reactive({
aircrash %>%
filter(crash_opr_type %in% input$aircrafttypeInput,
crash_year >= input$yearInput[1],
crash_year <= input$yearInput[2],)
})
output$Aircrafttypecount <-renderPlotly({
a1<-ggplot(data=d1(), aes(x= crash_year, y=stat(count), color =crash_opr_type ))+
geom_line(stat = "count")+theme_bw()+
theme(plot.title = element_text(color = "black", size = 20, face = "bold", hjust = 0.5),
axis.title.x =element_text(color = "black", size = 14, face = "bold", hjust = 0.5),
axis.title.y = element_text(color = "black", size = 14, face = "bold", hjust = 0.5))+
labs(y = "Number of Aircrashes",x="Year",title = "Number of Crashes per Year",color = "Aircraft Type")+
expand_limits(y=c(0,100)) +
scale_y_continuous(breaks=seq(0, 100, 20))+theme(legend.title = element_blank())
ggplotly(a1, source = "select", tooltip = c("crash_year","count"))
})
output$fatalitiesplot <-renderPlotly({
a2<-ggplot(data=d1(), aes(x= crash_year, y=fatalities, color=crash_opr_type ))+
geom_line(stat="summary", fun=sum) +theme_bw()+
theme(plot.title = element_text(color = "black", size = 20, face = "bold", hjust = 0.5),
axis.title.x =element_text(color = "black", size = 14, face = "bold", hjust = 0.5),
axis.title.y = element_text(color = "black", size = 14, face = "bold", hjust = 0.5))+
labs(y = "Number of Aircrashes",x="Year",title = "Number of Crashes per Year",color = "Aircraft Type")+
theme(legend.title = element_blank())
ggplotly(a2, source = "select", tooltip = c("crash_year","fatalities"))
})
}
Thanks!