我使用虹膜数据,并希望在闪亮的flex仪表板中使用geom_vline为每个Specie添加密度图中95%的数据线。但得到了错误信息。有没有人可以告诉代码中哪里不正确?谢谢以下是代码: photo for error message
---
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 :(得分: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")
我评论后你很喜欢。