I'm trying to modify the first answer from the post here to plot five axes and nine data series using ggplot. Four of the nine series (with "WT") can be plotted together and two others (with "Zone") can be plotted together for a total of five axes needed. My code is below.
I'm trying to plot the data as lines but I have geom_point in the p1 to p5 lines because I wasn't sure if it had something to do with stacking the plots and axes. Anyhow, I tried it with geom_line and it didn't make a difference. I have also tried having nine separate px's with no real difference. It seems to sort work if I only have three px's as in the example but once I add a forth or more the axes start to move around the plot. I realize that the plot is squished because of the plot_layout(widths = c(3, 1, 3, 1, 40)) line. I also understand that the example used scaling which I will get to once I can get all the axes to line up.
Thanks!
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")
