【发布时间】: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