如何在ggplot2中绘制5个坐标轴与9个数据系列?
问题:ggplot2实现5个坐标轴与9个数据系列的折线图绘制
需求:基于现有方案,在ggplot2中绘制5个坐标轴与9个数据系列,分组规则如下:
- 4个含“WT”的系列(WT0、WT90、WT180、WT270)共用一组轴
- 2个含“Zone”的系列(Zone0、Zone1)共用一组轴
- 剩余3个系列(OD、rpm、speed)各用一组轴,总计5个坐标轴
遇到的问题:
- 原本计划绘制折线图,但将p1至p5中的
geom_point替换为geom_line后无变化 - 尝试拆分为9个独立子图,轴混乱问题依旧存在
- 仅保留3个子图时能正常运行,添加第4个及以上子图后,坐标轴位置开始混乱
- 已知
plot_layout(widths = c(3, 1, 3, 1, 40))会导致图表被挤压,打算先解决轴对齐问题再处理缩放
附代码:
library(cowplot) library(patchwork) Process_Data <- avgext.df p1 <- ggplot(Process_Data, aes(length, OD)) + #here "1" is one geom_point() + theme(axis.line = element_line()) p2 <- ggplot(Process_Data, aes(length, WT0)) + geom_point() + geom_line(aes(x = length, y = WT90)) + geom_line(aes(x = length, y = WT180)) + geom_line(aes(x = length, y = WT270)) + theme(axis.line = element_line()) p3 <- ggplot(Process_Data, aes(length, Zone0)) + geom_point() + geom_line(aes(x = length, y = Zone1)) + theme(axis.line = element_line()) p4 <- ggplot(Process_Data, aes(length, rpm)) + geom_point() + theme(axis.line = element_line()) p5 <- ggplot(Process_Data, aes(length, speed)) + #geom_line(aes(color = "deeppink")) + geom_line(aes(x = length, y = OD), color = "black") + geom_line(aes(x = length, y = WT0), color = "red") + geom_line(aes(x = length, y = WT90), color = "blue") + geom_line(aes(x = length, y = WT180), color = "green") + geom_line(aes(x = length, y = WT270), color = "yellow") + geom_line(aes(x = length, y = Zone0), color = "gray") + geom_line(aes(x = length, y = Zone1), color = "beige") + geom_line(aes(x = length, y = rpm), color = "cyan") + theme(axis.line = element_line(), plot.margin = margin(10, 10, 10, 30)) wrap_elements(get_plot_component(p1, "ylab-l")) + wrap_elements(get_y_axis(p1)) + wrap_elements(get_plot_component(p2, "ylab-l")) + wrap_elements(get_y_axis(p2)) + wrap_elements(get_plot_component(p3, "ylab-l")) + wrap_elements(get_y_axis(p3)) + wrap_elements(get_plot_component(p4, "ylab-l")) + wrap_elements(get_y_axis(p4)) + p5 + plot_layout(widths = c(3, 1, 3, 1, 40))
数据:
dput(head(Process_Data,20)) structure(list(length = c(361.91, 362.05, 362.17, 362.3, 362.43, 362.55, 362.68, 362.8, 362.94, 363.07, 363.19, 363.32, 363.45, 363.57, 363.7, 363.83, 363.95, 364.08, 364.21, 364.33), OD = c(228.06, 227.85, 227.88, 228.28, 228.54, 228.82, 228.86, 228.77, 228.69, 228.9, 229.17, 228.88, 228.46, 228.27, 228.39, 228.58, 228.47, 228.29, 228, 227.8), WT0 = c(6.67, 6.49, 6.42, 6.6, 6.71, 6.61, 6.51, 6.5, 6.51, 6.67, 6.78, 6.73, 6.74, 6.51, 6.41, 6.49, 6.59, 6.56, 6.51, 6.48), WT90 = c(5.61, 5.44, 5.45, 5.55, 5.59, 5.58, 5.55, 5.45, 5.52, 5.54, 5.47, 5.4, 5.36, 5.4, 5.37, 5.39, 5.4, 5.41, 5.39, 5.31), WT180 = c(5.08, 5.1, 5.16, 5.26, 5.25, 5.23, 5.11, 5.08, 4.97, 5.01, 4.99, 4.97, 5.03, 5.13, 4.94, 5.02, 5.05, 5.08, 5.12, 5.18), WT270 = c(6.36, 6.19, 6.04, 6.17, 6.21, 6.28, 6.32, 6.39, 6.34, 6.14, 6.07, 6.31, 6.4, 6.42, 6.43, 6.35, 6.4, 6.54, 6.39, 6.3), Zone0 = c(265.25, 265.07, 265.42, 265.59, 265.59, 265.25, 265.94, 265.76, 266.11, 265.59, 265.94, 265.76, 265.94, 265.59, 265.76, 266.11, 265.76, 266.63, 266.28, 266.11), Zone1 = c(46.81, 41.69, 50.4, 45.27, 50.06, 45.45, 50.23, 48.35, 48.52, 54.67, 49.2, 54.33, 50.06, 53.82, 50.74, 60.82, 58.09, 53.99, 58.77, 57.41), rpm = c(40.24, 40.23, 40.26, 40.26, 40.26, 40.23, 40.22, 40.22, 40.26, 40.26, 40.26, 40.23, 40.23, 40.23, 40.23, 40.24, 40.24, 40.24, 40.26, 40.23), speed = c(1.508, 1.545, 1.537, 1.564, 1.554, 1.583, 1.577, 1.575, 1.568, 1.544, 1.537, 1.501, 1.489, 1.504, 1.536, 1.477, 1.522, 1.506, 1.473, 1.489)), row.names = c(NA, 20L), class = "data.frame")
内容的提问来源于stack exchange,提问作者Chris
相关产品推荐
相关产品推荐

