基于Shiny组件实现带动态分组的雷达图开发需求
动态雷达图Shiny应用
这是一款基于Shiny框架的动态雷达图生成工具,操作流程如下:
- 在Variable多选下拉组件中选择3个及以上数据列,用于指定雷达图的分析维度
- 在Group单选下拉组件中选择1个分组列,雷达图会根据该分组的不同取值生成差异化颜色的图例
- 点击「生成」按钮即可生成对应的雷达图
# 加载所需库 library(shiny) library(ggplot2) library(dplyr) library(fmsb) # 示例数据(替换为你的实际数据) Test_RShiny_Data <- structure(list(abs_edu = c(1, 0, 1, 0, 1, 0, 1, 1, 0, 1), abs_employ = c(1, 1, 0, 0, 1, 0, 1, 1, 1, 1), abs_wash = c(1, 0, 0, 1, 1, 0, 1, 0, 1, 1), abs_health = c(1, 0, 0, 1, 1, 1, 1, 0, 1, 1), abs_protection = c(1, 0, 0, 0, 1, 0, 1, 0, 1, 1), abs_document = c(1, 1, 0, 1, 1, 1, 1, 1, 1, 1), ast_hhasset = c(0, 1, 1, 1, 0, 1, 1, 0, 1, 1), ast_itc = c(1, 0, 1, 1, 1, 0, 1, 1, 1, 1), ast_transport = c(0, 0, 1, 0, 1, 0, 0, 1, 0, 0), ast_nonessential = c(1, 0, 1, 0, 0, 0, 0, 0, 0, 0), ast_employ = c(0, 1, 1, 0, 0, 0, 0, 1, 0, 1), ast_save = c(0, 0, 1, 0, 0, 0, 0, 0, 0, 1), sc_safecom = c(1, 0, 1, 0, 0, 0, 0, 0, 1, 1), sc_group = c(0, 1, 1, 1, 1, 1, 1, 0, 0, 0), sc_safeharass = c(1, 1, 1, 1, 1, 0, 0, 1, 1, 1), sc_safeterror = c(1, 1, 1, 1, 1, 0, 1, 1, 1, 1), sc_safehh = c(1, 1, 1, 1, 1, 0, 1, 1, 1, 1), ac_edu = c(3, 3, 3, 2, 3, 2, 3, 3, 3, 3), ac_numemploy = c(0, 1, 1, 1, 1, 1, 0, 1, 0, 0), ac_supportwork = c(0, 0, 1, 1, 1, 0, 1, 0, 1, 1), ac_tension = c(1, 0, 1, 1, 0, 0, 1, 0, 1, 1), cs = c(1, 1, 1, 0.441957086324692, 1, 0, 0.441957086324692, 0.441957086324692, 1, 1), ge = c(0.526506841182709, -0.51858788728714, 0.526506841182709, -0.51858788728714, -0.51858788728714, 0.243249282240868, 0.243249282240868, -0.51858788728714, -0.51858788728714, 0.526506841182709), gbv = c(0.770168602466583, 0.770168602466583, 0.770168602466583, 0.770168602466583, 0.770168602466583, 0.770168602466583, -0.0019789848010987, -0.0019789848010987, 0.770168602466583, 0.770168602466583 ), country = c("Ukrainian", "Ukrainian", "Moldovan", NA, "Ukrainian", "Ukrainian", "Ukrainian", "Ukrainian", "Ukrainian", "Ukrainian"), marital = c("Seperated/divorced/widowed", "Seperated/divorced/widowed", "Married", "Married", "Single/Engaged", "Married", "Seperated/divorced/widowed", "Seperated/divorced/widowed", "Seperated/divorced/widowed", "Seperated/divorced/widowed"), child = c("1-3 children", "1-3 children", "More than 3 children", "1-3 children", "1-3 children", "1-3 children", "1-3 children", "1-3 children", "1-3 children", "1-3 children"), ageg = c(2, 3, 3, 2, 3, 2, 2, 1, 3, 2)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame")) # 定义UI界面 ui <- fluidPage( titlePanel("动态雷达图"), sidebarLayout( sidebarPanel( selectInput("variable", "选择3个及以上变量", choices = colnames(Test_RShiny_Data),multiple = T), selectInput("Group", "分组依据", choices = colnames(Test_RShiny_Data),multiple = F), actionButton("goButton", "生成") ), mainPanel( plotOutput("radarPlot") ) ) ) # 定义服务器逻辑 server <- function(input, output) { # 响应式数据处理 processed_data <- reactive({ req(input$goButton) # 确保按钮已点击 isolate({ myvars <- input$variable newdata <- Test_RShiny_Data[myvars] }) }) output$radarPlot <- renderPlot({ radarchart(processed_data(), axistype = 1, pfcol = colors_fill, title = "子变量与AC支柱的相关性", pcol = 1:ncol(processed_data()), plwd = 2, plty = 1, cglty = 1, cglwd = 0.5, cglcol = "black", vlcex = 0.8, caxislabels = seq(0, 1, by = 0.2), maxmin = TRUE) legend(x=0.7, y=1, legend = colnames(processed_data()[input$Group,]), bty = "n", pch=20 , text.col = "grey", cex=1.2, pt.cex=3) }) } # 运行应用 shinyApp(ui = ui, server = server)
内容的提问来源于stack exchange,提问作者firmo23
相关产品推荐
相关产品推荐

