如何将热图与直方图连接起来?

How to connect heatmap with histogram?

让我们考虑一下我的热图闪亮代码:

library(shiny)
library(plotly)
library(quantmod)

#Data 

start <- as.Date("2013-01-01")
end <- as.Date("2016-10-01")
#Apple stock 
getSymbols("AAPL", src = "yahoo", from = start, to = end)
apple <- AAPL$AAPL.Close
#Gold
getSymbols('GOLD', src = 'yahoo', from = start, to = end)
gold <- GOLD$GOLD.Close
#S&P
getSymbols('^GSPC', src = 'yahoo', from = start, to = end)
sp <- as.numeric(`GSPC`[,4])
#Microsoft
getSymbols('MSFT', src = 'yahoo', from = start, to = end) 
msft <- MSFT$MSFT.Close

stock.frame <- data.frame(apple, gold, msft, sp)
colnames(stock.frame) <- c('apple', 'gold', 'msft', 'sp')


cmat <- cor(stock.frame)
plot_ly(z = cmat, type = "heatmap")
ui <- fluidPage(
    mainPanel(
        plotlyOutput("heatmap", width = "100%", height="600px")
    )
)

## server.R
server <- function(input, output) {
    output$heatmap <- renderPlotly({plot_ly(x = colnames(stock.frame), y = colnames(stock.frame), z = cmat, type = "heatmap") %>%
            layout(
                xaxis = list(title=colnames(stock.frame)),
                yaxis = list(title="ts")
            )
        
        })
}
shinyApp(ui,server)

还有我的直方图:

UI

library(shiny) # load the shiny package

# Define UI for application
shinyUI(fluidPage(
    
    # Header or title Panel 
    titlePanel(h4('Demostration of the renderPlot() - A Histogram with stock dataset', align = "center")),
    
    # Sidebar panel
    sidebarPanel(
        
        
        
        selectInput("var", label = "1. Select the quantitative Variable", 
                    choices = c("Apple" = 1, "Gold" = 2, "S&P" = 3, "BTC"=4),
                    selected = 3), 
        
        
        sliderInput("bin", "2. Select the number of histogram BINs by using the slider below", min=5, max=50, value=15),
        
        radioButtons("colour", label = "3. Select the color of histogram",
                     choices = c("Green", "Red",
                                 "Yellow"), selected = "Green")
    ),
    
    # Main Panel
    mainPanel(
        textOutput("text1"),
        textOutput("text2"),
        textOutput("text3"),
        plotOutput("myhist")
        
    )
    
)
)

服务器

library(shiny) # Load shiny package


start <- as.Date("2013-01-01")
end <- as.Date("2016-10-01")
#Apple stock 
getSymbols("AAPL", src = "yahoo", from = start, to = end)
apple <- AAPL$AAPL.Close
#Gold
getSymbols('GOLD', src = 'yahoo', from = start, to = end)
gold <- GOLD$GOLD.Close
#S&P500 
getSymbols('^GSPC', src = 'yahoo', from = start, to = end)
sp <- as.numeric(`GSPC`[,4])
#Microsoft
getSymbols('MSFT', src = 'yahoo', from = start, to = end) 
msft <- MSFT$MSFT.Close

stock.frame <- data.frame(apple, gold, msft, sp)
colnames(stock.frame) <- c('apple', 'gold', 'msft', 'sp')


shinyServer(
  function(input, output) {
    output$myhist <- renderPlot({
      colm <- as.numeric(input$var)
      hist(stock.frame[, colm], col = input$colour, xlim = c(min(stock.frame[, colm]), max(stock.frame[, colm])), main = "Histogram of stock dataset", breaks = seq(min(stock.frame[, colm]), max(stock.frame[, colm]), l = input$bin + 1), xlab = names(stock.frame[colm]))
    })
  }
)

我想要的是一个特殊的列表,我可以在其中选择是要热图还是直方图。因此,尽可能简单地说,我希望可以在一个应用程序中在热图或直方图之间切换(因为现在我有两个单独的应用程序)。您知道如何执行吗?

实现此目的的一种方法是使用 conditionalPanel,如下所示。

library(shiny) 
library(quantmod)

start <- as.Date("2013-01-01")
end <- as.Date("2016-10-01")
#Apple stock 
getSymbols("AAPL", src = "yahoo", from = start, to = end)
apple <- AAPL$AAPL.Close
#Gold
getSymbols('GOLD', src = 'yahoo', from = start, to = end)
gold <- GOLD$GOLD.Close
#S&P500 
getSymbols('^GSPC', src = 'yahoo', from = start, to = end)
sp <- as.numeric(`GSPC`[,4])
#Microsoft
getSymbols('MSFT', src = 'yahoo', from = start, to = end) 
msft <- MSFT$MSFT.Close

stock.frame <- data.frame(apple, gold, msft, sp)
colnames(stock.frame) <- c('apple', 'gold', 'msft', 'sp')
cmat <- cor(stock.frame)
### plot_ly(z = cmat, type = "heatmap")

### Define UI for application
ui <- fluidPage(
  
  # Header or title Panel 
  titlePanel(h4('Demostration of the renderPlot() - A Histogram with stock dataset', align = "center")),
  
  # Sidebar panel
  sidebarPanel(
    selectInput("var", label = "1. Select the quantitative Variable", 
                choices = c("Apple" = 1, "Gold" = 2, "S&P" = 3, "BTC"=4),
                selected = 3), 
    sliderInput("bin", "2. Select the number of histogram BINs by using the slider below", min=5, max=50, value=15),
    radioButtons("graphtype", label = "Select Type of Graph",
                 choices = c("Heatmap", "Histogram"), selected = "Heatmap"),
    conditionalPanel(
      condition = "input.graphtype == 'Histogram' ", 
      radioButtons("colour", label = "3. Select the color of histogram",
                   choices = c("Green", "Red", "Yellow"), selected = "Green")
    )
    
  ),
  
  # Main Panel
  mainPanel(
    textOutput("text1"),
    textOutput("text2"),
    textOutput("text3"),
    conditionalPanel(
      condition = "input.graphtype == 'Heatmap' ", plotlyOutput("heatmap", width = "100%", height="600px")
    ),
    conditionalPanel(
      condition = "input.graphtype == 'Histogram' ", plotOutput("myhist") 
    )
  )
  
)


server <-   function(input, output) {
    output$myhist <- renderPlot({
      colm <- as.numeric(input$var)
      hist(stock.frame[, colm], col = input$colour, xlim = c(min(stock.frame[, colm]), max(stock.frame[, colm])), main = "Histogram of stock dataset", breaks = seq(min(stock.frame[, colm]), max(stock.frame[, colm]), l = input$bin + 1), xlab = names(stock.frame[colm]))
    })
    
    output$heatmap <- renderPlotly({plot_ly(x = colnames(stock.frame), y = colnames(stock.frame), z = cmat, type = "heatmap") %>%
        layout(
          xaxis = list(title=colnames(stock.frame)),
          yaxis = list(title="ts")
        )
    })
}

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