将绘图放在左下角(侧边栏面板下方)
Place plot at bottom left (below sidebar panel)
我有这个应用程序。
library(shiny)
library(tidyverse)
library(dplyr)
a <- 1:5
df <- tibble(a, b = a * 2, c = b * 3)
a <- c(2, 3, 4, 1, 5)
test <- tibble(a, b = a * 2, c = b * 3)
ui <- fluidPage(
titlePanel("The title"),
sidebarLayout(
sidebarPanel(
fluidRow(
column(6,
wellPanel(
selectInput("features", "Choose a feature",
c("All", "b", "c"))
),
column(6,
selectInput("features_dens", "Choose a feature for density plots",
c("All", "b", "c"))
)
)
)
),
# Show a plot of the generated distribution
mainPanel(
tabsetPanel(
tabPanel("Plot",
fluidRow(column(10, plotOutput("actual")),
column(10, plotOutput("resids")))
),
tabPanel("Summary", verbatimTextOutput("summary")),
tabPanel("Table", dataTableOutput("sum_stats_table"))
)
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$summary <- renderPrint({
if (input$features == "All"){
df_lm <- lm(a~., data = df)
} else if (input$features == "b") {
df_lm <- lm(a~ b, data = df)
} else if (input$features == "c") {
df_lm <- lm(a~ c, data = df)
}
summary(df_lm)
})
output$sum_stats_table <- renderDataTable({
if (input$features == "All"){
df_lm <- lm(a~., data = df)
} else if (input$features == "b") {
df_lm <- lm(a~ b, data = df)
} else if (input$features == "c") {
df_lm <- lm(a~ c , data = df)
}
psych::describe(df, fast = TRUE)
})
output$actual <- renderPlot({
if (input$features == "All"){
df_lm <- lm(a~., data = df)
} else if (input$features == "b") {
df_lm <- lm(a~ b, data = df)
} else if (input$features == "c") {
df_lm <- lm(a~ c ,data = df)
}
preds <- predict(df_lm, test)
ggplot(data = test, aes(x = preds, y = a)) +
geom_point(alpha = 0.8, color = "darkgreen") +
geom_smooth(color="darkblue") +
geom_line(aes(x = (a),
y = (a)),
color = "blue", linetype=2) +
labs(title="Actual vs Predicted",
x ="Predicted", y = "Actual")
})
output$resids <- renderPlot({
if (input$features == "All"){
df_lm <- lm(a~., data = df)
} else if (input$features == "b") {
df_lm <- lm(a~ b, data = df)
} else if (input$features == "c") {
df_lm <- lm(a~ c , data = df)
}
preds <- predict(df_lm, test)
ggplot(data=test, aes(x = preds,
y = preds - a)) +
geom_point(alpha=0.8, color="darkgreen") +
geom_smooth(color="darkblue") +
labs(title="Residuals",
x ="Predicted", y = "residual error (prediction - actual)")
})
output$density <- renderPlot(
{
ggplot(df, aes(x=input$features_dens)) +
geom_histogram(aes(y=..density..), colour="black", fill="white", binwidth = 0.5)+
geom_density(alpha=0.7, fill='#336eff')
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
生成以下图像:
我想将 output$density
图放在侧边栏面板下方(左下角)
您必须制作自己的侧边栏布局。像这样:
ui <- fluidPage(
titlePanel("The title"),
fluidRow(
# sidebar
column(
width = 4,
tags$form(
class = "well",
fluidRow(
column(
6,
wellPanel(
selectInput(
"features", "Choose a feature",
c("All", "b", "c")
)
),
column(
6,
selectInput(
"features_dens", "Choose a feature for density plots",
c("All", "b", "c")
)
)
)
)
),
br(),
plotOutput("density")
),
# main panel
# Show a plot of the generated distribution
column(
width = 8,
role = "main",
tabsetPanel(
tabPanel(
"Plot",
fluidRow(
column(10, plotOutput("actual")),
column(10, plotOutput("resids"))
)
),
tabPanel("Summary", verbatimTextOutput("summary")),
tabPanel("Table", dataTableOutput("sum_stats_table"))
)
)
)
)
您的服务器代码有错误:
output$density <- renderPlot({
ggplot(df, aes(x = input$features_dens)) +
geom_histogram(aes(y = ..density..), colour = "black", fill = "white", binwidth = 0.5) +
geom_density(alpha = 0.7, fill = "#336eff")
})
将替换为:
output$density <- renderPlot({
ggplot(df, aes_string(x = input$features_dens)) +
......
})
但请注意,当您选择 "All"
时会出现错误。
我有这个应用程序。
library(shiny)
library(tidyverse)
library(dplyr)
a <- 1:5
df <- tibble(a, b = a * 2, c = b * 3)
a <- c(2, 3, 4, 1, 5)
test <- tibble(a, b = a * 2, c = b * 3)
ui <- fluidPage(
titlePanel("The title"),
sidebarLayout(
sidebarPanel(
fluidRow(
column(6,
wellPanel(
selectInput("features", "Choose a feature",
c("All", "b", "c"))
),
column(6,
selectInput("features_dens", "Choose a feature for density plots",
c("All", "b", "c"))
)
)
)
),
# Show a plot of the generated distribution
mainPanel(
tabsetPanel(
tabPanel("Plot",
fluidRow(column(10, plotOutput("actual")),
column(10, plotOutput("resids")))
),
tabPanel("Summary", verbatimTextOutput("summary")),
tabPanel("Table", dataTableOutput("sum_stats_table"))
)
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$summary <- renderPrint({
if (input$features == "All"){
df_lm <- lm(a~., data = df)
} else if (input$features == "b") {
df_lm <- lm(a~ b, data = df)
} else if (input$features == "c") {
df_lm <- lm(a~ c, data = df)
}
summary(df_lm)
})
output$sum_stats_table <- renderDataTable({
if (input$features == "All"){
df_lm <- lm(a~., data = df)
} else if (input$features == "b") {
df_lm <- lm(a~ b, data = df)
} else if (input$features == "c") {
df_lm <- lm(a~ c , data = df)
}
psych::describe(df, fast = TRUE)
})
output$actual <- renderPlot({
if (input$features == "All"){
df_lm <- lm(a~., data = df)
} else if (input$features == "b") {
df_lm <- lm(a~ b, data = df)
} else if (input$features == "c") {
df_lm <- lm(a~ c ,data = df)
}
preds <- predict(df_lm, test)
ggplot(data = test, aes(x = preds, y = a)) +
geom_point(alpha = 0.8, color = "darkgreen") +
geom_smooth(color="darkblue") +
geom_line(aes(x = (a),
y = (a)),
color = "blue", linetype=2) +
labs(title="Actual vs Predicted",
x ="Predicted", y = "Actual")
})
output$resids <- renderPlot({
if (input$features == "All"){
df_lm <- lm(a~., data = df)
} else if (input$features == "b") {
df_lm <- lm(a~ b, data = df)
} else if (input$features == "c") {
df_lm <- lm(a~ c , data = df)
}
preds <- predict(df_lm, test)
ggplot(data=test, aes(x = preds,
y = preds - a)) +
geom_point(alpha=0.8, color="darkgreen") +
geom_smooth(color="darkblue") +
labs(title="Residuals",
x ="Predicted", y = "residual error (prediction - actual)")
})
output$density <- renderPlot(
{
ggplot(df, aes(x=input$features_dens)) +
geom_histogram(aes(y=..density..), colour="black", fill="white", binwidth = 0.5)+
geom_density(alpha=0.7, fill='#336eff')
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
生成以下图像:
我想将 output$density
图放在侧边栏面板下方(左下角)
您必须制作自己的侧边栏布局。像这样:
ui <- fluidPage(
titlePanel("The title"),
fluidRow(
# sidebar
column(
width = 4,
tags$form(
class = "well",
fluidRow(
column(
6,
wellPanel(
selectInput(
"features", "Choose a feature",
c("All", "b", "c")
)
),
column(
6,
selectInput(
"features_dens", "Choose a feature for density plots",
c("All", "b", "c")
)
)
)
)
),
br(),
plotOutput("density")
),
# main panel
# Show a plot of the generated distribution
column(
width = 8,
role = "main",
tabsetPanel(
tabPanel(
"Plot",
fluidRow(
column(10, plotOutput("actual")),
column(10, plotOutput("resids"))
)
),
tabPanel("Summary", verbatimTextOutput("summary")),
tabPanel("Table", dataTableOutput("sum_stats_table"))
)
)
)
)
您的服务器代码有错误:
output$density <- renderPlot({
ggplot(df, aes(x = input$features_dens)) +
geom_histogram(aes(y = ..density..), colour = "black", fill = "white", binwidth = 0.5) +
geom_density(alpha = 0.7, fill = "#336eff")
})
将替换为:
output$density <- renderPlot({
ggplot(df, aes_string(x = input$features_dens)) +
......
})
但请注意,当您选择 "All"
时会出现错误。