R语言ggplot2绘制双Y轴图表遇轴刻度问题求助
双Y轴图表轴刻度不匹配问题修复
我需要绘制一张目标图样式的双Y轴图表,但目前轴刻度显示异常,多次修改代码后仍不符合预期。以下是我的数据和代码,请帮忙解决刻度不匹配的问题:
# Datos Clorofila-Radiación horario <- c("D1-6:30", "D1-12:00", "D1-18:00", "D2-6:30", "D2-12:00", "D2-18:00", "D3- 6:30", "D3-12:00", "D3-18:00") trh3 <- c(1.85, 1.21, 2.23, 1.50, 1.11, 1.86, 1.74, 1.2, 1.63) trh5 <- c(1.99, 1.50, 1.98, 1.90, 1.14, 2.01, 1.59, 1.04, 1.53) rad <- c(22.53, 1909.10, 30.32, 22.32, 2342.10, 12.65, 28.70, 1279.8, 28.68) # Crear un data frame datos <- data.frame(horario, trh3, trh5, rad) # Ordenar los datos por el orden deseado en el eje x datos$horario <- factor(datos$horario, levels = c("D1-6:30","D1-12:00","D1-18:00","D2-6:30","D2-12:00","D2-18:00","D3-6:30","D3-12:00","D3-18:00")) # Gráfico ggplot(datos, aes(x = horario)) + geom_line(aes(y = trh3, color = "trh3", group = 1)) + geom_line(aes(y = trh5, color = "trh5", group = 1)) + geom_line(aes(y = rad, color = "rad", group = 1), linetype = "dashed") + scale_y_continuous( name = "Clorofila (µg/ml)", breaks = seq(1, 2.5, by = 0.2), labels = seq(1, 2.5, by = 0.2)) + scale_y_continuous( sec.axis = sec_axis(~./1000, name = "Radiación solar (W/m²)", breaks = seq(0, 2500, by = 500)/1000), name = "Radiación solar (W/m²)", breaks = seq(0, 2500, by = 500), labels = seq(0, 2500, by = 500)) + scale_color_manual( values = c("trh3" = "blue", "trh5" = "green", "rad" = "red"), labels = c("trh3 (Clorofila)" = "trh3", "trh5 (Clorofila)" = "trh5", "Radiación solar")) + labs(x = "Semana de medición", y = "Parámetros", color = "Variable") + theme_minimal() + theme(legend.position = "bottom")
问题根源分析
你的代码存在3个核心问题导致刻度异常:
- 重复调用
scale_y_continuous,第二个调用会完全覆盖第一个的左轴设置,导致叶绿素的刻度失效 - 辐射数据
rad的数值范围(20-2300)远大于叶绿素数据(1-2.5),直接绘制会压缩叶绿素线条,双Y轴的核心逻辑是先将右轴数据转换到左轴范围,再通过反向转换显示原始刻度 scale_color_manual的标签映射搞反了,键值对位置错误
修正后的完整代码
# Datos Clorofila-Radiación horario <- c("D1-6:30", "D1-12:00", "D1-18:00", "D2-6:30", "D2-12:00", "D2-18:00", "D3-6:30", "D3-12:00", "D3-18:00") trh3 <- c(1.85, 1.21, 2.23, 1.50, 1.11, 1.86, 1.74, 1.2, 1.63) trh5 <- c(1.99, 1.50, 1.98, 1.90, 1.14, 2.01, 1.59, 1.04, 1.53) rad <- c(22.53, 1909.10, 30.32, 22.32, 2342.10, 12.65, 28.70, 1279.8, 28.68) # Crear un data frame datos <- data.frame(horario, trh3, trh5, rad) # Ordenar los datos por el orden deseado en el eje x datos$horario <- factor(datos$horario, levels = c("D1-6:30","D1-12:00","D1-18:00","D2-6:30","D2-12:00","D2-18:00","D3-6:30","D3-12:00","D3-18:00")) # Gráfico修正版 ggplot(datos, aes(x = horario)) + # 叶绿素数据直接使用原始值 geom_line(aes(y = trh3, color = "trh3", group = 1)) + geom_line(aes(y = trh5, color = "trh5", group = 1)) + # 将辐射数据转换为左轴匹配范围(除以1000) geom_line(aes(y = rad/1000, color = "rad", group = 1), linetype = "dashed") + # 统一设置Y轴:左轴为叶绿素,右轴通过反向转换还原辐射刻度 scale_y_continuous( name = "Clorofila (µg/ml)", breaks = seq(1, 2.5, by = 0.2), labels = seq(1, 2.5, by = 0.2), sec.axis = sec_axis(~.*1000, name = "Radiación solar (W/m²)", breaks = seq(0, 2500, by = 500)) ) + # 修正颜色标签映射 scale_color_manual( values = c("trh3" = "blue", "trh5" = "green", "rad" = "red"), labels = c("trh3 (Clorofila)", "trh5 (Clorofila)", "Radiación solar") ) + labs(x = "Semana de medición", color = "Variable") + theme_minimal() + theme(legend.position = "bottom")
关键修改说明
- 合并Y轴设置:只调用一次
scale_y_continuous,避免左轴设置被覆盖,同时在同一个函数内定义右轴的反向转换规则 - 数据范围对齐:将
rad除以1000,让其数值范围(0.01-2.3)与叶绿素数据(1-2.5)匹配,保证三条曲线都能正常显示 - 右轴刻度还原:
sec_axis(~.*1000)将左轴刻度值乘以1000,还原辐射数据的原始单位(W/m²),同时设置符合需求的刻度断点 - 修正颜色标签:调整
scale_color_manual的labels参数,让标签文本与颜色值正确对应
内容的提问来源于stack exchange,提问作者INGRID MENDOZA
相关产品推荐
相关产品推荐

