繁体   English   中英

在闪亮的错误中使用geom_vline添加分位数线

[英]add quantile line using geom_vline in shiny got errors

我使用虹膜数据,并希望使用geom_vline为闪亮的Flex仪表板中的每个Specie添加密度图中95%的数据线。 但收到错误消息。 有人能说出目的代码在哪里不正确吗? 感谢下面的代码: 错误消息的照片

---
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)
})  

```

函数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")

在您发表评论后,我怀念。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM