繁体   English   中英

R 中 Shiny 应用程序中的图形布局; 使布局更简洁

[英]Figure layout within Shiny app in R; making the layout more concise

我正在用 R 开发一个闪亮的应用程序,我需要一些关于如何通过使其更简洁来改进应用程序布局的指示。 我有一个sidebarPanel ,其中包含一些列结构图,上半部分如下所示:-

在此处输入图片说明

随着更多的情节进一步下降。 但是,我希望在第一个情节之后,有些人坐在sidebarPanel下方,两个两个,如下所示:-

在此处输入图片说明

这是我的代码:-


#libraries ====================================
library(shiny)
library(tidyverse)
library(ggplot2)
library(cluster)    # clustering algorithms
library(factoextra) # clustering algorithms & visualization




#filter choices=================================


choices1 = c("AAA","BBB","CCC","DDD")




#ui===============================




ui<-fluidPage(
  titlePanel('Minimal example'),
  tabsetPanel(
    
    tabPanel("example text",
             #sidebarLayout(
             sidebarPanel(width = 4,
                          dateRangeInput("daterangeinput", "Select date range", format = "yyyy-mm-dd",
                                         start = min("2021-06-05"),
                                         end = max("2021-06-15")),
                          numericInput("start", "Select minimum",10,min=0, max=23),
                          numericInput("end", "Select maximum",22, min=0, max=23),
                          pickerInput("choice", "Pick something",
                                      choices = choices1, options = list('actions-box'=TRUE,'live-search'=TRUE), multiple = T)),
             mainPanel(fluidRow(
               column(width = 8, h4("Cluster plot"), plotOutput("clusterplot", width = "100%")), 
               column(width = 8, h4("Scatter plot"),plotOutput("scatterplot", width="100%")),
               column(width = 8, h4("Box plot"),plotOutput("boxplot", width ="100%")),
               column(width = 8, h4("Histogram"),plotOutput("histogram", width ="100%")),
               column(width = 8, h4("Bar plot"),plotOutput("barplot", width ="100%")))),
    )#end of tabpanel
  )#end of tabset panel
)#end of fluidpage/UI




#server ==========================


server<-function(input,output,session){
    
  
  #clustering
  scaledData <- scale(iris[,1:4])
  irisCluster <- kmeans(scaledData, center=3, nstart=20)
  irisCluster

    
    output$scatterplot<-renderPlot({
      
      scatter <- ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width)) 
      scatter + geom_point(aes(color=Species, shape=Species)) +
        xlab("Sepal Length") +  ylab("Sepal Width") +
        ggtitle("Sepal Length-Width")

      
    })
    
    
    
    output$boxplot<-renderPlot({
      
      box <- ggplot(data=iris, aes(x=Species, y=Sepal.Length))
      box + geom_boxplot(aes(fill=Species)) + 
        ylab("Sepal Length") + ggtitle("Iris Boxplot") +
        stat_summary(fun.y=mean, geom="point", shape=5, size=4) 
    })
    
    
    
    
    
    output$histogram<-renderPlot({
      
      histogram <- ggplot(data=iris, aes(x=Sepal.Width))
      histogram + geom_histogram(binwidth=0.2, color="black", aes(fill=Species)) + 
        xlab("Sepal Width") +  ylab("Frequency") + ggtitle("Histogram of Sepal Width")
      
    })
    
    
    output$barplot<-renderPlot({
      
      set.seed(1234)
      iris1 <- iris[sample(1:nrow(iris), 110), ]
      hline <- data.frame(Species=c("setosa", "versicolor", "virginica"), hline=as.vector(table(iris$Species)))
      hline
      
      
      bar <- ggplot(data=iris1, aes(x=Species))
      bar + geom_bar() + 
        xlab("Species") +  ylab("Count") + ggtitle("Bar plot of Sepal Length") +
        geom_errorbar(data=hline, aes(y=hline, ymin=hline, ymax=hline), col="red", linetype="dashed")
      
    })

    #cluster plot ======================
    
    output$clusterplot<-renderPlot({
      
      fviz_cluster(irisCluster, data = scaledData, geom = "")+
        theme(axis.title.x = element_blank(), axis.title.y = element_blank())
      
    })
    
  
}


shinyApp(ui,server)

有人可以向我展示我需要对UI进行的调整,以便我可以获得所需的输出吗?

谢谢!

您可以将除第一个之外的所有其他图放在单独的行中,如下所示:

ui<-fluidPage(
  titlePanel('Minimal example'),
  tabsetPanel(
    
    tabPanel("example text",
             #sidebarLayout(
             sidebarPanel(width = 4,
                          dateRangeInput("daterangeinput", "Select date range", format = "yyyy-mm-dd",
                                         start = min("2021-06-05"),
                                         end = max("2021-06-15")),
                          numericInput("start", "Select minimum",10,min=0, max=23),
                          numericInput("end", "Select maximum",22, min=0, max=23),
                          shinyWidgets::pickerInput("choice", "Pick something",
                                      choices = choices1, options = list('actions-box'=TRUE,'live-search'=TRUE), multiple = T)),
             mainPanel(
               fluidRow(
                 column(width = 8, h4("Cluster plot"), plotOutput("clusterplot", width = "100%"))
                 )
               )
             )
    ),
  fluidRow(
    column(width = 6, h4("Scatter plot"),plotOutput("scatterplot", width="100%")),
    column(width = 6, h4("Box plot"),plotOutput("boxplot", width ="100%")),
    column(width = 6, h4("Histogram"),plotOutput("histogram", width ="100%")),
    column(width = 6, h4("Bar plot"),plotOutput("barplot", width ="100%"))
    )
  )

如果您希望左侧的绘图与侧边栏面板对齐,您可以将它们的宽度调整为“4”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM