ggplotly 工具提示显示 NA 值
ggplotly tooltip showing NA value
我正在使用 R-shiny 开发我的第一个应用程序。我正在使用 ggplotly 制作交互式图表。出于某种原因,工具提示将数值显示为 NA。我正在计算每年死于民用飞机和军用飞机的人数总和
我的UI代码是:
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")
)
)
)
)
)
我的服务器代码是:
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"))
})
}
谢谢!
问题是您的数据中每年都有多个 fatalities
值,因为每年都会发生多次崩溃。 tooltip
每个点只接受单个值,所以它只是 returns NA
因为它不知道如何处理多个值。
最简单的解决方法是在 d1
:
之后创建一个单独的反应式
d2 <- reactive({
d1() %>%
group_by(crash_opr_type, crash_year) %>%
summarise(fatalities = sum(fatalities,na.rm = TRUE))
})
然后对于情节 a2
,使用 d2()
而不是 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"))
我还删除了 geom_line()
的 stat="summary", fun=sum
参数,因为它们不再是必需的。
我正在使用 R-shiny 开发我的第一个应用程序。我正在使用 ggplotly 制作交互式图表。出于某种原因,工具提示将数值显示为 NA。我正在计算每年死于民用飞机和军用飞机的人数总和
我的UI代码是:
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")
)
)
)
)
)
我的服务器代码是:
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"))
})
}
谢谢!
问题是您的数据中每年都有多个 fatalities
值,因为每年都会发生多次崩溃。 tooltip
每个点只接受单个值,所以它只是 returns NA
因为它不知道如何处理多个值。
最简单的解决方法是在 d1
:
d2 <- reactive({
d1() %>%
group_by(crash_opr_type, crash_year) %>%
summarise(fatalities = sum(fatalities,na.rm = TRUE))
})
然后对于情节 a2
,使用 d2()
而不是 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"))
我还删除了 geom_line()
的 stat="summary", fun=sum
参数,因为它们不再是必需的。