审美长度问题 geom_line 闪亮
problems with aesthetic length geom_line shiny
我有您可以生成的数据:
set.seed(3)
dat.0<-as.data.frame(expand.grid(c("MATH","CHEM","BIOL"),c("Spring","Summer","Fall"),c(2016,2017,2018),0:17))
names(dat.0)<-c("SUBJ","SEM","YEAR","WEEK")
dat.0$MEET<-floor(runif(486,0,150))
我正在尝试制作一个闪亮的应用程序,它将显示 "MEET" 系列的线图,x 轴上的 "WEEK" 由 "SUBJ" 字段着色。
除了闪亮之外情节看起来还不错:
dat.0%>%
ggplot(aes(x = WEEK,y = MEET,colour = SUBJ))+
geom_point()+
geom_path()+
facet_grid(YEAR~SEM)
但是当我使用 shiny 创建应用程序时,如果我尝试在绘图上绘制多个系列,它会报错。更重要的是,如果您在应用程序中单独绘制每个系列,您会发现无论您 "SUBJ" select,它都会打印相同的图:
choices = levels(as.factor(dat.0$SUBJ))
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("STEM Tutoring App"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("SUBJ",
"Course Subject:",
choices = choices,
multiple = T,
selectize = T)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("linePlot")
)
)
)
server <- function(input, output) {
output$linePlot <- renderPlot({
req(input$SUBJ)
dat.0%>%
ggplot(aes(x = WEEK,y = MEET,colour = input$SUBJ))+
geom_point()+
geom_path()+
facet_grid(YEAR~SEM)})}
# Run the application
shinyApp(ui = ui, server = server)
我知道这就快完成了,我只是不知道如何使它看起来不错并按您期望的方式运行。
这个技巧,正如用户@aosmith 在评论中解释的那样,您需要通过在侧边栏后的 renderPlot 函数中定义子集参数来使数据集对用户输入做出反应,因此您可以将
server <- function(input,output) {
output$linePlot<-renderPlot({
req(input$SUBJ)
plot.data <- dat.0 %>%
dplyr::filter(SUBJ %in% input$SUBJ)
plot.data %>%
ggplot(aes(x = WEEK, y = MEET, colour = SUBJ)) +
geom_point() +
geom_path() +
facet_grid(YEAR~SEM)
})
}
基于用户输入的子集是我感到困惑的地方。不过问题解决了。谢谢你的帮助!
我有您可以生成的数据:
set.seed(3)
dat.0<-as.data.frame(expand.grid(c("MATH","CHEM","BIOL"),c("Spring","Summer","Fall"),c(2016,2017,2018),0:17))
names(dat.0)<-c("SUBJ","SEM","YEAR","WEEK")
dat.0$MEET<-floor(runif(486,0,150))
我正在尝试制作一个闪亮的应用程序,它将显示 "MEET" 系列的线图,x 轴上的 "WEEK" 由 "SUBJ" 字段着色。
除了闪亮之外情节看起来还不错:
dat.0%>%
ggplot(aes(x = WEEK,y = MEET,colour = SUBJ))+
geom_point()+
geom_path()+
facet_grid(YEAR~SEM)
但是当我使用 shiny 创建应用程序时,如果我尝试在绘图上绘制多个系列,它会报错。更重要的是,如果您在应用程序中单独绘制每个系列,您会发现无论您 "SUBJ" select,它都会打印相同的图:
choices = levels(as.factor(dat.0$SUBJ))
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("STEM Tutoring App"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("SUBJ",
"Course Subject:",
choices = choices,
multiple = T,
selectize = T)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("linePlot")
)
)
)
server <- function(input, output) {
output$linePlot <- renderPlot({
req(input$SUBJ)
dat.0%>%
ggplot(aes(x = WEEK,y = MEET,colour = input$SUBJ))+
geom_point()+
geom_path()+
facet_grid(YEAR~SEM)})}
# Run the application
shinyApp(ui = ui, server = server)
我知道这就快完成了,我只是不知道如何使它看起来不错并按您期望的方式运行。
这个技巧,正如用户@aosmith 在评论中解释的那样,您需要通过在侧边栏后的 renderPlot 函数中定义子集参数来使数据集对用户输入做出反应,因此您可以将
server <- function(input,output) {
output$linePlot<-renderPlot({
req(input$SUBJ)
plot.data <- dat.0 %>%
dplyr::filter(SUBJ %in% input$SUBJ)
plot.data %>%
ggplot(aes(x = WEEK, y = MEET, colour = SUBJ)) +
geom_point() +
geom_path() +
facet_grid(YEAR~SEM)
})
}
基于用户输入的子集是我感到困惑的地方。不过问题解决了。谢谢你的帮助!