R Shiny:条件标签 geom_text

R Shiny: Conditional labeling with geom_text

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

如果薪水低,文本应显示在栏的顶部上方。如果薪水很高,它应该显示在条形图顶部下方(就像 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)

请尝试使用 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)

请检查 salary > 10000 的 if 语句。目前如果相等则为 -1.5。