You need to enable JavaScript to run this app. R语言ggplot2调整x轴标签文本、格式与方向及代码优化问询
优惠活动
大模型
产品
解决方案
定价
更多

R语言ggplot2调整x轴标签文本、格式与方向及代码优化问询

R绘图优化方案(tidyverse + cowplot)

待实现需求

  • 恢复x轴标签的空格分隔格式
  • 移除x轴标签中的"behavior"冗余字段
  • 旋转x轴标签为垂直方向,避免标签重叠
  • 简化现有冗余的代码实现逻辑

优化后完整代码

library(tidyverse)
library(cowplot)

# 原始数据结构保持不变
df <- structure(list(Percent_HL_BehaviorChAfterSoundStart = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140),
                     Percent_NH_BehaviorChAfterSoundStart = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140),
                     Percent_HL_CLAfterSoundStop = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140),
                     Percent_NH_BehaviorChAfterSoundStop = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140),
                     Percent_HL_BehaviorChBeforeSoundStart = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140),
                     Percent_NH_BehaviorChBeforeSoundStart = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140),
                     Percent_HL_BehaviorChAfterMMStartGesticulate = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140),
                     Percent_NH_BehaviorChAfterPStartGesticulate = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140),
                     Percent_HL_BehaviorChAfterPStartExtension = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140),
                     Percent_NH_BehaviorChAfterPStartExtension = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140),
                     Percent_HL_BehaviorChAfterPStartOther = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140),
                     Percent_NH_BehaviorChAfterPStartOther = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140),
                     Percent_HL_BehaviorChAfterPStopGesture = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140),
                     Percent_NH_BehaviorChAfterPStopGesticulate = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140),
                     Percent_HL_BehaviorChAfterMMStopExtension = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140),
                     Percent_NH_BehaviorChAfterPStopExtension = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140),
                     Percent_HL_BehaviorChAfterPStopOther = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140),
                     Percent_NH_BehaviorChAfterPStopOther = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140),
                     Percent_HL_BehaviorChBeforeMMStartGesticulate = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140),
                     Percent_NH_BehaviorChBeforePStartGesticulate = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140),
                     Percent_HL_BehaviorChBeforeMMStartExtension = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140),
                     Percent_NH_BehaviorChBeforePStartExtension = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140),
                     Percent_HL_BehaviorChBeforePStartOther = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140),
                     Percent_NH_BehaviorChBeforePStartOther = c(0.970,0.870,0.830,0.670,0.360,0.000,0.370,0.710,0.140)),
                class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, -9L))

# 精简后的数据处理逻辑
df_Percent <- df %>% 
  pivot_longer(cols = starts_with("Percent_"),
               names_to = "names",
               values_to = "values") %>% 
  separate(names, c("prefix", "Group", "Behavior"), sep = "_") %>% 
  # 一步完成标签清理:移除Behavior字段+插入空格
  mutate(Behavior = str_remove(Behavior, "BehaviorCh") %>% 
           str_replace_all("([a-z])([A-Z])", "\\1 \\2"))

# 绘图代码
plot_Percent <- ggplot(df_Percent, aes(x = fct_inorder(Behavior), y = values, color = Group)) +
  geom_point(position = position_dodge(width = 0.3), size = 1) + 
  scale_y_continuous(limits = c(0, 1)) +
  labs(x = "Behavior", y = "Value") +
  theme_bw() +
  # 实现x轴标签垂直旋转
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))

plot_grid(plot_Percent, labels = "AUTO")

关键优化点说明

  • 数据重构阶段用starts_with("Percent_")匹配目标列,替代原写死的列序号规则,避免后续新增列时出现数据遗漏
  • 标签处理一步完成:先移除BehaviorCh冗余字段,再通过正则在大小写字母交界处插入空格,自动生成带空格的可读标签
  • 新增theme配置项axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),实现x轴标签垂直旋转,同时调整对齐参数避免标签偏移出绘图区域
  • 移除geom_point中冗余的stat="identity"参数,该参数为柱状图专用配置,散点图无需设置

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.08 10:57:02