ggplot line plots are not showing up on proportional secondary axis

I am trying to plot a barplot, and overlay some plots onto it using a secondary axis. I am able to adjust the secondary axis to be proportional to the values but the line plots still seem be squished at the bottom.

Here is my code and data:

> dput(dsr1)
structure(list(Site = c("DF", "DF", "DF", "DF", "DR", "DR", "DR", 
"DR", "FF", "FF", "FF", "FF", "FR", "FR", "FR", "FR", "MR", "MR", 
"MR", "MR"), Gene = c("dsr", "dsr", "dsr", "dsr", "dsr", "dsr", 
"dsr", "dsr", "dsr", "dsr", "dsr", "dsr", "dsr", "dsr", "dsr", 
"dsr", "dsr", "dsr", "dsr", "dsr"), Var = c("Sep", "Nov", "Dec", 
"Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", 
"Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan"), Avg = c(26192.15213, 
1060.62995, 5976.49445, 18627.35088, 9230.00425, 8154.795375, 
15599.3215, 33674.06525, 5375.773538, 1243.98395, 1368.094813, 
18126.26463, 11128.72975, 3512.390263, 15628.89188, 12514.475, 
10136.56213, 10330.751, 15907.58675, 21435.79538), Std = c(1971.846094, 
80.20201232, 1492.776454, 1716.348126, 1312.541508, 682.2806779, 
3751.417264, 1518.661188, 1035.91546, 169.1677183, 68.78619696, 
1653.659567, 3832.856982, 218.6107193, 650.6329325, 3516.620678, 
2528.207035, 451.3704901, 907.4479828, 550.5151197)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

> dput(srb1)
structure(list(Site = c("DF", "DF", "DF", "DF", "DF", "DF", "DF", 
"DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", 
"DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", 
"DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", 
"DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DR", "DR", "DR", 
"DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", 
"DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", 
"DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", 
"DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", 
"DR", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", 
"FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", 
"FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", 
"FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", 
"FF", "FF", "FF", "FF", "FF", "FR", "FR", "FR", "FR", "FR", "FR", 
"FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", 
"FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", 
"FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", 
"FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "MR", "MR", 
"MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", 
"MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", 
"MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", 
"MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", 
"MR", "MR"), SRB = c("A", "A", "A", "A", "B", "B", "B", "B", 
"C", "C", "C", "C", "D", "D", "D", "D", "E", "E", "E", "E", "F", 
"F", "F", "F", "G", "G", "G", "G", "H", "H", "H", "H", "I", "I", 
"I", "I", "J", "J", "J", "J", "K", "K", "K", "K", "L", "L", "L", 
"L", "A", "A", "A", "A", "B", "B", "B", "B", "C", "C", "C", "C", 
"D", "D", "D", "D", "E", "E", "E", "E", "F", "F", "F", "F", "G", 
"G", "G", "G", "H", "H", "H", "H", "I", "I", "I", "I", "J", "J", 
"J", "J", "K", "K", "K", "K", "L", "L", "L", "L", "A", "A", "A", 
"A", "B", "B", "B", "B", "C", "C", "C", "C", "D", "D", "D", "D", 
"E", "E", "E", "E", "F", "F", "F", "F", "G", "G", "G", "G", "H", 
"H", "H", "H", "I", "I", "I", "I", "J", "J", "J", "J", "K", "K", 
"K", "K", "L", "L", "L", "L", "A", "A", "A", "A", "B", "B", "B", 
"B", "C", "C", "C", "C", "D", "D", "D", "D", "E", "E", "E", "E", 
"F", "F", "F", "F", "G", "G", "G", "G", "H", "H", "H", "H", "I", 
"I", "I", "I", "J", "J", "J", "J", "K", "K", "K", "K", "L", "L", 
"L", "L", "A", "A", "A", "A", "B", "B", "B", "B", "C", "C", "C", 
"C", "D", "D", "D", "D", "E", "E", "E", "E", "F", "F", "F", "F", 
"G", "G", "G", "G", "H", "H", "H", "H", "I", "I", "I", "I", "J", 
"J", "J", "J", "K", "K", "K", "K", "L", "L", "L", "L"), Var = c("Sep", 
"Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", 
"Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", 
"Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", 
"Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", 
"Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", 
"Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", 
"Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", 
"Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", 
"Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", 
"Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", 
"Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", 
"Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", 
"Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", 
"Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", 
"Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", 
"Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", 
"Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", 
"Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", 
"Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", 
"Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", 
"Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", 
"Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", 
"Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", 
"Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", 
"Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", 
"Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", 
"Jan", "Sep", "Nov", "Dec", "Jan"), Avg = c(0, 0, 0, 0, 0, 0.003854866, 
0.061106412, 0.071682456, 0.00059896, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0.098315511, 0, 0.004801973, 0.063737569, 1.964397117, 
0.145974324, 0.155585996, 2.021656134, 0.144172778, 0.039715264, 
0.072734409, 0.277671026, 0.395869671, 0.094600576, 0.416784381, 
0.509686627, 0.002391391, 0, 0, 0.01107881, 0.01776674, 0, 0.010330774, 
0.116655538, 0.019222751, 0.048718956, 0.003774214, 0.048769952, 
0.000404064, 0, 0, 0, 0, 0, 0, 0, 0.000808129, 0.025281279, 0.000935706, 
0.007744674, 0, 0, 0, 0, 0, 0, 0, 0, 0.024454972, 0.026716446, 
0.010346329, 0.031761494, 0.176511803, 0.002071508, 0, 0.004252813, 
0.022717954, 0.044868941, 0.017135875, 0.02262901, 0.204066738, 
0.061230801, 0.07198863, 0.190769924, 0.000673709, 0, 0, 0, 0.019986705, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.013240102, 0.006850463, 
0.007235685, 0.014429782, 0.001452608, 0, 0, 0, 0, 0, 0, 0, 0.056095432, 
0.00906202, 0.003799885, 0.018233616, 0.120468519, 0.001127625, 
0.005593377, 0.007697997, 0.116444276, 0.060955479, 0.027829322, 
0.059367186, 0.232145552, 0.02543649, 0.089065429, 0.08453107, 
0, 0, 0, 0, 0.015783104, 0, 0, 0.001603001, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0.008758482, 0.005594702, 0.013502405, 0.010478338, 
0, 0, 0, 0, 0, 0, 0, 0, 0.009879588, 0.005346856, 0.016387101, 
0.012409584, 0.001962154, 0.001734556, 0, 0.007286852, 0.032146728, 
0.014142999, 0.047512303, 0.025582156, 0.122754967, 0.019880746, 
0.142894055, 0.158862745, 0, 0, 0, 0, 0, 0, 0.000797048, 0, 0, 
0, 0, 0, 0, 0.002049933, 0, 0, 0, 0, 0.00742221, 0, 0.007622556, 
0.017323055, 0.002210342, 0.00431524, 0, 0, 0, 0, 0, 0, 0, 0, 
0.027315991, 0.062928107, 0.014127331, 0.026469388, 0, 0.001349002, 
0.087496432, 0.00255964, 0.03077342, 0.054161156, 0.036458083, 
0.024667621, 0.166278721, 0.16856285, 0.167405409, 0.163786308, 
0, 0, 0, 0, 0, 0.005834094, 0.002711961, 0, 0, 0, 0.009848701, 
0), Std = c(0, 0, 0, 0, 0, 0.004489104, 0.101688894, 0.064949287, 
0.00119792, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.07991923, 0, 0.008036531, 
0.10050744, 1.697626013, 0.050273793, 0.192288778, 1.475742817, 
0.084535326, 0.021019687, 0.078525424, 0.14633414, 0.44279092, 
0.077102705, 0.774390338, 0.259203849, 0.004782782, 0, 0, 0.012875964, 
0.025904929, 0, 0.014403465, 0.194281549, 0.013769571, 0.04968971, 
0.004797693, 0.019973095, 0.000808129, 0, 0, 0, 0, 0, 0, 0, 0.001616257, 
0.006575046, 0.001871412, 0.001771095, 0, 0, 0, 0, 0, 0, 0, 0, 
0.016365279, 0.01172175, 0.008458564, 0.00480104, 0.353023607, 
0.004143017, 0, 0.005034407, 0.011968923, 0.017255941, 0.016173283, 
0.004738345, 0.09921792, 0.015230554, 0.053297221, 0.0894813, 
0.001347418, 0, 0, 0, 0.039973411, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0.009959679, 0.011114534, 0.008731848, 0.011138718, 
0.002905215, 0, 0, 0, 0, 0, 0, 0, 0.047973853, 0.007723351, 0.005046906, 
0.007643438, 0.097174061, 0.002255249, 0.011186755, 0.005347923, 
0.043569938, 0.0436422, 0.015534005, 0.039596993, 0.144497391, 
0.022316042, 0.051071736, 0.030856551, 0, 0, 0, 0, 0.019935073, 
0, 0, 0.003206002, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.007767112, 
0.007440585, 0.010550983, 0.009268475, 0, 0, 0, 0, 0, 0, 0, 0, 
0.00909171, 0.007409357, 0.008755822, 0.014391853, 0.003924308, 
0.003469112, 0, 0.006015714, 0.010585435, 0.006256366, 0.042498882, 
0.009395162, 0.090393123, 0.011347959, 0.116478044, 0.091113074, 
0, 0, 0, 0, 0, 0, 0.001594095, 0, 0, 0, 0, 0, 0, 0.002434431, 
0, 0, 0, 0, 0.014844419, 0, 0.008804914, 0.006774488, 0.002575667, 
0.005594923, 0, 0, 0, 0, 0, 0, 0, 0, 0.019338184, 0.058467452, 
0.007963778, 0.028527808, 0, 0.002698003, 0.174992863, 0.005119279, 
0.017470551, 0.032819923, 0.037334218, 0.013764006, 0.122531556, 
0.165503776, 0.194704629, 0.085870127, 0, 0, 0, 0, 0, 0.009423796, 
0.005423922, 0, 0, 0, 0.019697402, 0)), row.names = c(NA, -240L
), class = c("tbl_df", "tbl", "data.frame"))

###keeping the order of my Var variables
dsr1$Var <- factor(dsr1$Var,levels=unique(dsr1$Var))
srb1$Var <- factor(srb1$Var,levels=unique(srb1$Var))

###bar plot
dsr_plot = ggplot(dsr1, aes(x = Var, y = Avg, ymin = Avg-Std, ymax = Avg+Std, fill = Site)) + 
    facet_wrap(~Site, ncol = 5) +
    geom_bar(colour = "black", stat = "identity", position = "dodge", width = 0.65, alpha = 0.8) +
    geom_errorbar(colour = "black", stat = "identity", position = position_dodge(0.65), width = 0.4) + 
    theme(axis.text.x = element_text(size = 16, angle = 90, vjust = 0.25), 
          axis.text.y = element_text(size = 16),
          panel.spacing.y = unit(1, "lines")) + 
    scale_y_continuous(expand = c(0,0))
dsr_plot

###adding the line plots
dsr_plot + geom_line(data = srb1, aes(x = Var, y = Avg, colour = SRB, group = SRB), size = 1) + 
    geom_point(data = srb1, size = 3, aes(colour = SRB)) + 
    geom_errorbar(data = srb1, aes(ymin = Avg - Std, ymax = Avg + Std, width = 0.25, colour = SRB), size = 0.5) + 
    scale_y_continuous(sec.axis = sec_axis(trans= ~./1000, name = "SRB (%)")) + 
    theme(legend.position = "top", legend.justification = "right",
          legend.text = element_text(size = 16),
          legend.title = element_text(size = 20, face = "bold"),
          axis.title.y.right = element_text(margin = margin(l = 15))) + guides(shape = guide_legend(override.aes = list(size = 4)))

As you can see, the line plots seem to be squished down at the bottom even though I've scaled my secondary y-axis down so that it is proportional to the values scale_y_continuous(sec.axis = sec_axis(trans= ~./1000, name = "SRB (%)"))

Thanks in advance.

The plot is drawn in layers, so once you've plotted data that determines the scale of the default y-axis, the remaining data must be accommodated that scale, which is why the lines are so close to the x-axis. The secondary axis does not depend on the data, nor is the data plotted relative to it; it's simply a tool for creating another labeling scheme opposite the primary axis.

My guess is you may have to find a way to transform the two Avg columns so that their values have a similar ranges of values.

In the code below I scaled the values of srb1$Avg by 10000 because its range is about 0 - 2 and scaling by 1000 still leaves the values very low in the plot.

library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 4.3.3
dsr1 <- structure(list(Site = c("DF", "DF", "DF", "DF", "DR", "DR", "DR", 
                        "DR", "FF", "FF", "FF", "FF", "FR", "FR", "FR", "FR", "MR", "MR", 
                        "MR", "MR"), Gene = c("dsr", "dsr", "dsr", "dsr", "dsr", "dsr", 
                                              "dsr", "dsr", "dsr", "dsr", "dsr", "dsr", "dsr", "dsr", "dsr", 
                                              "dsr", "dsr", "dsr", "dsr", "dsr"), Var = c("Sep", "Nov", "Dec", 
                                                                                          "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", 
                                                                                          "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan"), Avg = c(26192.15213, 
                                                                                                                                                           1060.62995, 5976.49445, 18627.35088, 9230.00425, 8154.795375, 
                                                                                                                                                           15599.3215, 33674.06525, 5375.773538, 1243.98395, 1368.094813, 
                                                                                                                                                           18126.26463, 11128.72975, 3512.390263, 15628.89188, 12514.475, 
                                                                                                                                                           10136.56213, 10330.751, 15907.58675, 21435.79538), Std = c(1971.846094, 
                                                                                                                                                                                                                      80.20201232, 1492.776454, 1716.348126, 1312.541508, 682.2806779, 
                                                                                                                                                                                                                      3751.417264, 1518.661188, 1035.91546, 169.1677183, 68.78619696, 
                                                                                                                                                                                                                      1653.659567, 3832.856982, 218.6107193, 650.6329325, 3516.620678, 
                                                                                                                                                                                                                      2528.207035, 451.3704901, 907.4479828, 550.5151197)), row.names = c(NA, 
                                                                                                                                                                                                                                                                                          -20L), class = c("tbl_df", "tbl", "data.frame"))

srb1 <- structure(list(Site = c("DF", "DF", "DF", "DF", "DF", "DF", "DF", 
                        "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", 
                        "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", 
                        "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", 
                        "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DF", "DR", "DR", "DR", 
                        "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", 
                        "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", 
                        "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", 
                        "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", "DR", 
                        "DR", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", 
                        "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", 
                        "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", 
                        "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", "FF", 
                        "FF", "FF", "FF", "FF", "FF", "FR", "FR", "FR", "FR", "FR", "FR", 
                        "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", 
                        "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", 
                        "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", 
                        "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "MR", "MR", 
                        "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", 
                        "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", 
                        "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", 
                        "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", "MR", 
                        "MR", "MR"), SRB = c("A", "A", "A", "A", "B", "B", "B", "B", 
                                             "C", "C", "C", "C", "D", "D", "D", "D", "E", "E", "E", "E", "F", 
                                             "F", "F", "F", "G", "G", "G", "G", "H", "H", "H", "H", "I", "I", 
                                             "I", "I", "J", "J", "J", "J", "K", "K", "K", "K", "L", "L", "L", 
                                             "L", "A", "A", "A", "A", "B", "B", "B", "B", "C", "C", "C", "C", 
                                             "D", "D", "D", "D", "E", "E", "E", "E", "F", "F", "F", "F", "G", 
                                             "G", "G", "G", "H", "H", "H", "H", "I", "I", "I", "I", "J", "J", 
                                             "J", "J", "K", "K", "K", "K", "L", "L", "L", "L", "A", "A", "A", 
                                             "A", "B", "B", "B", "B", "C", "C", "C", "C", "D", "D", "D", "D", 
                                             "E", "E", "E", "E", "F", "F", "F", "F", "G", "G", "G", "G", "H", 
                                             "H", "H", "H", "I", "I", "I", "I", "J", "J", "J", "J", "K", "K", 
                                             "K", "K", "L", "L", "L", "L", "A", "A", "A", "A", "B", "B", "B", 
                                             "B", "C", "C", "C", "C", "D", "D", "D", "D", "E", "E", "E", "E", 
                                             "F", "F", "F", "F", "G", "G", "G", "G", "H", "H", "H", "H", "I", 
                                             "I", "I", "I", "J", "J", "J", "J", "K", "K", "K", "K", "L", "L", 
                                             "L", "L", "A", "A", "A", "A", "B", "B", "B", "B", "C", "C", "C", 
                                             "C", "D", "D", "D", "D", "E", "E", "E", "E", "F", "F", "F", "F", 
                                             "G", "G", "G", "G", "H", "H", "H", "H", "I", "I", "I", "I", "J", 
                                             "J", "J", "J", "K", "K", "K", "K", "L", "L", "L", "L"), Var = c("Sep", 
                                                                                                             "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", 
                                                                                                             "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", 
                                                                                                             "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", 
                                                                                                             "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", 
                                                                                                             "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", 
                                                                                                             "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", 
                                                                                                             "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", 
                                                                                                             "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", 
                                                                                                             "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", 
                                                                                                             "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", 
                                                                                                             "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", 
                                                                                                             "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", 
                                                                                                             "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", 
                                                                                                             "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", 
                                                                                                             "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", 
                                                                                                             "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", 
                                                                                                             "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", 
                                                                                                             "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", 
                                                                                                             "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", 
                                                                                                             "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", 
                                                                                                             "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", 
                                                                                                             "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", 
                                                                                                             "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", 
                                                                                                             "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", 
                                                                                                             "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", 
                                                                                                             "Dec", "Jan", "Sep", "Nov", "Dec", "Jan", "Sep", "Nov", "Dec", 
                                                                                                             "Jan", "Sep", "Nov", "Dec", "Jan"), Avg = c(0, 0, 0, 0, 0, 0.003854866, 
                                                                                                                                                         0.061106412, 0.071682456, 0.00059896, 0, 0, 0, 0, 0, 0, 0, 0, 
                                                                                                                                                         0, 0, 0, 0.098315511, 0, 0.004801973, 0.063737569, 1.964397117, 
                                                                                                                                                         0.145974324, 0.155585996, 2.021656134, 0.144172778, 0.039715264, 
                                                                                                                                                         0.072734409, 0.277671026, 0.395869671, 0.094600576, 0.416784381, 
                                                                                                                                                         0.509686627, 0.002391391, 0, 0, 0.01107881, 0.01776674, 0, 0.010330774, 
                                                                                                                                                         0.116655538, 0.019222751, 0.048718956, 0.003774214, 0.048769952, 
                                                                                                                                                         0.000404064, 0, 0, 0, 0, 0, 0, 0, 0.000808129, 0.025281279, 0.000935706, 
                                                                                                                                                         0.007744674, 0, 0, 0, 0, 0, 0, 0, 0, 0.024454972, 0.026716446, 
                                                                                                                                                         0.010346329, 0.031761494, 0.176511803, 0.002071508, 0, 0.004252813, 
                                                                                                                                                         0.022717954, 0.044868941, 0.017135875, 0.02262901, 0.204066738, 
                                                                                                                                                         0.061230801, 0.07198863, 0.190769924, 0.000673709, 0, 0, 0, 0.019986705, 
                                                                                                                                                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.013240102, 0.006850463, 
                                                                                                                                                         0.007235685, 0.014429782, 0.001452608, 0, 0, 0, 0, 0, 0, 0, 0.056095432, 
                                                                                                                                                         0.00906202, 0.003799885, 0.018233616, 0.120468519, 0.001127625, 
                                                                                                                                                         0.005593377, 0.007697997, 0.116444276, 0.060955479, 0.027829322, 
                                                                                                                                                         0.059367186, 0.232145552, 0.02543649, 0.089065429, 0.08453107, 
                                                                                                                                                         0, 0, 0, 0, 0.015783104, 0, 0, 0.001603001, 0, 0, 0, 0, 0, 0, 
                                                                                                                                                         0, 0, 0, 0, 0, 0, 0.008758482, 0.005594702, 0.013502405, 0.010478338, 
                                                                                                                                                         0, 0, 0, 0, 0, 0, 0, 0, 0.009879588, 0.005346856, 0.016387101, 
                                                                                                                                                         0.012409584, 0.001962154, 0.001734556, 0, 0.007286852, 0.032146728, 
                                                                                                                                                         0.014142999, 0.047512303, 0.025582156, 0.122754967, 0.019880746, 
                                                                                                                                                         0.142894055, 0.158862745, 0, 0, 0, 0, 0, 0, 0.000797048, 0, 0, 
                                                                                                                                                         0, 0, 0, 0, 0.002049933, 0, 0, 0, 0, 0.00742221, 0, 0.007622556, 
                                                                                                                                                         0.017323055, 0.002210342, 0.00431524, 0, 0, 0, 0, 0, 0, 0, 0, 
                                                                                                                                                         0.027315991, 0.062928107, 0.014127331, 0.026469388, 0, 0.001349002, 
                                                                                                                                                         0.087496432, 0.00255964, 0.03077342, 0.054161156, 0.036458083, 
                                                                                                                                                         0.024667621, 0.166278721, 0.16856285, 0.167405409, 0.163786308, 
                                                                                                                                                         0, 0, 0, 0, 0, 0.005834094, 0.002711961, 0, 0, 0, 0.009848701, 
                                                                                                                                                         0), Std = c(0, 0, 0, 0, 0, 0.004489104, 0.101688894, 0.064949287, 
                                                                                                                                                                     0.00119792, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.07991923, 0, 0.008036531, 
                                                                                                                                                                     0.10050744, 1.697626013, 0.050273793, 0.192288778, 1.475742817, 
                                                                                                                                                                     0.084535326, 0.021019687, 0.078525424, 0.14633414, 0.44279092, 
                                                                                                                                                                     0.077102705, 0.774390338, 0.259203849, 0.004782782, 0, 0, 0.012875964, 
                                                                                                                                                                     0.025904929, 0, 0.014403465, 0.194281549, 0.013769571, 0.04968971, 
                                                                                                                                                                     0.004797693, 0.019973095, 0.000808129, 0, 0, 0, 0, 0, 0, 0, 0.001616257, 
                                                                                                                                                                     0.006575046, 0.001871412, 0.001771095, 0, 0, 0, 0, 0, 0, 0, 0, 
                                                                                                                                                                     0.016365279, 0.01172175, 0.008458564, 0.00480104, 0.353023607, 
                                                                                                                                                                     0.004143017, 0, 0.005034407, 0.011968923, 0.017255941, 0.016173283, 
                                                                                                                                                                     0.004738345, 0.09921792, 0.015230554, 0.053297221, 0.0894813, 
                                                                                                                                                                     0.001347418, 0, 0, 0, 0.039973411, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                                                                                                                                                     0, 0, 0, 0, 0, 0, 0.009959679, 0.011114534, 0.008731848, 0.011138718, 
                                                                                                                                                                     0.002905215, 0, 0, 0, 0, 0, 0, 0, 0.047973853, 0.007723351, 0.005046906, 
                                                                                                                                                                     0.007643438, 0.097174061, 0.002255249, 0.011186755, 0.005347923, 
                                                                                                                                                                     0.043569938, 0.0436422, 0.015534005, 0.039596993, 0.144497391, 
                                                                                                                                                                     0.022316042, 0.051071736, 0.030856551, 0, 0, 0, 0, 0.019935073, 
                                                                                                                                                                     0, 0, 0.003206002, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.007767112, 
                                                                                                                                                                     0.007440585, 0.010550983, 0.009268475, 0, 0, 0, 0, 0, 0, 0, 0, 
                                                                                                                                                                     0.00909171, 0.007409357, 0.008755822, 0.014391853, 0.003924308, 
                                                                                                                                                                     0.003469112, 0, 0.006015714, 0.010585435, 0.006256366, 0.042498882, 
                                                                                                                                                                     0.009395162, 0.090393123, 0.011347959, 0.116478044, 0.091113074, 
                                                                                                                                                                     0, 0, 0, 0, 0, 0, 0.001594095, 0, 0, 0, 0, 0, 0, 0.002434431, 
                                                                                                                                                                     0, 0, 0, 0, 0.014844419, 0, 0.008804914, 0.006774488, 0.002575667, 
                                                                                                                                                                     0.005594923, 0, 0, 0, 0, 0, 0, 0, 0, 0.019338184, 0.058467452, 
                                                                                                                                                                     0.007963778, 0.028527808, 0, 0.002698003, 0.174992863, 0.005119279, 
                                                                                                                                                                     0.017470551, 0.032819923, 0.037334218, 0.013764006, 0.122531556, 
                                                                                                                                                                     0.165503776, 0.194704629, 0.085870127, 0, 0, 0, 0, 0, 0.009423796, 
                                                                                                                                                                     0.005423922, 0, 0, 0, 0.019697402, 0)), row.names = c(NA, -240L
                                                                                                                                                                     ), class = c("tbl_df", "tbl", "data.frame"))

###keeping the order of my Var variables
dsr1$Var <- factor(dsr1$Var,levels=unique(dsr1$Var))
srb1$Var <- factor(srb1$Var,levels=unique(srb1$Var))

range(srb1$Avg)
#> [1] 0.000000 2.021656

###bar plot
dsr_plot = ggplot(dsr1, aes(x = Var, y = Avg, ymin = Avg-Std, ymax = Avg+Std, fill = Site)) + 
  facet_wrap(~Site, ncol = 5) +
  geom_bar(colour = "black", stat = "identity", position = "dodge", width = 0.65, alpha = 0.8) +
  geom_errorbar(colour = "black", stat = "identity", position = position_dodge(0.65), width = 0.4) + 
  theme(axis.text.x = element_text(size = 16, angle = 90, vjust = 0.25), 
        axis.text.y = element_text(size = 16),
        panel.spacing.y = unit(1, "lines")) #+ 
  #scale_y_continuous(expand = c(0,0))
dsr_plot


###adding the line plots
dsr_plot + geom_line(data = srb1, aes(x = Var, y = Avg * 10000, colour = SRB, group = SRB), size = 1) + 
  geom_point(data = srb1, size = 3, aes(colour = SRB)) + 
  geom_errorbar(data = srb1, aes(ymin = (Avg - Std)*10000, ymax = (Avg + Std)*10000, width = 0.25, colour = SRB), size = 0.5) + 
  scale_y_continuous(sec.axis = sec_axis(trans= ~./10000, name = "SRB (%)"), expand = c(0,0)) + 
  theme(legend.position = "top", legend.justification = "right",
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 20, face = "bold"),
        axis.title.y.right = element_text(margin = margin(l = 15))) + guides(shape = guide_legend(override.aes = list(size = 4)))
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
#> Warning: The `trans` argument of `sec_axis()` is deprecated as of ggplot2 3.5.0.
#> ℹ Please use the `transform` argument instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.

Created on 2024-07-25 with reprex v2.0.2

2 Likes

This worked. Thank you again!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.