R Shiny绘制雷达图报错:数据需为data.frame问题求助
问题分析与解决
核心问题
- 数据类型不匹配:你通过
industry$Centralgov提取的是单列向量,而radarchart()要求输入包含最大值(Max)、最小值(Min)的完整数据框,且格式要符合fmsb包的要求。 - 变量名笔误:
"Other"分支里写了counties$Other,但你的数据框变量是industry,属于拼写错误。
修正后的服务器逻辑
把server部分替换成以下代码,核心是构建符合雷达图要求的数据结构:
server <- function(input, output) { output$radarPlot <- renderPlot({ # 映射用户选择到数据框的列名 col_name <- switch(input$var, "Central government" = "Centralgov", "Local government" = "Localgov", "IT and Telecoms" = "IT", "Professional services, law and accountancy" = "PS", "Finance, banking and insurance" = "Finance", "Health" = "Health", "Social care/housing association" = "Social", "Other charity/voluntary sector" = "Charity", "Retail" = "Retail", "Engineering" = "Engineering", "Manufacturing" = "Manufacturing", "Pharmaceutical" = "Pharmaceutical", "Transport" = "Transport", "Utilities" = "Utilities", "Hospitality" = "Hospitality", "Education (HE, FE)" = "Education", "Consulting" = "Consulting", "Art, media and design" = "Art", "Other" = "Other") # 提取Max、Min和选中行业的数据,转置后符合radarchart要求的格式 data <- industry[, c("Max", "Min", col_name)] data_t <- t(data) rownames(data_t) <- c("Max", "Min", input$var) # 绘制雷达图,添加样式优化可读性 radarchart(data_t, title = paste("L&D Capabilities -", input$var), vlabels = rownames(data), cglcol = "gray", cglty = 1, cglwd = 0.8, pcol = "#2E86AB", plwd = 2, plty = 1) }) }
为什么这样改?
- fmsb的雷达图要求数据结构为:每行是一个观测(Max、Min、目标行业),每列是一个指标(L&D能力),你的原始数据是指标在行、观测在列,所以需要用
t()转置。 - 必须包含Max和Min行,雷达图才能确定每个指标的刻度范围,否则无法生成有效图表。
- 修正了
counties$Other的笔误,确保数据提取正确。
完整可运行代码
# Load packages ---- library(shiny) library(fmsb) # Load data ---- industry <- structure(list(Max = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Min = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Centralgov = c(0.6, 0.18, 0.27, 0.27, 0.27, 0.27, 0.36, 0.3, 0.55, 0.45, 0.1, 0, 0.1, 0.27, 0.64, 0.09, 0.09, 0.18, 0.27, 0, 0.09, 0.18, 0.25, 0.29, 0.14), Localgov = c(0.36, 0.5, 0.36, 0.5, 0.42, 0.42, 0.09, 0.27, 0.36, 0.55, 0.3, 0.36, 0.55, 0.45, 0.73, 0.36, 0.18, 0.45, 0.64, 0.36, 0.27, 0.18, 0.3, 0.2, 0.6), IT = c(0.73, 0.33, 0.47, 0.51, 0.38, 0.18, 0.34, 0.38, 0.62, 0.41, 0.19, 0.38, 0.49, 0.41, 0.62, 0.32, 0.22, 0.38, 0.58, 0.51, 0.33, 0.34, 0.41, 0.15, 0.37 ), PS = c(0.73, 0.4, 0.56, 0.6, 0.48, 0.48, 0.29, 0.24, 0.63, 0.56, 0.29, 0.41, 0.27, 0.36, 0.71, 0.28, 0.16, 0.48, 0.4, 0.52, 0.36, 0.38, 0.29, 0.25, 0.13), Finance = c(0.9, 0.44, 0.66, 0.66, 0.61, 0.52, 0.44, 0.5, 0.86, 0.62, 0.32, 0.39, 0.48, 0.59, 0.86, 0.3, 0.27, 0.5, 0.52, 0.52, 0.57, 0.51, 0.56, 0.33, 0.29), Health = c(0.88, 0.33, 0.47, 0.65, 0.28, 0.37, 0.33, 0.29, 0.78, 0.47, 0.18, 0.13, 0.47, 0.5, 0.78, 0.26, 0.16, 0.41, 0.58, 0.5, 0.38, 0.39, 0.33, 0.13, 0.29), Social = c(0.7, 0.25, 0.5, 0.33, 0.3, 0.2, 0.1, 0.4, 0.5, 0.2, 0, 0.22, 0, 0.2, 0.4, 0.1, 0.3, 0.1, 0.3, 0.3, 0.33, 0.3, 0.33, 0, 0.11), Charity = c(0.8, 0.55, 0.62, 0.44, 0.5, 0.31, 0.08, 0.33, 0.58, 0.5, 0.4, 0.36, 0.33, 0.38, 0.82, 0.15, 0.08, 0.36, 0.22, 0.42, 0.2, 0.42, 0.18, 0.22, 0.11), Retail = c(0.62, 0.38, 0.46, 0.27, 0.25, 0.09, 0.08, 0.31, 0.82, 0.46, 0.25, 0.27, 0.25, 0.54, 0.69, 0.08, 0.17, 0.31, 0.67, 0.5, 0.33, 0.5, 0.38, 0.18, 0.08), Engineering = c(0.6, 0, 0.4, 0.25, 0.17, 0.17, 0, 0, 0.33, 0.5, 0.25, 0.33, 0.6, 0.17, 0.33, 0, 0, 0.33, 0.33, 0.17, 0.17, 0.5, 0.2, 0, 0), Manufacturing = c(0.56, 0.22, 0.35, 0.42, 0.42, 0.4, 0.24, 0.2, 0.56, 0.41, 0.24, 0.11, 0.21, 0.3, 0.63, 0.1, 0, 0.25, 0.42, 0.58, 0.21, 0.35, 0.25, 0.33, 0.06), Pharmaceutical = c(0.43, 0.25, 0, 0.71, 0.63, 0.25, 0.13, 0.13, 0.63, 0.43, 0, 0, 0, 0, 0.38, 0.25, 0.13, 0.38, 0.38, 0.5, 0, 0, 0.33, 0, 0.17), Transport = c(0.77, 0.62, 0.79, 0.57, 0.71, 0.64, 0.14, 0.5, 0.79, 0.46, 0.38, 0.21, 0.36, 0.38, 0.64, 0.43, 0.29, 0.21, 0.57, 0.64, 0.29, 0.54, 0.57, 0.36, 0.15), Utilities = c(1, 0.6, 0.4, 0.33, 0.2, 0.2, 0.6, 0.6, 0.8, 0.6, 0.25, 0.2, 0.8, 0.4, 1, 0.4, 0.4, 0.6, 0.4, 0.6, 0.2, 0.2, 0.2, 0.2, 0), Hospitality = c(0.67, 0, 0.67, 0.4, 0.67, 0.33, 0.33, 0.83, 0.83, 0.2, 0.67, 0.17, 0.2, 0.33, 0.83, 0.33, 0.33, 0, 0.67, 1, 0.5, 0.33, 0.33, 0.6, 0.33), Education = c(0.87, 0.33, 0.47, 0.53, 0.41, 0.38, 0.5, 0.47, 0.65, 0.41, 0.2, 0.31, 0.47, 0.65, 0.53, 0.24, 0.29, 0.38, 0.56, 0.41, 0.31, 0.19, 0.38, 0.27, 0.35), Consulting = c(0.67, 0.5, 0.67, 1, 0.33, 0.33, 0.17, 0.5, 1, 0.6, 0.33, 0.6, 0.4, 0.67, 0.5, 0.17, 0.17, 0.4, 0.6, 0.5, 0.5, 0.33, 0.4, 0.25, 0.25), Art = c(1, 0.2, 0.6, 0.5, 0.4, 0.2, 0.2, 0.6, 0.6, 0.4, 0.2, 0.2, 0.6, 0.5, 0.5, 0.2, 0.2, 0.2, 0.2, 0.6, 0.4, 0.4, 0.4, 0.2, 0.25), Other = c(0.67, 0.57, 0.71, 0.29, 0.57, 0.43, 0.14, 0.5, 0.67, 0.29, 0.57, 0.29, 0.43, 0.57, 0.71, 0.29, 0.43, 0.29, 0.43, 0.57, 0.71, 0.43, 0.5, 0.6, 0.4)), class = "data.frame", row.names = c("In-person classroom delivery", "Strategy and governance", "Stakeholder engagement", "Instructional design", "Crafting learning journeys / blended solutions", "Supporting ongoing workplace performance", "Facilitating social and collaborative learning", "Understanding learner behaviour", "Virtual classroom / webinar delivery", "Digital content development", "Performance consulting", "Business acumen", "Marketing and communications", "Coaching and mentoring", "Learning management / administration", "Analytics / data management", "Evaluating impact", "Technology/infrastructure", "Project management", "Leveraging L&D expertise", "Knowledge management", "Negotiation, persuasion, and influence", "Learning experience design", "Community engagement", "Research capabilities")) # User interface ---- ui <- fluidPage( titlePanel("L&D Capabilities 2023"), sidebarLayout( sidebarPanel( helpText("Check which L&D capabilities your industry has in-house in 2023."), selectInput("var", label = "Choose a variable to display", choices = c("Central government", "Local government", "IT and Telecoms", "Professional services, law and accountancy", "Finance, banking and insurance", "Health", "Social care/housing association", "Other charity/voluntary sector", "Retail", "Engineering", "Manufacturing", "Pharmaceutical", "Transport", "Utilities", "Hospitality", "Education (HE, FE)", "Art, media and design", "Other", "Consulting"), selected = "Central government"), ), mainPanel(plotOutput("radarPlot")) ) ) # Server logic ---- server <- function(input, output) { output$radarPlot <- renderPlot({ col_name <- switch(input$var, "Central government" = "Centralgov", "Local government" = "Localgov", "IT and Telecoms" = "IT", "Professional services, law and account
相关产品推荐
相关产品推荐

