IER
IER

Reputation: 1

Radio button with radar plot shiny

I'm trying to make a shiny dashboard with attributes of superheros graphed in a radar plot. I'd also like to be able to use radio buttons to select which of the superheros I'd like to see in the graph. However, when I run this code, I get an error that says: Error in polygon: invalid value specified for graphical parameter "lwd". There is no lwd command that I'm aware of for radar charts so I'm not sure how to correct this. Does anyone have a suggestion on how to handle this error?

library(fmsb)

data<-data.frame(Strength = c(7, 0, 6, 7, 4, 3), 
                 Speed = c(7, 0 , 5, 7, 3, 2),
                 Intelligence = c(7, 0, 6, 2, 4, 3),
                 Fighting_Skills = c(7, 0, 4, 4, 4, 6),
                 Energy = c(7, 0, 6, 6, 1, 1),
                 Durability = c(7, 0, 6, 6, 3, 3),
                 row.names = c("max", "min", "Iron Man", "Thor", "Spiderman", "Captain America"))
head(data)

colors_fill<-c(scales::alpha("gray", 0.1))
               #scales::alpha("gold", 0.1),
               #scales::alpha("tomato", 0.2),
               #scales::alpha("skyblue", 0.2))


colors_line<-c(scales::alpha("darkgray", 0.9))
               #scales::alpha("gold", 0.9),
               #scales::alpha("tomato", 0.9),
               #scales::alpha("royalblue", 0.9))


#radarchart(data,
           #seg =7,
           #title = "Radar Chart",
           #pcol = colors_line,
           #pfcol = colors_fill, 
           #plwd = 1)

#legend(x = 0.6,
       #y=1.35,
       #legend = rownames(data[-c(1,2),]),
      # bty = "n", pch = 20, col = colors_line, cex = 1.2, pt.cex = 3)


# Define UI for application
ui <- fluidPage(

    # Application title
    titlePanel("Radar chart"),

    # Sidebar with a radio buttons for person
    sidebarLayout(
        sidebarPanel(
            radioButtons("variablechoice", "People Choice", 
                         choices = c("Iron Man", "Thor", "Spiderman", "Captain America"),
                         selected = "Thor")
        ),
        

        # Show a plot
        mainPanel(
           plotOutput("radar")
        )
    )
)

# Define server logic required to draw a radar plot
server <- function(input, output) {

    output$radar <- renderPlot({
      if( input$variablechoice=="Iron Man"){new<-data[c(3),] }
      if( input$variablechoice=="Thor"){new<-data[c(4),] }
      if( input$variablechoice=="Spiderman"){new<-data[c(5),] }
      if( input$variablechoice=="Captain America"){new<-data[c(6),] }
      
      radarchart(new,
                 seg = 7,
                 #title = "Radar Chart",
                 pcol = colors_line,
                 pfcol = colors_fill, 
                 plwd = 0.5)
      
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

Upvotes: 0

Views: 173

Answers (1)

stefan
stefan

Reputation: 124213

The issue is that you have missed to include the first two rows of your data which contain the min and max values for the categories in your new dataframe. That's why radarchart throws an error:

library(fmsb)
library(shiny)

data <- data.frame(
  Strength = c(7, 0, 6, 7, 4, 3),
  Speed = c(7, 0, 5, 7, 3, 2),
  Intelligence = c(7, 0, 6, 2, 4, 3),
  Fighting_Skills = c(7, 0, 4, 4, 4, 6),
  Energy = c(7, 0, 6, 6, 1, 1),
  Durability = c(7, 0, 6, 6, 3, 3),
  row.names = c("max", "min", "Iron Man", "Thor", "Spiderman", "Captain America")
)

colors_fill <- c(scales::alpha("gray", 0.1))
colors_line <- c(scales::alpha("darkgray", 0.9))

# Define UI for application
ui <- fluidPage(
  # Application title
  titlePanel("Radar chart"),

  # Sidebar with a radio buttons for person
  sidebarLayout(
    sidebarPanel(
      radioButtons("variablechoice", "People Choice",
        choices = c("Iron Man", "Thor", "Spiderman", "Captain America"),
        selected = "Thor"
      )
    ),
    # Show a plot
    mainPanel(
      plotOutput("radar")
    )
  )
)

# Define server logic required to draw a radar plot
server <- function(input, output) {
  output$radar <- renderPlot({
    if (input$variablechoice == "Iron Man") {
      new <- data[c(1:2, 3), ]
    }
    if (input$variablechoice == "Thor") {
      new <- data[c(1:2, 4), ]
    }
    if (input$variablechoice == "Spiderman") {
      new <- data[c(1:2, 5), ]
    }
    if (input$variablechoice == "Captain America") {
      new <- data[c(1:2, 6), ]
    }

    radarchart(new,
      seg = 7,
      # title = "Radar Chart",
      pcol = colors_line,
      pfcol = colors_fill,
      plwd = 0.5
    )
  })
}

# Run the application
shinyApp(ui = ui, server = server)

enter image description here

Upvotes: 1

Related Questions