0

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"))
        
    })
    
}

Tooltip showing NA

Thanks!

Deepansh Arora
  • 724
  • 1
  • 3
  • 15

1 Answers1

1

The issue is that for each year in your data there are multiple values of fatalities, because multiple crashes per year. tooltip only accepts single values per point, so it just returns NA because it doesn't know how to process multiple values.

Simplest fix is to create a separate reactive after d1:

d2 <- reactive({
  d1() %>%
    group_by(crash_opr_type, crash_year) %>%
    summarise(fatalities = sum(fatalities,na.rm = TRUE))
}) 

Then for the plot a2, use d2() instead of d1():

a2<-ggplot(data=d2(), aes(x= crash_year, y=fatalities, color=crash_opr_type))+
  geom_line() +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"))

I also removed the stat="summary", fun=sum arguments of geom_line() since they are no longer necessary.

Dan Kennedy
  • 380
  • 1
  • 9