绘图图未显示在Shiny中,以及如何为每个绘图显示“名称”?

时间:2018-11-05 09:15:43

标签: r shiny plotly

我是R编码的新手,我只是想为我为幻想超级联赛制作的代码提供帮助。我已经使用了renderPlotly和plot_ly函数,但是当我运行Shiny应用程序时,它没有出现。另外,我想知道如何将数据集的名称合并到显示的绘图中。

任何帮助将不胜感激。谢谢

library(shiny)
library(plotly)
library(ggplot2)
library(ggrepel)
library(dplyr)
BPSvsPPG <- read.csv("file:///C:/Users/haoso/Documents/FPL_Database.csv", stringsAsFactors = FALSE)

# Dump Data
d <- dput(BPSvsPPG, file = "BPSvsPPG")

# Filter the Data
BPSvsPPG2 <- subset(BPSvsPPG, AveragePoints > 4) 
n_total <- max(BPSvsPPG2$RoundNo)
names <- unique(BPSvsPPG2$Full.Name)
positions <- unique(BPSvsPPG2$PositionsList)

# Create UI 
ui <- fluidPage(
  sidebarLayout(
    # Inputs
    sidebarPanel(
      # y-axis
      selectInput(inputId = "y",
                  label = "AverageBPS", 
                  choices = "AverageBPS",
                  selected = "AverageBPS"), 
      # x-axis
      selectInput(inputId = "x",
                  label = "AveragePoints",
                  choices = "AveragePoints",
                  selected = "AveragePoints"),

      # positions
      selectInput(inputId = "Pos",
                  label = "Positions",
                  choices = positions),

      # Set slider
      numericInput(inputId = "RoundNo", 
                   label = "RoundNo", 
                   min = 1, max = n_total, 
                   value = 4)
    ), 
    # Outputs
    mainPanel(
      plotlyOutput("BPS"),
      verbatimTextOutput("event")
    )
  )
)
# Server code
server <- function(input, output) {

  # Create Subset of Data for GW 
  GW_subset <- reactive({
    req(input$RoundNo)
    filter(BPSvsPPG2, RoundNo %in% input$RoundNo & PositionsList %in% input$Pos)
  })

  # renderPlotly
  output$BPS <- renderPlotly({
    ggplotly(ggplot(GW_subset(), aes_string(x = input$x, y = input$y)) + geom_point() + facet_wrap(~input$Pos)
    )})

  # renderPrint
  output$event <- renderPrint({
    d <- event_data("plotly_hover") 
    if (is.null(d)) "Hover on a point!" else d
  })
}

shinyApp(ui, server)

dput(head(BPSvsPPG))的输出

> dput(head(BPSvsPPG))
structure(list(Full.Name = c("Tammy Abraham", "Adam Smith", "Adrian", 
"Sergio Aguero", "Nathan Ake", "Marc Albrighton"), Current.Cost = c(5.5, 
4.5, 4.5, 11, 5, 5.4), GW = c("GW1", "GW1", "GW1", "GW1", "GW1", 
"GW1"), BPSLastRound = c("0", "25", "0", "4", "33", "0"), FirstName = c("Tammy", 
"", "", "Sergio", "Nathan", "Marc"), Surname = c("Abraham", "Adam Smith", 
"Adrian", "Aguero", "Ake", "Albrighton"), PositionsList = c("FWD", 
"DEF", "GLK", "FWD", "DEF", "MID"), Team = c("CHE", "BOU", "WHU", 
"MCI", "BOU", "LEI"), Cost = c(5500000L, 4500000L, 4500000L, 
11000000L, 5000000L, 5400000L), PointsLastRound = c(0L, 6L, 0L, 
2L, 8L, 0L), TotalPoints = c(0L, 6L, 0L, 2L, 8L, 0L), AveragePoints = c(0, 
6, 0, 2, 8, 0), AveragePointsPerDollar = c(0, 1.33e-06, 0, 1.82e-07, 
1.6e-06, 0), TotalPointsPerDollar = c(0, 1.33e-06, 0, 1.82e-07, 
1.6e-06, 0), GameweekWeighting = c(0L, 0L, 0L, 0L, 0L, 0L), TransfersOut = c(1823L, 
2437L, 1999L, 53898L, 9917L, 13253L), YellowCards = c(0L, 0L, 
0L, 0L, 0L, 0L), GoalsConceded = c(0L, 0L, 0L, 0L, 0L, 0L), GoalsConcededPoints = c(0L, 
0L, 0L, 0L, 0L, 0L), Saves = c(0L, 0L, 0L, 0L, 0L, 0L), SavesPoints = c(0L, 
0L, 0L, 0L, 0L, 0L), GoalsScored = c(0L, 0L, 0L, 0L, 0L, 0L), 
    GoalsScoredPoints = c(0L, 0L, 0L, 0L, 0L, 0L), ValueSeason = c(0, 
    1.3, 0, 0.2, 1.6, 0), TransfersOutRound = c(1823L, 2437L, 
    1999L, 53898L, 9917L, 13253L), PriceRise = c(0L, 0L, 0L, 
    0L, 0L, -1L), PriceFallRound = c(0L, 0L, 0L, 0L, 0L, 1L), 
    LastSeasonPoints = c(0L, 6L, 0L, 2L, 8L, 0L), PriceFall = c(0L, 
    0L, 0L, 0L, 0L, 1L), ValueForm = c(0, 1.3, 0, 0.2, 1.6, 0
    ), PenaltiesMissed = c(0L, 0L, 0L, 0L, 0L, 0L), Form = c(0, 
    6, 0, 2, 8, 0), Bonus = c(0L, 0L, 0L, 0L, 2L, 0L), FanRating = c(0L, 
    0L, 0L, 0L, 0L, 0L), CleanSheets = c(0L, 1L, 0L, 1L, 1L, 
    0L), CleanSheetPoints = c(0L, 0L, 0L, 0L, 0L, 0L), Assists = c(0L, 
    0L, 0L, 0L, 0L, 0L), SelectedByPercent = c(0.2, 0.7, 0.5, 
    33.2, 4.3, 0.9), TransfersIn = c(416L, 7257L, 212L, 135506L, 
    26175L, 384L), OwnGoals = c(0L, 0L, 0L, 0L, 0L, 0L), EAIndex = c(0L, 
    0L, 0L, 0L, 0L, 0L), PenaltiesSaved = c(0L, 0L, 0L, 0L, 0L, 
    0L), DreamteamCount = c(0L, 0L, 0L, 0L, 0L, 0L), MinutesPlayed = c(0L, 
    90L, 0L, 78L, 90L, 0L), TransfersInRound = c(416L, 7257L, 
    212L, 135506L, 26175L, 384L), PriceRiseRound = c(0L, 0L, 
    0L, 0L, 0L, -1L), RedCards = c(0L, 0L, 0L, 0L, 0L, 0L), BPS = c(0L, 
    25L, 0L, 4L, 33L, 0L), RoundNo = c(1L, 1L, 1L, 1L, 1L, 1L
    ), AverageBPS = c(0, 25, 0, 4, 33, 0)), row.names = c(NA, 
6L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

刻面包装有几个问题。您试图将这些选项作为PositionsList列的唯一观察值,但是facet wrap参数应该是列名,而不是列中的观察值。另外,您还希望将闪亮的输入与~符号结合使用,而您需要使用paste0("~", input$Pos)

如果目标是将图表分为不同位置的构面,则可以将该列名称硬编码为构面函数。

要在ggplotly中添加工具提示,请为text的美观程度提供一个geom_point参数,并在其中添加您要填充工具提示的列,然后在tooltip = "text"中使用ggplotly功能。

我认为下面的代码应该可以帮助您:

output$BPS <- renderPlotly({

  p <- ggplot(GW_subset(), aes_string(x = input$x, y = input$y)) + 
    geom_point(aes(text = Full.Name)) + 
    facet_wrap(~PositionsList)

  ggplotly(p, tooltip = "text")

})