将轴标签冻结在R闪亮的图形顶部

问题描述 投票:0回答:1

我有一个很长的 renderGirafe。我希望用户向下滚动后能够看到 x 轴值(位于图的顶部)。

这是示例代码。在这段代码中,我希望用户在向下滚动时仍然能够看到年份。我对生成什么类型的图非常灵活,只要将鼠标悬停在每个块上时显示标签即可。欢迎提出建议!

library(shiny)
library(tidyverse)
library(ggiraph)

ui <- fluidRow(girafeOutput("my_plot"))

server <- function(input, output){
  output$my_plot <- renderGirafe({
    
    data <- data.frame(
      letter = rep(c(LETTERS, letters, paste0(LETTERS, letters))),
      year = factor(rep_len(2000:2008, 78)),
      value = floor(runif(78, 1, 6))
    )
    
    g <- ggplot(data) + 
      geom_tile_interactive(aes(x = year, y = letter, fill = value, tooltip = value)) + 
      scale_x_discrete(position = "top") + 
      coord_fixed() +
      theme_classic() + 
      theme(axis.text.x = element_text(
        angle = 90,
        hjust = 0.5,
        vjust = 0,
        size = 17
      )) 
    
    girafe(ggobj = g,  
           options = list(opts_sizing(rescale = F)),
           width_svg = 20,
           height_svg = 50
    )
  })
}

shinyApp(ui = ui, server = server)
javascript css r shiny ggiraph
1个回答
0
投票

使用

plotly
,您可以在悬停时滚动 y 轴,请参阅图像中的光标:

hovering axis

用户必须知道这一点,并且没有明显的滚动条(“rangeslider”可以完成这项工作,但这不适用于 y 轴),因此这并不理想,但结果仍然达到了。

library(shiny)
library(tidyverse)

ui <- fluidRow(
  plotlyOutput("my_plot")
)

server <- function(input, output){
  output$my_plot <- renderPlotly({
    
    data <- data.frame(
      letter = rep(c(LETTERS, letters, paste0(LETTERS, letters))),
      year = factor(rep_len(2000:2008, 78)),
      value = floor(runif(78, 1, 6))
    )

    g <- ggplot(data) + 
      geom_tile(aes(x = year, y = letter, fill = value), show.legend = F) + 
      scale_x_discrete(position = "top") + 
      coord_fixed() +
      theme_classic() + 
      theme(axis.text.x = element_text(
        angle = 90,
        hjust = 0.5,
        vjust = 0,
        size = 17
      )) 

    # adjust the height to your needs:
    ggplotly(g, height = 5000) %>% 
      layout(xaxis = list(side = "top"))

  })
}

shinyApp(ui = ui, server = server)
© www.soinside.com 2019 - 2024. All rights reserved.