闪亮:ggplot的背景图片

时间:2020-01-27 12:20:09

标签: r shiny ggplotly

我制作了一个以png为背景的ggplot。将此工作空间本地化没有问题。但作为一个闪亮的情节是行不通的。

Server.R

library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(dashboardthemes)
library(DT)
library(png)
library(rasterImage)
library(ggpubr)
library(plotly)

# Define server logic required to draw a histogram
shinyServer(function(input, output, session) {

    output$ShootPosition <- renderPlotly({

        data <- data.frame(x = rnorm(100),
                           y = rnorm(100))
        ggplot(data, aes(x, y), tooltip = TRUE) +
               background_image(readPNG("test.png")) +
               geom_point()
      }) 

    }
)

ui.R

library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(dashboardthemes)
library(DT)
library(png)
library(rasterImage)
library(ggpubr)
library(plotly)

header <- dashboardHeader(
    title = "Test"
)

sidebar <- dashboardSidebar(
    sidebarMenu(
        menuItem("Tables", icon = icon("table"), 
                 menuSubItem("Players", tabName = "Tables_Players")
        )
    )
)

body <- dashboardBody(
    tabItems(
        tabItem("Tables_Players",
                fluidPage(
                    titlePanel("Charts Players"),
                    fluidRow(
                        plotlyOutput("ShootPosition", height = '800px')
                    )
                )
        )
    )
)


ui = dashboardPage(
    header,
    sidebar,
    body)

目标是制作冰球射击图。

1 个答案:

答案 0 :(得分:0)

在服务器部分,您没有为ggplotly定义renderPlotly对象。只有ggplot对象不会在renderPlotly中呈现。请定义p<-ggplot()+...,然后将ggplotly(p)定义为renderPlotly中的最后一条语句。应该可以。