R 中 Shiny 应用程序中的图形布局;使布局更简洁
Posted
技术标签:
【中文标题】R 中 Shiny 应用程序中的图形布局;使布局更简洁【英文标题】:Figure layout within Shiny app in R; making the layout more concise 【发布时间】:2021-12-23 19:57:26 【问题描述】:我正在用 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", )),
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
进行的调整,以便获得所需的输出?
谢谢!
【问题讨论】:
【参考方案1】:您可以将除第一个之外的所有其他图放在单独的行中,如下所示:
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", )),
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”。
【讨论】:
这正是我所追求的。谢谢!以上是关于R 中 Shiny 应用程序中的图形布局;使布局更简洁的主要内容,如果未能解决你的问题,请参考以下文章
三十Java图形化界面设计——布局管理器之GridLayout(网格布局)
如何将应用程序的布局更改为 LandScape 以及如何使应用程序的布局适应移动设备和平板电脑