我正在努力使我的表格的行名出现在我闪亮的应用程序的 x 轴上,以清楚地列出“第 1 区”、“第 5 区”、“第 7 区”、“第 8 区”的行名, “第 9 区”、“第 11 区”、“第 13 区”、“第 14 区”、“第 17 区”)
我有一个随后的闪亮输出,它将两个条形图组合在一起,使用 x 轴的相同区域布局;但是,x 轴显示为一个连续变量,我似乎无法弄清楚如何制作它,因此 x 轴列出了不同的区域。
这是我现在正在处理的闪亮输出的代码:
#Creating my tables for ShinyOutput
District <- c(1,5,7,8,9,11,13,14,17)
vessel_type_is_general <- c(685,269,476,590,245,228,222,298,171)
tonage_is_missing <- c(65,6,6,13,9,19,12,5,0)
vessel_class_is_incorrect <-c(7,7,9,126,2,11,6,4,7)
route_stability_is_missing<-c(121,89,41,316,32,52,135,47,25)
table1 <- data.frame(District, vessel_type_is_general, tonage_is_missing, vessel_class_is_incorrect, route_stability_is_missing)
District <- c(1,5,7,8,9,11,13,14,17)
vessel_type_is_general <- c(18,4,6,202,0,2,7,0,3)
tonage_is_missing <- c(1,5,1,10,2,0,6,1,0)
vessel_class_is_incorrect <-c(3,4,5,68,0,6,1,2,0)
route_stability_is_missing<-c(4,6,1,114,2,0,1,0,0)
table2 <- data.frame(District, vessel_type_is_general, tonage_is_missing, vessel_class_is_incorrect, route_stability_is_missing)
row.names(table1) <- c('District1','District5','District7','District8','District9','District11','District13','District14','District17')
row.names(table2) <- c('District1','District5','District7','District8','District9','District11','District13','District14','District17')
#loading the Necessary Packages for this Shiny Output
library(shiny)
library(plotly)
ui <- fluidPage(titlePanel( "SPV ERROR CHARTS"),
( sidebarLayout( fluidRow(sidebarPanel(
uiOutput("Data1"),
uiOutput("Data2"),
uiOutput("column1") )),
mainPanel(
plotlyOutput("maingraph1")
)))
)
server <- function(input,output, session){
# selectInput function to select one table from the list of Points Given tables
output$Data1 <- renderUI({
selectInput("dataTables", label = "Select a Table(Points Given)", choices = c("SPV_original", "SPV_current"))
})
# reactive environment to map the selected table name with actual dataframe(i.e, points given)
Data_to_display_Tab1 <- reactive({
if (input$dataTables == "SPV_original") {
df1 <- table1
} else df1 <- table2
return(df1)
})
# Another selectInput function to select a table from the list of Points Used
output$Data2 <- renderUI({
selectInput(inputId = "dataTables2", label = "Select a Table(Points Used)", choices = c("SPV_original","SPV_current"))
})
# reactive environment to map the selected table name with actual dataframe(i.e, points used)
Data_to_display_Tab2 <- reactive({
if (input$dataTables2 == "table1") {
df2 <- table1
} else df2 <- table2
return(df2)
})
# selectInput function to display variable names of selected table from previous selectInput
output$column1 <- renderUI({
selectInput(inputId = "columnNames", label = "Select a Variable", choices = names(Data_to_display_Tab1()[,-c(6)]), selected = "vessel_type_is_general")
})
# Plotly code
output$maingraph1 <- renderPlotly({
plot_ly(Data_to_display_Tab1(), x = ~District, y = Data_to_display_Tab1()[[input$columnNames]], type = 'bar', name = 'Error_Types') %>%
add_trace( x = ~District, y = Data_to_display_Tab2()[[input$columnNames]], name = 'Error_Types') %>%
layout(xaxis = list(title = "District"), yaxis = list(title = input$columnNames), barmode = 'group')
})
}
shinyApp(ui = ui, server = server)
这是我的 x 轴的图像:
这是我希望如何格式化 x 轴的图像: