【问题标题】:stacked geom_bar in shiny that depends on select input取决于选择输入的闪亮堆叠geom_bar
【发布时间】:2019-12-05 03:12:17
【问题描述】:

我正在尝试在shiny 中包含一个堆叠条形图,该条形图取决于选择输入。它在shiny 之外工作正常,但在shiny 中它不显示多个条形图。

代码:

library(shiny)
library(ggplot2)
# Define UI  ----
ui <- fluidPage(

  # Application title
  titlePanel("Group fairness analysis"),

  # Sidebar 
  sidebarLayout(
    sidebarPanel(
      selectInput("group", "Group:", 
                  c("Age" = "age",
                    "Gender" = "gender",
                    "Region" = "region",
                    "Ethnicity"="ethnicity"))
      ),

    # Show a plot of the generated distribution
    mainPanel(
      h3("Accuracy bar chart"),
      plotOutput("accPlot")
    )
  )
)

# Define server logic ----
server <- function(input, output) {
  output$accPlot <- renderPlot({   
    g2 <- ggplot(df %>% count(get(input$group),correct) , aes(x=c(input$group),y=n,fill=as.factor(correct))) +
      geom_bar(stat="identity",position=position_fill())+
      scale_y_continuous(labels = scales::percent) +
      geom_text(aes(label = paste0((n/nrow(df))*100,"%")), position = position_fill(vjust = 0.5), size = 5)+
      theme_bw()+
      ylab("")+
      coord_flip()
    g2

  })

}

shinyApp(ui, server)

样本数据

# data -----------------------------------------------------------
n<-20 #number of users
threshold <- 60 #threshold in risk score for referral to YS
df <- data.frame(age = rep(0,n),
                 gender = rep(0,n),
                 ethnicity = rep(0,n),
                 region = rep(0,n),
                 score = rep(0,n),
                 referred = rep(0,n),
                 target = rep(0,n))

df$age <- as.factor(sample(c(15,16,17),size=n,replace=TRUE))
df$gender <- as.factor(sample(c('M','F'),size=n,replace=TRUE))
df$ethnicity<- as.factor(sample(c('European','Maori','Pacific','other'),size=n,replace=TRUE))
df$region<-as.factor(sample(c('North','Mid','South'),size=n,replace=TRUE))
df$score<-runif(n,min=0,max=100)
df$target<-sample(c(0,1),size=n,replace = TRUE)

df[which(df$score>=threshold),"referred"]<-1

df$colour<-rep(0,n)
df[which(df$referred==1 & df$target==1),"colour"]<-1
df[which(df$referred==1 & df$target==0),"colour"]<-2
df[which(df$referred==0 & df$target==1),"colour"]<-3
df[which(df$referred==0 & df$target==0),"colour"]<-4

df$correct<-rep(0,n)
df[which(df$referred==0 & df$target==0),"correct"]<-1
df[which(df$referred==1 & df$target==1),"correct"]<-1
df[which(df$referred==0 & df$target==1),"correct"]<-0
df[which(df$referred==1 & df$target==0),"correct"]<-0

应该是这样的

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:

    来自selectInput 的input$group 是一个字符串,而不是变量符号。您可以使用 rlang::sym 将其转换为 ggplot 的符号并使用 !! 进行评估。

    此外,您对 ggplot 的审美可以使用 aes_string 并将您的列名作为字符串引用。

    并将您的 correct 列单独转换为因子。

    df$correct <- as.factor(df$correct)
    
    ...
    
    g2 <- ggplot(df %>% count(!!rlang::sym(input$group), correct), aes_string(x=c(input$group), y="n", fill="correct")) + 
    
    ...
    

    【讨论】:

      猜你喜欢
      • 2020-03-28
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      • 2015-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多