2016-11-19 33 views
0

我使用虹膜数据,并希望在密度图中使用geom_vline为闪亮的Flex仪表板中的每个物种添加95%的数据线。但有错误信息。有没有人可以告诉代码中哪里不正确?由于下面的代码是: photo for error message添加分位线使用geom_vline闪亮有错误

--- 
title: "Untitled" 
output: 
flexdashboard::flex_dashboard: 
orientation: columns 
vertical_layout: fill 
runtime: shiny 
--- 

```{r setup, include=FALSE} 
library(flexdashboard) 
#initialize 
library(datasets) 
library(ggplot2) 
#helper function (convert vector to named list) 
namel<-function (vec){ 
tmp<-as.list(vec) 
names(tmp)<-as.character(unlist(vec)) 
tmp 
} 


``` 

Column 
===================================== 

```{r} 
pageWithSidebar(
# title 
headerPanel("Select Options"), 

#input 
sidebarPanel 
(
    selectInput("dataset","Data:", 
        list(iris = "iris") 
        ), 
    selectInput("variable","Variable:", "Loading..."), 
    selectInput("group","Group:", "Loading..."), 
    selectInput("plot.type","Plot Type:", 
        list(boxplot = "boxplot", density = "density") 
        ), 
    checkboxInput("show.points", "show points", TRUE) 
), 

# output     
mainPanel(
    h3(textOutput("caption")), 
    #h3(htmlOutput("caption")), 
    uiOutput("plot") # depends on input 
) 
) 
``` 


```{r} 

#update variable and group based on dataset 
observe({ 
    if (is.null(input$dataset)) 
     return() 
    obj<-switch(input$dataset, 
     "iris" = iris)  
    var.opts<-namel(colnames(obj)[1:4]) 
    var.opts2<-namel(colnames(iris)[5]) 
    updateSelectInput(session, "variable", choices = var.opts) 
    updateSelectInput(session, "group", choices = var.opts2) 
    }) 

output$caption<-renderText({ 
    switch(input$plot.type, 
     "boxplot" = "Boxplot", 

     "density" = "Density plot" 
    ) 
    }) 


output$plot <- renderUI({ 
    plotOutput("p") 
}) 

#plotting function using ggplot2 
output$p <- renderPlot({ 

variable <- get(input$dataset)[[input$variable]] 
group <- get(input$dataset)[[input$group]] 
if (is.null(variable) || is.null(group)) 
    return(NULL) 

plot.obj<<-list() # not sure why input$X can not be used directly? 
plot.obj$data<<-get(input$dataset) 
plot.obj$variable<<-with(plot.obj$data,get(input$variable)) 
plot.obj$group<<-with(plot.obj$data,get(input$group)) 

#dynamic plotting options 
plot.type<-switch(input$plot.type, 
     "boxplot" = geom_boxplot(), 
     "density" = geom_density(alpha=.75)) 
require(ggplot2) 

#plotting theme 
.theme<- theme(
      axis.line = element_line(colour = 'gray', size = .75), 
      panel.background = element_blank(), 
      plot.background = element_blank() 
      ) 

if(input$plot.type=="boxplot") {  #control for 1D or 2D graphs 
    p<-ggplot(plot.obj$data, 
      aes(
       x  = plot.obj$group, 
       y  = plot.obj$variable, 
       fill = as.factor(plot.obj$group) 
       ) 
      ) + plot.type 

      if(input$show.points==TRUE) 
      { 
       p<-p+ geom_point(color='black',alpha=0.5, position = 'jitter') 
      } 

    } else { 


    p<-ggplot(plot.obj$data, 
      aes(
       x  = plot.obj$variable, 
       fill = as.factor(plot.obj$group), 
       group = as.factor(plot.obj$group), 
       color = as.factor(plot.obj$group) 
       )  
      ) + plot.type+geom_vline(xintercept=quantile(plot.obj$data[which(plot.obj$group=='virginica'),],.95),colour="red")+geom_vline(xintercept=quantile(plot.obj$data[which(plot.obj$group=='versicolor'),],.95),colour="red")+ 
     geom_vline(xintercept=quantile(plot.obj$data[which(plot.obj$group=='setosa'),],.95),colour="red") 

    } 


p<-p+labs(
     fill = input$group, 
     x  = "", 
     y  = input$variable 
     ) + 
.theme 

print(p) 
}) 

```

回答

0

功能quantile需要一个载体,但你给一个数据帧。假设您想为Sepal.Length分配一个分位数。比添加一列名称$Sepal.Length

geom_vline(xintercept = quantile(unlist(plot.obj$data[which(plot.obj$group == 'virginica'),][input$variable]), 0.95),colour = "red") + 
geom_vline(xintercept = quantile(unlist(plot.obj$data[which(plot.obj$group == 'versicolor'),][input$variable]), 0.95),colour = "red") + 
geom_vline(xintercept = quantile(unlist(plot.obj$data[which(plot.obj$group == 'setosa'),][input$variable]), 0.95),colour = "red") 

我在你的评论后被eddited。