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

R语言双轴堆叠柱形图+折线图绘制问题求助

问题解决:ggplot2双轴组合图折线不显示及负轴显示正值

问题描述

  1. 使用ggplot2绘制柱形图+折线图的双轴组合图表时,折线图完全未显示
  2. 为让负向情感柱形向下展示,已将对应count设为负值,但希望该轴刻度显示正值而非负值

数据与原代码

数据

df_graph <- structure(list(Year = c(2021, 2021, 2021, 2021, 2021, 2021, 2021, 
2021, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 
2022, 2022), Month = structure(c(9L, 9L, 10L, 10L, 11L, 11L, 
12L, 12L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L), levels = c("Jan", 
"Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", 
"Nov", "Dec"), class = c("ordered", "factor")), Sentiment = c("Negative", 
"Positive", "Negative", "Positive", "Negative", "Positive", "Negative", 
"Positive", "Negative", "Positive", "Negative", "Positive", "Negative", 
"Positive", "Negative", "Positive", "Negative", "Positive", "Negative", 
"Positive"), count = c(-35L, 86L, -86L, 80L, -55L, 65L, -80L, 
241L, -17L, 262L, -65L, 194L, -110L, 223L, -241L, 186L, -72L, 
166L, -262L, 117L), Compound_score = c(0.366274285714286, 0.512205813953488, 
0.4213, 0.5130075, 0.416335294117647, 0.582609230769231, 0.360905454545455, 
0.583192946058091, 0.346581944444444, 0.567261832061069, 0.3731, 
0.567573195876289, 0.400187272727273, 0.564790134529148, 0.280109803921569, 
0.558009677419355, 0.359274, 0.585918072289157, 0.372865625, 
0.565974358974359)), class = c("grouped_df", "tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -20L), groups = structure(list(
    Year = c(2021, 2021, 2021, 2021, 2022, 2022, 2022, 2022, 
    2022, 2022), Month = structure(c(9L, 10L, 11L, 12L, 1L, 2L, 
    3L, 4L, 5L, 6L), levels = c("Jan", "Feb", "Mar", "Apr", "May", 
    "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), class = c("ordered", 
    "factor")), .rows = structure(list(1:2, 3:4, 5:6, 7:8, 9:10, 
        11:12, 13:14, 15:16, 17:18, 19:20), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -10L), .drop = TRUE))

原代码

p <- ggplot(df_graph, aes(Month)) +
  geom_col(aes(y=count, color = Sentiment)) +
  geom_line(aes(y=Compound_score,group = Sentiment, color = Sentiment)) +
  scale_y_continuous(
    
    # Features of the first axis
    name = "Compound Score", limits = c(-500,500),
    
    # Add a second axis and specify its features
    sec.axis = sec_axis(trans=~./500, name="Count of Tweets")
  )

p

问题分析与解决方案

1. 折线图未显示的原因及解决

原因:Compound_score的数值范围仅为0.28~0.59,而y轴范围设置为c(-500,500),折线的y值相对于轴范围极小,几乎和x轴重合,因此无法显示。

解决方法:将Compound_score缩放至与count相近的数值范围,同时调整双轴的转换关系,确保折线落在可视区间内。

2. 负轴显示正值的解决

原因:直接修改count为负值会导致轴刻度显示负数,我们可以保留count的正负(保证柱形向下的方向),通过刻度标签转换,将负刻度显示为正值。

修正后的完整代码

library(ggplot2)

# 取消数据分组,避免绘图时的分组干扰
df_graph <- ungroup(df_graph)

p <- ggplot(df_graph, aes(Month)) +
  # 柱形图用fill映射颜色,确保柱形内部填充,position=dodge让正负情感柱形并排
  geom_col(aes(y = count, fill = Sentiment), position = "dodge") +
  # 折线图:将Compound_score缩放500倍,匹配count的数值范围,添加点增强可读性
  geom_line(aes(y = Compound_score * 500, group = Sentiment, color = Sentiment), 
            linewidth = 1.2) +
  geom_point(aes(y = Compound_score * 500, color = Sentiment)) +
  scale_y_continuous(
    name = "Tweet Count", # 主轴对应count
    limits = c(-300, 300), # 调整范围适配数据极值
    # 将负刻度转换为正值显示,柱形方向保持不变
    labels = function(x) ifelse(x < 0, abs(x), x),
    # 次轴对应Compound_score,转换函数还原缩放比例
    sec.axis = sec_axis(trans = ~./500, name = "Compound Score")
  ) +
  # 自定义颜色,区分正负情感
  scale_fill_manual(values = c("Negative" = "#E63946", "Positive" = "#457B9D")) +
  scale_color_manual(values = c("Negative" = "#1D3557", "Positive" = "#1D3557")) +
  labs(title = "推文情感数量与复合得分月度趋势", x = "月份") +
  theme_minimal() +
  theme(
    axis.title.y.right = element_text(color = "#1D3557"),
    axis.title.y = element_text(color = "#E63946"),
    plot.title = element_text(hjust = 0.5)
  )

p

代码说明

  • 取消分组:原数据为分组数据,取消分组避免自动分组对绘图的干扰
  • 柱形图优化:用fill映射颜色替代color,让柱形内部有填充色;position=dodge让正负情感的柱形并排显示
  • 折线图缩放:将Compound_score乘以500,使其数值范围与count(-262~262)匹配,确保折线在可视区间内
  • 负轴标签转换:通过labels参数将负刻度转换为正值显示,同时保留柱形向下的视觉效果
  • 双轴匹配:次轴用~./500将缩放后的数值还原为原始Compound_score范围
  • 样式优化:自定义颜色、添加数据点、调整主题,提升图表可读性与美观度

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 08:20:27