Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

r - Creating plots in a shiny app with Baseball data sorted by a third variable (Pitcher Name)

I'm new to R and developing a shiny app to analyze baseball data. I want to be able to sort by the players name.

I started by creating a drop down menu to select the desired player name:

ui <- fluidPage(
selectInput(inputId = "num1",
    label = "Pitcher Name",
    choices = levels(PitcherName),
    selected = NULL
),
plotOutput("plot"))

and I want to make the following ggplot show the data depending on which name is selected from the list:

server <- function(input, output) {
output$plot <-renderPlot({
    input$num1
        bullpen %>%
            ggplot(aes(x=PlateLocSide, y=PlateLocHeight)) +
            geom_point(data = bullpen, aes(color = TaggedPitchType)) +
            scale_color_manual(values = c('green','blue','red','purple','yellow')) +
            geom_path(data = sz, aes(x=x, y=z)) +
            xlim(-3,3) +
            ylim(0,6) +
            ggtitle("Pitch Location by Pitch Type")
})
question from:https://stackoverflow.com/questions/65876916/creating-plots-in-a-shiny-app-with-baseball-data-sorted-by-a-third-variable-pit

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can create a dataframe that's filtered based on your input and then feed it to the ggplot.


output$plot <-renderPlot({
 bp <- bullpen %>% filter(PitcherName %in% num1)

 ggplot(bp, aes(x=PlateLocSide, y=PlateLocHeight)) +
            geom_point(data = bp, aes(color = TaggedPitchType)) +
            scale_color_manual(values = c('green','blue','red','purple','yellow')) +
            geom_path(data = sz, aes(x=x, y=z)) +
            xlim(-3,3) +
            ylim(0,6) +
            ggtitle("Pitch Location by Pitch Type")
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...