在一个 renderPlot 中可视化多个绘图 - r shiny
Visualize multiple plots in one renderPlot - rshiny
我正在尝试构建我的第一个闪亮应用,下面是我的示例代码:
library(shiny)
library(ggplot2)
library(gridExtra)
data('mtcars')
if (interactive()) {
shinyApp(
ui = fluidPage(
sliderInput('num',label='Insert Number of clusters',value = 3,min = 2,max = 10,step = 1),
plotOutput("data")
),
server = function(input, output) {
output$data<-renderPlot({autoplot(kmeans(mtcars,input$num),data=mtcars,label=TRUE,label.size=3)})
}
)}
现在我想要实现的是将聚类与原始数据结合起来,并在按聚类列分组后可视化所有变量的直方图和箱线图。例如,
如果用户选择5个簇,我的最终数据可以如下:
mtcars_with_clsuters<-cbind(mtcars,clusters$cluster)
mpg cyl disp hp drat wt qsec vs am gear carb cluster
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 3
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 5
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 2
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 2
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 4
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 2
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 1
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 2
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 4
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 2
Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 1
Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 1
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 4
Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 5
所以现在我想按聚类列对我的数据进行分组,并在上面 renderPlot
内的 k-means 聚类可视化旁边可视化它。
如有任何帮助,我们将不胜感激。
您可以尝试使用gridExtra
将多个绘图排列在一起,然后在renderPlot
中输出此绘图(或使用facets with ggplot2
)。但是,您也可以使用 fluidRow
/column
系统来构建页面并输出多个图。请参阅以下带有聚类图和一个箱线图的解决方案:
library(shiny)
library(ggplot2)
library(ggfortify)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput('num',label='Insert Number of clusters',value = 3,min = 2,max = 10,step = 1)
),
mainPanel(
fluidRow(
column(width = 6,
plotOutput("data")
),
column(width = 6,
plotOutput("boxplot"))
)
)
)
)
server <- function(input, output, session) {
clust_data <- reactive({
kmeans(mtcars,input$num)
})
output$data<-renderPlot({autoplot(clust_data(),data=mtcars,label=TRUE,label.size=3)})
output$boxplot <- renderPlot({
mtcars_with_clusters <- cbind(mtcars, clust_data()$cluster)
colnames(mtcars_with_clusters) <- c(colnames(mtcars_with_clusters[-ncol(mtcars_with_clusters)]),
"cluster")
boxplot(mpg ~ cluster, data = mtcars_with_clusters)
})
}
shinyApp(ui, server)
我正在尝试构建我的第一个闪亮应用,下面是我的示例代码:
library(shiny)
library(ggplot2)
library(gridExtra)
data('mtcars')
if (interactive()) {
shinyApp(
ui = fluidPage(
sliderInput('num',label='Insert Number of clusters',value = 3,min = 2,max = 10,step = 1),
plotOutput("data")
),
server = function(input, output) {
output$data<-renderPlot({autoplot(kmeans(mtcars,input$num),data=mtcars,label=TRUE,label.size=3)})
}
)}
现在我想要实现的是将聚类与原始数据结合起来,并在按聚类列分组后可视化所有变量的直方图和箱线图。例如,
如果用户选择5个簇,我的最终数据可以如下:
mtcars_with_clsuters<-cbind(mtcars,clusters$cluster)
mpg cyl disp hp drat wt qsec vs am gear carb cluster
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 3
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 5
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 2
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 2
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 4
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 2
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 1
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 2
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 4
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 2
Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 1
Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 1
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 4
Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 5
所以现在我想按聚类列对我的数据进行分组,并在上面 renderPlot
内的 k-means 聚类可视化旁边可视化它。
如有任何帮助,我们将不胜感激。
您可以尝试使用gridExtra
将多个绘图排列在一起,然后在renderPlot
中输出此绘图(或使用facets with ggplot2
)。但是,您也可以使用 fluidRow
/column
系统来构建页面并输出多个图。请参阅以下带有聚类图和一个箱线图的解决方案:
library(shiny)
library(ggplot2)
library(ggfortify)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput('num',label='Insert Number of clusters',value = 3,min = 2,max = 10,step = 1)
),
mainPanel(
fluidRow(
column(width = 6,
plotOutput("data")
),
column(width = 6,
plotOutput("boxplot"))
)
)
)
)
server <- function(input, output, session) {
clust_data <- reactive({
kmeans(mtcars,input$num)
})
output$data<-renderPlot({autoplot(clust_data(),data=mtcars,label=TRUE,label.size=3)})
output$boxplot <- renderPlot({
mtcars_with_clusters <- cbind(mtcars, clust_data()$cluster)
colnames(mtcars_with_clusters) <- c(colnames(mtcars_with_clusters[-ncol(mtcars_with_clusters)]),
"cluster")
boxplot(mpg ~ cluster, data = mtcars_with_clusters)
})
}
shinyApp(ui, server)