【问题标题】:R Shiny: Conditional labeling with geom_textR Shiny:使用 geom_text 进行条件标记
【发布时间】:2019-12-10 11:33:03
【问题描述】:

在下面的示例中,我想使用条件标签。

如果薪水较低,则文本应显示在条形顶部上方。如果薪水很高,它应该显示在栏的顶部下方(就像它已经使用 geom_text(aes(label=text), vjust=-0.25))。条件本身运作良好。但是:注释应显示为字符串,即“low”和“high”。但就我而言,有“1”和“2”。我不知道这些数字是从哪里来的。是否有可能将文本显示为字符串,就像它包含在数据框中一样?

library(ggplot2)

ui <- fluidPage(

  titlePanel("conditional label"),
  sidebarLayout(
    sidebarPanel(),
    mainPanel(plotOutput(outputId = "distPlot")
    )
  )
)

server <- function(input, output) {

  output$distPlot <- renderPlot({

    employee <- c('John Doe','Peter Gynn','Jolie Hope')
    salary <- c(5000, 9000, 12000)
    text <- c('low', 'low', 'high')
    df <- data.frame(employee, salary, text)

    plot <- ggplot(data=df, aes(x=employee, y=salary), color="black") +
      geom_bar(color="black", stat="identity", show.legend = FALSE) +
      scale_y_continuous(limits=c(0,13000)) +
      geom_text(aes(label=text), vjust=-0.25) +
      geom_text(aes(label = ifelse(salary > 10000, text, "")), vjust=1.5) +
      geom_text(aes(label = ifelse(salary < 10000, text, "")), vjust=-1.5)

    print(plot)


  })

}

shinyApp(ui, server)

【问题讨论】:

    标签: r if-statement shiny label geom-text


    【解决方案1】:

    请尝试使用dplyr::if_else()NULL 而不是""

    library(shiny)
    library(ggplot2)
    library(dplyr)
    
    ui <- fluidPage(
    
        titlePanel("conditional label"),
        sidebarLayout(
            sidebarPanel(),
            mainPanel(plotOutput(outputId = "distPlot")
            )
        )
    )
    
    server <- function(input, output) {
    
        output$distPlot <- renderPlot({
    
            employee <- c('John Doe','Peter Gynn','Jolie Hope')
            salary <- c(5000, 9000, 12000)
            text <- c('low', 'low', 'high')
            df <- data.frame(employee, salary, text)
    
            plot <- ggplot(data=df, aes(x=employee, y=salary), color="black") +
                geom_bar(color="black", stat="identity", show.legend = FALSE) +
                scale_y_continuous(limits=c(0,13000)) +
                geom_text(aes(label=text), vjust=-0.25) +
                geom_text(aes(label = if_else(salary > 10000, text, NULL)), vjust=1.5) +
                geom_text(aes(label = if_else(salary < 10000, text, NULL)), vjust=-1.5)
    
            print(plot)
    
    
        })
    
    }
    
    shinyApp(ui, server)
    

    更新

    使用ifelse()查看改进的答案:

    library(shiny)
    library(ggplot2)
    
    ui <- fluidPage(
    
        titlePanel("conditional label"),
        sidebarLayout(
            sidebarPanel(),
            mainPanel(plotOutput(outputId = "distPlot")
            )
        )
    )
    
    server <- function(input, output) {
    
        output$distPlot <- renderPlot({
    
            employee <- c('John Doe','Peter Gynn','Jolie Hope')
            salary <- c(5000, 9000, 12000)
            text <- c('low', 'low', 'high')
            df <- data.frame(employee, salary, text)
    
            plot <- ggplot(data=df, aes(x=employee, y=salary), color="black") +
                geom_bar(color="black", stat="identity", show.legend = FALSE) +
                scale_y_continuous(limits=c(0,13000)) +
                geom_text(aes(label=text), vjust=-0.25) +
                geom_text(aes(label = text, vjust=ifelse(salary > 10000, 1.5, -1.5)))
    
            print(plot)
    
    
        })
    
    }
    
    shinyApp(ui, server)
    

    请检查工资> 10000的if语句。当前如果相等,则为-1.5。

    【讨论】:

    • 效果很好。但是如何防止“警告:删除包含缺失值的 2 行(geom_text)”,这是以前没有的?
    • 请参阅上面的更新答案。请检查这是否适用于您的用例。文字没有改变,所以我把 ifelse 放在 vjust 上。
    • 我认为更新的解决方案在效率和可读性方面更好。谢谢!
    • 同意,最初我只是回答了您的问题,但现在我从整体上重新审视了您的实际用例。
    猜你喜欢
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多