如何用ggplot2调整双Y轴右侧刻度至0-60范围
双Y轴图表适配与右侧轴范围修正问题
我尝试在同一图表中对比两个温度相关变量:
- 最初生成Plot 01,代表第二个变量的绿色曲线无法完全显示在图表内
- 调整代码中的轴变换逻辑生成Plot 02后,右侧Y轴数值不准确,希望将右侧Y轴范围设置为
c(0, 60)并从0开始
解决方案
双Y轴的核心是建立左右轴的数值映射关系,让感知数据的[0,60]范围对应左侧温度轴的[21,31]显示区间:
- 转换感知数据到温度轴区间:使用公式
(tem.perception / 60) * 10 + 21,将0映射到21,60映射到31,确保曲线能在左侧轴的显示范围内完整呈现 - 反向生成右侧轴刻度:通过公式
(. - 21) * 6将左侧温度值还原为感知数据,保证右侧轴数值准确 - 替换轴范围控制方式:移除
coord_cartesian(ylim = c(21, 31)),改用scale_y_continuous的limits参数控制左侧轴范围,避免数据截断
修改后的完整代码
rm(list = ls()) # Resetear o limpiar todos los objetos del area de trabajo graphics.off() # Resetear o limpiar area de graficos # Load necessary libraries library(ggplot2) library(tidyr) # Data tem.perception <- c(55, 58, 57, 11, 0, 0, 0, 4, 15, 18, 22, 35) tem.inahmi <- read.table(header = TRUE, sep = ",", text = "year,ENE,FEB,MAR,ABR,MAY,JUN,JUL,AGO,SEP,OCT,NOV,DIC 1980,25.1,25.1,25.8,24.1,26.3,25.1,26.0,25.1,25.1,23.3,25.1,25.1 1981,27.3,28.3,27.5,27.3,25.3,23.7,22.5,22.7,25.5,25.5,25.5,25.2 1982,25.6,26.9,27.8,28.1,27.6,25.5,25.2,24.5,26.5,26.5,26.5,26.9 1983,26.3,27.3,27.2,26.7,27.0,26.3,25.5,26.3,24.9,25.4,26.3,26.8 1984,27.3,26.8,27.0,26.7,26.0,25.0,24.6,24.8,25.2,25.4,25.9,26.5 1985,27.3,27.1,27.0,26.7,26.0,25.0,24.6,24.8,25.2,25.4,25.9,26.5 1986,27.3,27.1,27.0,26.7,26.0,23.6,24.6,24.8,24.2,25.4,25.9,26.3 1987,27.3,27.1,27.0,26.7,26.0,25.0,24.6,24.8,25.2,25.4,25.9,26.5 1988,27.3,27.1,27.0,26.7,26.0,25.0,24.6,24.8,25.2,25.4,25.9,26.5 1989,27.3,27.1,27.0,26.7,26.0,25.0,24.6,24.8,25.2,25.4,25.9,26.5 1990,27.3,27.1,27.0,26.7,26.0,25.0,24.6,24.8,25.2,25.4,25.9,26.5 1991,26.3,27.8,27.6,27.8,27.7,25.6,26.3,23.6,24.8,25.4,25.8,27.3 1992,28.5,27.7,26.0,26.5,25.9,26.0,24.4,24.4,24.4,25.7,25.8,26.7 1993,27.6,27.0,26.8,26.3,24.3,24.2,26.0,26.0,26.0,26.0,26.0,26.0 1994,27.3,27.1,27.0,26.7,26.0,25.0,24.6,24.8,25.2,25.4,25.9,26.5 1995,28.3,26.2,28.1,27.7,27.0,26.2,24.1,24.5,25.1,25.6,26.1,25.9 1996,27.4,28.5,27.5,27.6,26.5,24.2,23.1,23.7,24.8,24.8,25.4,26.7 1997,27.4,28.2,28.0,27.3,26.9,26.1,27.0,27.2,28.0,27.1,27.4,27.6 1998,27.8,28.0,27.5,26.7,25.5,24.7,25.2,24.3,26.2,25.3,26.2,26.6 1999,27.5,26.5,26.6,26.2,25.8,25.8,23.6,25.8,24.5,25.8,26.0,25.8 2000,27.6,25.9,26.4,25.9,25.9,25.9,24.1,25.9,25.2,25.9,26.0,25.9 2001,27.1,27.1,27.0,26.7,26.0,25.0,24.6,24.6,25.0,25.4,25.9,27.2 2002,27.3,28.3,27.0,26.7,26.0,25.0,24.6,25.1,25.8,25.4,25.9,26.5 2003,27.7,27.1,28.2,26.7,26.0,25.0,24.6,24.8,25.2,25.4,25.9,26.5 2004,28.6,27.1,27.0,26.7,26.0,25.0,24.6,24.8,25.2,25.5,25.8,27.0 2005,28.3,27.3,26.8,27.4,27.0,26.1,24.7,24.6,24.4,24.7,25.7,26.5 2006,27.6,26.9,26.2,26.3,25.4,24.3,24.2,25.1,25.9,26.1,27.0,27.6 2007,28.1,28.8,28.3,27.0,26.7,24.9,25.0,24.3,25.0,24.7,26.1,26.4 2008,26.2,26.0,25.9,26.0,24.1,22.2,23.3,24.2,25.5,25.3,25.7,26.8 2009,26.7,26.0,26.1,25.9,25.3,24.3,24.7,24.9,25.4,25.9,25.8,26.6 2010,26.8,26.6,26.4,26.0,25.1,24.7,23.6,24.3,24.3,24.8,25.2,26.0 2011,27.6,27.1,27.8,27.2,25.8,25.2,24.1,24.5,25.2,24.7,26.1,26.2 2012,27.3,27.1,27.0,26.7,26.0,25.0,24.6,24.8,25.2,25.4,25.9,26.5 2013,25.2,25.2,25.2,26.4,25.3,24.5,24.3,24.6,24.7,25.2,25.1,26.7 " ) tem.inahmi$year <- as.factor(tem.inahmi$year) # Convert the 'year' column to a factor # see max and min max(tem.perception); min(tem.perception) max(tem.inahmi[,2:13]); min(tem.inahmi[,2:13]) # Melt the data into long format to have month, year, and value columns tem.inahmi_melted <- tem.inahmi %>% gather(key = "month", value = "value", -year) # Creating a month factor variable with custom levels for correct ordering tem.inahmi_melted$month <- factor(tem.inahmi_melted$month, levels = c("ENE", "FEB", "MAR", "ABR", "MAY", "JUN", "JUL", "AGO", "SEP", "OCT", "NOV", "DIC")) # Create a dataframe for the geom_line data tem.geom_line <- data.frame( month = factor(c("ENE", "FEB", "MAR", "ABR", "MAY", "JUN", "JUL", "AGO", "SEP", "OCT", "NOV", "DIC"), levels = c("ENE", "FEB", "MAR", "ABR", "MAY", "JUN", "JUL", "AGO", "SEP", "OCT", "NOV", "DIC")), value = (tem.perception / 60) * 10 + 21 # 将感知数据映射到温度轴区间 ) # Construct the basic ggplot2 tem.plot <- ggplot(tem.inahmi_melted, aes(x = month, y = value)) + geom_boxplot(aes(colour = "INAMHI"), fill = "NA") + # Boxplot with blue border geom_line(data = tem.geom_line, aes(y = value, colour = "Percepción"), group = 1, size = 0.8) + # Geom line in red scale_color_manual(name = "", values = c("INAMHI" = "#F8766D", "Percepción" = "#00BA38")) + # Assign colors scale_y_continuous( limits = c(21, 31), # 控制左侧轴范围 sec.axis = sec_axis(~(. - 21) * 6, name = "Percepción [Nro. respuestas]") # 反向生成右侧轴刻度 ) + theme_minimal() + xlab("Mes") + ylab("Temperatura [°C]")+ labs(title = "TEMPERATURE (Inahmi vs Perception)") + theme(legend.position="bottom", axis.title.y = element_text(color = "#F8766D"), # Y axis label color in blue axis.title.y.right = element_text(color = "#00BA38"), # Second Y axis label color in red axis.text.y = element_text(color = "#F8766D"), # Y axis text color in blue axis.text.y.right = element_text(color = "#00BA38")) print(tem.plot)
内容的提问来源于stack exchange,提问作者Darwin PC
相关产品推荐
相关产品推荐

