【问题标题】:ggplot show the value rounded to one decimal place above the bar chartggplot 在条形图上方显示四舍五入到小数点后一位的值
【发布时间】:2022-07-07 20:09:23
【问题描述】:

我想将条形图上方的值四舍五入到小数点后一位。例如 14 应该是 1.5 而不是 1.52977。

This is how the bar chart looks right now

这是我的代码:

CPUE1 <- CPUE
CPUE1$Strecke <- factor (CPUE1$Strecke, levels = c('30/31', '14', '12', '10','1c','1bc', '1b'))

ggplot(CPUE1, aes(x= Strecke, y= CPUE, fill = Strecke ))+
  geom_bar(stat='identity', position = 'dodge')+
  theme_minimal()+
  geom_text (aes (label = CPUE), position=position_dodge(width=0.9), vjust=-0.25)+
  scale_fill_manual (values = 
                       c("12" = "green", "10"= "green",
                         "1c" = "green", "14"= "red",
                         "1b"= "red","1bc"= "red","30/31" = "red"))

【问题讨论】:

    标签: ggplot2 bar-chart rounding


    【解决方案1】:

    round 添加到您的geom_text

    library(tidyverse)
    
    tribble(
      ~Strecke, ~CPUE,
      "1b", 1.333,
      "1c", 1.222,
      "1b", 2.666,
      "1c", 2.777
    ) |>
      mutate(Strecke = factor(Strecke)) |>
      ggplot(aes(Strecke, CPUE, fill = Strecke)) +
      geom_bar(stat = "identity", position = "dodge") +
      theme_minimal() +
      geom_text(aes(label = round(CPUE, 1)), position = position_dodge(width = 0.9), vjust = -0.25) +
      scale_fill_manual(values = c("1b" = "red", "1c" = "blue"))
    

    reprex package (v2.0.1) 于 2022-07-07 创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-12
      相关资源
      最近更新 更多