【发布时间】:2021-04-21 18:56:00
【问题描述】:
我正在做一个项目,我们必须创建一个显示数据的 R Shiny 应用程序。我有四个定量变量(个人、社交、体重和饮食)。我希望在应用程序中指定时,在学校的每一年和每种性别都有 4 个密度图相互叠加。密度图不会出现,无论我做什么,它们都不会出现。任何建议都会很有帮助!
library(shiny)
library(ggplot2)
library(purrr)
library(dplyr)
#plotting theme for ggplot2
.theme<- theme(
axis.line = element_line(colour = 'gray', size = .75),
panel.background = element_blank(),
plot.background = element_blank()
)
# UI for app
ui<-(pageWithSidebar(
# title
headerPanel("Select Options for Body Image and Disordered Eating Among UW-Madison Students"),
#input
sidebarPanel
( # Input: Select what to display
selectInput("gender", "Gender:",
choices = c("Female",
"Male",
"Nonbinary",
"Do not care to say"),
selected = "Female"),
selectInput("school", "Year in School:",
choices = c("First Year",
"Second Year",
"Third Year",
"Fourth Year"),
selected = "First Year")),
mainPanel(plotOutput("densityplot"))
)
)
# --------------------------------------------SERVER---------------------------------------------
server<-(function(input, output){
output$plot <- renderUI({
plotOutput("p")
data = switch(input$gender,
"Female"="Female",
"Male"= "Male",
"Nonbinary"= "Nonbinary",
"Do not care to say" = "Do not care to say")
data = switch(input$school,
"First Year"="First Year",
"Second Year"= "Second Year",
"Third Year"= "Third Year",
"Fourth Year" = "Fourth Year")
data = switch(output$var,
"Personal"= cleandatafull$personal,
"Social"= cleandatafull$social,
"Weight"= cleandatafull$weight,
"Eating" = cleandatafull$eating)
output$densityplot<-renderPlot({
ggplot(cleandatafull, aes(x=output$var)) +
geom_density(adjust=1.5, alpha=.4) })
# There is something going wrong here with the density plot and I just want something to show up so I can fix it to look how I want it to.
})
})
shinyApp(ui, server)
【问题讨论】:
标签: r ggplot2 shiny density-plot