微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 R Shiny 中组织图

如何解决如何在 R Shiny 中组织图

所以我有一个数据子集,看起来像:

dummy_stage <- structure(list(USER_TYPE = c("external","external","internal","unidentified","external"),SESSION_DATE = structure(c(18694,18695,18696,18697,18698,18699,18700,18701,18702,18703,18704,18705),class = "Date"),DAILY_ACTIVE_USERS = c(23,279,6,1,146,4,10,186,8,54),SESSIONS_PER_USER = c(2,3,5,2),DAILY_ACTIVE_USERS_WITH_EVENTS = c(31,37,43,52,22,18,95,32,81,65,80,91,78),DAILY_SESSIONS = c(1,97,77,99,29,62,71,78,87,0),SECONDS_PER_SESSION = c(8171,3360,5390,2261,1780,9200,6066,1505,8053,1603,514,5150,6506),PAGE_VISIT_COUNT = c(1221,86,11,66,98,42,53,100,21),EVENTS_PER_SESSION = c(3,2,7,4),PAGEVIEWS_PER_SESSION = c(8,9,5)),row.names = c(NA,-13L
),class = c("tbl_df","tbl","data.frame"))

我正在构建一个闪亮的应用程序,它使用户能够与这些数据进行交互。第一次闪亮的用户我有两个问题:

  1. 我想在 Shiny Dashboard 的第一个选项卡中显示四个图。我希望它们以二乘二的布局存在,但是当前两个处于正确位置时,第三和第四个图堆叠甚至位于选项卡区域下方,这是一个问题。

  2. 我想在主选项卡上显示四个 ggplot。我不〜爱〜他们有白色背景。我更喜欢它们的颜色与白色不同,最好是透明的。

代码如下:

# Test App for Stage
library(shiny)
library(shinydashboard)
library(viridis)
library(hrbrthemes)
library(scales)
library(readxl)
library(tidyverse)

# Data Flow ----

##Todo Replace and implement SNowflake data.
dummy_stage <- read_excel("~/Documents/dummy_stage.xlsx",col_types = c("text","date","numeric","numeric")) %>% 
  mutate(SESSION_DATE = as.Date(SESSION_DATE))





# Define UI ----
ui <- dashboardPage(
  dashboardHeader(title = "Title Dashboard 2.0"),dashboardSidebar(    
    sidebarMenu(
      menuItem("Daily Session Stats",tabName = "Daily",icon = icon("dashboard")),menuItem("Page & Events Stats",tabName = "wee",menuItem("Booking Inquiry Details",tabName = "hee",icon = icon("book"))
    )
  ),dashboardBody(
    tabItems(
      #Tab 1
      tabItem(
        tabName = "Daily",titlePanel("Daily Session Stats"),fluidRow(
          Box(
            checkBoxGroupInput("checkGroup",h4("User Type"),choices = list("external" = 1,"internal" = 2,"unidentified" = 3),selected = 1),daterangeInput("dates",h4("Session Date")))
        ),column(6,plotOutput("Plot1")),plotOutput("Plot2")),column(7,plotOutput("Plot3")),column(8,plotOutput("Plot4"))
#Could be error here.
      ),# Second tab content
      tabItem(tabName = "wee",titlePanel("Tab 2")
      ),# Third tab content
      tabItem(tabName = "hee",titlePanel("Tab 3")
      )
    )
  )
)


server <- function(input,output) {
  
  #
  output$Plot1 <- renderPlot({
    ggplot(dummy_stage %>% 
             group_by(SESSION_DATE) %>% 
             summarize(DAILY_ACTIVE_USERS_SUM = sum(DAILY_ACTIVE_USERS)),aes(y=DAILY_ACTIVE_USERS_SUM,x=SESSION_DATE)) + 
      geom_col(fill = "steelblue") +
      #  scale_fill_viridis(discrete = T) +
      ggtitle("Plot 1") +
      theme(axis.text.x = element_text(face="bold",angle=45,vjust = .5),panel.background = element_blank())+ # Todo HERE
      scale_x_date(date_breaks  ="1 day")
    
    
    
  })
  
  output$Plot2 <- renderPlot({
    ggplot(dummy_stage %>% 
             group_by(SESSION_DATE,USER_TYPE) %>% 
             summarize(DAILY_ACTIVE_USERS_WITH_EVENTS_SUM = sum(DAILY_ACTIVE_USERS_WITH_EVENTS)),aes(fill=USER_TYPE,y=DAILY_ACTIVE_USERS_WITH_EVENTS_SUM,x=SESSION_DATE)) + 
      geom_bar(position="stack",stat="identity") +
      scale_fill_viridis(discrete = T) +
      ggtitle("Plot 2") +
      theme(axis.text.x = element_text(face="bold",vjust = .5))+
      scale_x_date(date_breaks  ="1 day")
    
    
    
  })
  
  output$Plot3 <- renderPlot({
    ggplot(dummy_stage %>% 
             group_by(SESSION_DATE,USER_TYPE) %>% 
             summarize(DAILY_SESSIONS_SUM = sum(DAILY_SESSIONS)),y=DAILY_SESSIONS_SUM,stat="identity") +
      scale_fill_viridis(discrete = T) +
      ggtitle("Plot 3") +
      theme(axis.text.x = element_text(face="bold",vjust = .5))+
      scale_x_date(date_breaks  ="1 day")
    
  })
  
  output$Plot4 <- renderPlot({
    ggplot(dummy_stage %>% 
             group_by(SESSION_DATE) %>% 
             summarize(EVENTS_PER_SESSION = mean(EVENTS_PER_SESSION),PAGEVIEWS_PER_SESSION = mean(PAGEVIEWS_PER_SESSION)),aes(x=SESSION_DATE)) + 
      geom_line(aes(y = EVENTS_PER_SESSION),color = "darkred") + 
      geom_line(aes(y = PAGEVIEWS_PER_SESSION),color="steelblue") +
      ggtitle("Plot 4") +
      theme(axis.text.x = element_text(face="bold",vjust = .5))+
      scale_x_date(date_breaks  ="1 day")
    
    
    
  })
  
}


# Run the app ----
shinyApp(ui = ui,server = server)

我认为将它们放在 col() 中会有所帮助但无济于事,开始认为这是不对的。

最后,我希望在破折号的第一个标签中有四个图 (2x2),最好在没有白色背景的情况下看起来不错。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。