【问题标题】:position dodge and geom_text with faceting and positive/negative numbersposition dodge 和 geom_text with faceting 和 positive/negative numbers
【发布时间】:2023-02-01 20:21:27
【问题描述】:

我需要在条形的顶部/底部放置标签,因为我的数字既有正数也有负数(= x 轴的两侧)。

到目前为止,我一直在尝试使用 vjust/hjust 但没有成功......

total_merged%>%
      ggplot(aes(factor(NAPLAN_YEAR), value_add_sector_total, fill = model,
                 label = value_add_sector_total
               )) +
      geom_col(position = position_dodge(width = 0.75), width = 0.5) +
      facet_grid(.~sector_report, switch = 'x') +
      scale_fill_manual(NULL, values = c('#1e3763', '#bebebe'), labels = c("Base", "MLSH")) +
      theme_minimal() +
      geom_text(position = position_dodge(width = 0.75), hjust=0.5, vjust = 1.7, #angle = 90, 
                color="darkred")+
          
      labs(title="",
           x ="", y="System 'value add'")+
      theme(legend.position = 'bottom',
          aspect.ratio = 4/3,
            strip.placement = 'outside',
            panel.spacing.x = unit(0, 'mm'),
            axis.title.x = element_blank(),
            panel.grid.major.x = element_blank(),
            panel.grid.minor.y = element_blank(),
            strip.text = element_text(face = 2),
            legend.text=element_text(size=9),
            legend.title=element_text(size=9),
            axis.text= element_text(size = 9, colour = "black")
      )

我的数据如下所示(前 50 个):

structure(list(value_add_sector_total = c(-1.38, -1.38, -1.38, 
-1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, 
-1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, 
-1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, 
-1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, 
-1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, 
-1.38, -1.38), NAPLAN_YEAR = c(2011, 2011, 2011, 2011, 2011, 
2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 
2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 
2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 
2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 
2011), model = c("base", "base", "base", "base", "base", "base", 
"base", "base", "base", "base", "base", "base", "base", "base", 
"base", "base", "base", "base", "base", "base", "base", "base", 
"base", "base", "base", "base", "base", "base", "base", "base", 
"base", "base", "base", "base", "base", "base", "base", "base", 
"base", "base", "base", "base", "base", "base", "base", "base", 
"base", "base", "base", "base")), row.names = c(NA, -50L), class = c("tbl_df", 
"tbl", "data.frame"))

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    你可以在aes()里面使用vjust()

    data.frame(V1 = c(20,-30,-40,50), V2 = letters[1:4], V3 = c("A","A","B","B")) |> 
      ggplot(aes(V2, V1, fill = V3)) + 
      geom_col() +
      geom_text(aes(label = V1, vjust = ifelse(V1 > 0, 0, 1)) )
    

    【讨论】:

      猜你喜欢
      • 2018-12-15
      • 2019-09-19
      • 1970-01-01
      • 2012-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-16
      • 2022-01-19
      相关资源
      最近更新 更多