I want to adjust y-axis limit both for primary and secondary axis by using the example of case: How to limit primary y-axis and secondary y-axis? and ggplot with 2 y axes on each side and different scales
I want to adjust primary y-axis from 0-50 and secondary y-axis from 0-500, then don't want to show any plot with higher value than the limit (even though there are some data with values higher than the limit).
I didn't get any error with my code but the limit that I have set is not been successfully applied to the graph.
Here is my example of data:
df2 <- structure(list(startdate = structure(c(17903, 17910, 17917, 17924,
17931, 17938, 17945, 17952, 17959, 17966, 17982, 17987, 18001,
18003, 18015, 18022, 18029, 18031, 18036, 18043, 18050, 18057,
18064, 18072, 18079, 18085, 18099, 18106, 18113, 18120, 18127,
18134, 18141, 18148, 18155, 18162, 18169, 18183, 18197, 18204,
18211, 18218, 18225, 18227, 18232, 18234, 18239, 18246, 18253,
18267, 18274, 18281, 18288, 18295, 18302, 18309, 18316, 18323,
18330, 18337, 18344, 18351, 18358, 18365, 18373, 18379, 18386,
18393, 18400, 18407, 18414, 18421, 18428, 18430, 18435, 18442,
18449, 18456, 18463, 18472, 18477, 18484, 18491, 18498, 18505,
18514, 18519, 18526, 18533, 18540, 18547, 18554, 18561, 18568,
18575, 18583, 18589, 18596, 18603, 18610, 18617, 18624), class = "Date"),
Al = c(24.744, 19.272, 15.245, 21.497, 26.086, 5.867, 23.722,
30.269, 25.666, 17.106, 53.07, NA, 226.995, 70.341, 108.865,
18.15, 445.203, 393.528, 11.151, 52.329, 37.737, 16.68, 124.039,
22.667, 19.125, 82.391, 87.85, 19.041, 77.098, 34.27, 10.912,
116.28, 42.9, 9.282, 35.504, NA, 133.95, 94.311, 124.97,
63.374, 99.062, 54.366, 38.925, 66.56, 19.525, 221.973, 140.54,
68.699, 117.965, 456, 13, 44.5, 46.6, 69.4, NA, 12.3, 6.81,
NA, 20.6, 19.6, NA, 24.2, 71.6, 566, 219, 158, 58.2, 217,
351, 13.6, 38.3, 91.5, 90.2, 23.8, 23.4, 21.4, 42.9, 13.8,
NA, 35.8, 24, 9.11, 32.6, 24.6, 286, NA, 28.9, 10, NA, 331,
101, 6.58, 83.9, 2230, 1100, NA, NA, 638, 622, 143, 96, 28.3
), Fe = c(9.627, 12.429, 10.115, 9.498, 14.555, 4.39, 12.201,
12.888, 12.318, 9.889, 19.607, 11.202, 51.294, 21.877, 43.531,
9.539, 131.812, 123.998, 7.991, 21.365, 18.732, 8.378, 42.805,
5.886, 10.994, 29.268, NA, 7.832, 15.377, 12.558, 4.829,
42.002, 16.464, 5.545, 17.778, NA, 67.634, 37.384, 49.764,
28.589, 37.174, 21.271, 16.639, 29.878, 11.689, 90.459, 36.085,
15.883, 34.31, 210, 7.55, 21.8, 23.4, 32.2, 8.5, 5.76, 4.83,
1.85, 10.9, 10.5, 2.16, 12.4, 34.1, 212, 106, 65.1, 26.9,
93.1, 163, 6.41, 15.4, 34.7, 36, 10.1, 14.7, 11, 23, 5.36,
1.72, 23.3, 20.2, 6.64, 20.1, 14.3, 129, NA, 13.9, 6.6, NA,
193, 42.1, 4.29, 37.7, 1260, 585, NA, NA, 288, 289, 64.6,
43, 14.1), Mn = c(0.184, 0.377, 0.334, 0.163, 0.416, 0.101,
0.351, 0.359, 0.302, 0.406, 0.393, 0.277, 2.624, 0.656, 0.822,
0.205, 2.401, 2.403, 0.161, 0.415, NA, 0.155, 1.416, 0.134,
0.212, NA, NA, 0.337, 0.898, 0.217, NA, 1.027, 0.264, NA,
0.284, NA, 1.176, 0.599, 0.808, 0.462, 0.826, 0.487, 0.293,
0.518, 0.242, 1.848, 1.083, 0.483, 0.732, 4.22, 0.227, 0.564,
0.446, 0.624, 0.178, 0.198, 0.25, 0.054, 0.245, 0.296, 0.071,
0.304, 0.739, 4.4, 1.62, 0.987, 0.405, 1.45, 3.04, 0.121,
0.447, 0.756, 0.559, 0.201, 0.3, 0.136, 0.431, 0.885, NA,
0.456, 0.366, 0.217, 0.257, 0.208, 3.59, NA, 0.208, 0.091,
NA, 4.91, 0.685, 0.076, 0.7, 22.3, 11.1, NA, NA, 4.21, 5.26,
1.08, 0.722, 0.269), Ti = c(1.032, 0.763, 0.795, 0.861, 1.263,
0.426, 1.168, 1.284, 1.257, 0.706, 1.566, 0.965, 3.978, 1.939,
4.109, 0.787, 11.025, 12.884, 0.691, 1.58, 1.541, 0.788,
4.588, 0.45, 0.873, 2.115, NA, 1.636, 1.195, 0.971, 0.37,
3.132, 1.351, 0.328, 1.222, NA, 4.251, 2.502, 3.157, 2.044,
2.627, 1.698, 1.34, 1.879, 0.77, 4.539, 2.46, 1.17, 2.2,
8.52, 0.492, 1.41, 1.65, 2.1, 0.652, 0.368, 0.277, NA, 0.762,
0.712, NA, 0.787, 2.2, 11.6, 5.29, 4.65, 2.16, 5.38, 13.7,
0.555, 1.41, 2.78, 3.15, 0.88, 0.955, 0.853, 1.65, 0.379,
NA, 1.05, 1.06, 0.574, 1.45, 1.02, 7.83, NA, 1.06, 0.501,
NA, 8.52, 2.96, 0.339, 2.68, 47.2, 23.2, NA, NA, 13.2, 12.9,
4.11, 2.14, 1.1)), row.names = c(NA, -102L), class = c("tbl_df",
"tbl", "data.frame"))
And here is the code:
library(ggplot2)
library(dplyr)
library(tidyr)
library(lubridate)
library(scales)
label_y1 = expression(bold(Mn,Ti~(ng/m^{3})))
label_y2 = expression(bold(Al,Fe~(ng/m^{3})))
#Determine certain date for shading
shade <- df2 %>% transmute(year = year(startdate)) %>% unique() %>%
mutate( from = as.Date(paste0(year, "-02-14")), to = as.Date(paste0(year, "-05-07")))
# Function factory for secondary axis transforms
train_sec <- function(primary, secondary) {
from <- range(secondary)
to <- range(primary)
# Forward transform for the data
forward <- function(x) {
rescale(x, from = from, to = to)
}
# Reverse transform for the secondary axis
reverse <- function(x) {
rescale(x, from = to, to = from)
}
list(fwd = forward, rev = reverse)
#Set the limit of both y-axis
sec <- train_sec(c(0, 50), c(0, 500))
#Plotting data
ggplot(df2) +
geom_line( aes(x=startdate, y=Mn, color='Mn')) +
geom_line( aes(x=startdate, y=Ti, color='Ti')) +
geom_line( aes(x=startdate, y= sec$fwd(Al), color = 'Al')) +
geom_line( aes(x=startdate, y= sec$fwd(Fe), color = 'Fe')) +
geom_rect(data = shade, aes(xmin = from, xmax = to, ymin = -Inf, ymax = Inf), fill = 'red',alpha=0.1) +
scale_y_continuous(name = label_y1, sec.axis = sec_axis(~sec$rev(.), name = label_y2))+ ggtitle ("a)")+
theme_bw()+ theme(legend.position = c(0.1, 0.9),legend.direction="horizontal", axis.text.x = element_text(face="bold", size=10) ,axis.text.y = element_text(face="bold", size=10), axis.title = element_text(size = 10), plot.title = element_text(size=10, face="bold", hjust=0.05,vjust = - 12), legend.spacing.y = unit(0, "mm"), axis.text = element_text(colour = 1),legend.background = element_blank(),legend.box.background = element_blank(), legend.key = element_blank(), legend.justification = "left")+labs(color = NULL, fill = NULL, x=NULL)+guides(colour = guide_legend(override.aes = list(size=1)))
I'm not really sure why the limitation to y-axis is not successful. If anybody know the reason and how to fix this, please let me know. I really appreciate it.
Thank you so much. Best regards.