You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言绘图:移除无对比的study_year标签并将其余标签右对齐轴

问题需求

我用R语言绘制了一幅柱状图,每个柱子展示不同周期间的数值变化百分比。现在需要完成两个调整:

  • 移除study_year对应的标签(该周期无可对比对象)
  • 将其余标签放置在图表右侧,与坐标轴对齐

感谢@Rui Barradas此前对图表制作的帮助。

数据集

Name | Active_period| site |Time | count
A | last_year| north| mornings |10
A | last_year| south| mornings |20
A | last_year| north| evenings|45
A | last_year| south| evenings|35
A | this_year| north| mornings |80
A | this_year| south| mornings |60
A | this_year| north| evenings|95
A | this_year| south| evenings|120
A | study_year| north| mornings |100
A | study_year| south| mornings |400
A | study_year| north| evenings|220
A | study_year| south| evenings|32
B | last_year| north| mornings |10
B | last_year| south| mornings |45
B | last_year| north| evenings|25
B | last_year| south| evenings|20
B | this_year| north| mornings |300
B | this_year| south| mornings |250
B | this_year| north| evenings|140
B | this_year| south| evenings|20
B | study_year| north| mornings |10
B | study_year| south| mornings |20
B | study_year| north| evenings|10
B | study_year| south| evenings|20

当前绘图代码

theme_my <- function(){ 
  theme_grey(base_size = 10) %+replace%
    theme(
      legend.position = "bottom", 
      axis.text = element_text(size = 11), 
      axis.title = element_text(size = 11, face = "bold"),
      strip.text.x = element_text(size = 12),
      strip.text.y = element_text(size = 12, angle = -90),
      plot.title = element_text(size = 14, face = "bold"),
      plot.subtitle = element_text(size = 13)
    )
}


suppressPackageStartupMessages({
  library(dplyr)
  library(ggplot2)
})
    
data %>%
  mutate(Active_period = factor(Active_period,levels = c("study_year","last_year", "this_year"))) %>%
  group_by(Name, site, Time) %>%
  arrange(Active_period) %>%
  mutate(perc = scales::percent(c(0, diff(count))/dplyr::lag(count, default  = 1))) %>%
  ungroup() %>%
  mutate(Active_period = forcats::fct_rev(Active_period)) %>%
  ggplot(aes(Name, count, fill = Active_period)) +
  geom_col(
    width = 0.8, 
    position = position_dodge2(width = 0.8, preserve = "single")
  ) +
  geom_text(
    position = position_dodge2(width = 0.8, preserve = "single"),
    aes(label = perc, group = Active_period),
    hjust = -0.2
  ) +
  scale_fill_manual(
    name = " ", 
    values = c(study_year ="#5d5c5c", this_year = "#4475aa", last_year = "#333239"),
    limits = c("study_year","last_year", "this_year")
  ) +
  coord_flip() +
  facet_grid(Time ~ site) +
  theme_my()

当前图表效果

当前柱状图效果

解决方案

要实现需求,对代码做两处关键修改即可,完整代码如下:

theme_my <- function(){ 
  theme_grey(base_size = 10) %+replace%
    theme(
      legend.position = "bottom", 
      axis.text = element_text(size = 11), 
      axis.title = element_text(size = 11, face = "bold"),
      strip.text.x = element_text(size = 12),
      strip.text.y = element_text(size = 12, angle = -90),
      plot.title = element_text(size = 14, face = "bold"),
      plot.subtitle = element_text(size = 13)
    )
}


suppressPackageStartupMessages({
  library(dplyr)
  library(ggplot2)
})
    
data %>%
  mutate(Active_period = factor(Active_period,levels = c("study_year","last_year", "this_year"))) %>%
  group_by(Name, site, Time) %>%
  arrange(Active_period) %>%
  mutate(perc = scales::percent(c(0, diff(count))/dplyr::lag(count, default  = 1))) %>%
  # 将study_year对应的百分比设为NA,自动隐藏标签
  mutate(perc = ifelse(Active_period == "study_year", NA, perc)) %>%
  ungroup() %>%
  mutate(Active_period = forcats::fct_rev(Active_period)) %>%
  ggplot(aes(Name, count, fill = Active_period)) +
  geom_col(
    width = 0.8, 
    position = position_dodge2(width = 0.8, preserve = "single")
  ) +
  geom_text(
    position = position_dodge2(width = 0.8, preserve = "single"),
    aes(label = perc, group = Active_period),
    # 因coord_flip反转坐标轴,hjust=1实现右对齐,vjust=0.5保证垂直居中
    hjust = 1,
    vjust = 0.5,
    # 微调标签与柱子的间距
    nudge_x = -0.1
  ) +
  scale_fill_manual(
    name = " ", 
    values = c(study_year ="#5d5c5c", this_year = "#4475aa", last_year = "#333239"),
    limits = c("study_year","last_year", "this_year")
  ) +
  coord_flip() +
  facet_grid(Time ~ site) +
  theme_my()

修改说明

  1. 移除study_year标签:通过ifelse把study_year对应的perc值设为NA,geom_text默认不会渲染缺失值的标签,直接实现隐藏效果。
  2. 标签右对齐:由于使用了coord_flip(),坐标轴方向反转,设置hjust=1让标签紧贴柱子右侧对齐,vjust=0.5保证标签在柱子垂直中心位置,nudge_x=-0.1微调标签和柱子的距离,确保和坐标轴对齐更整齐。

内容的提问来源于stack exchange,提问作者GeoBeez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 12:05:11