How to adjust the limit of primary and secondary y-axis? - r

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.

I added limits directly into scale_y_continous and I think this works
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(
limits = c(0, 50),
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)))

Related

Print the labels of a list of variables

What I am trying to do is print the labels of a set of variables contained in a dataframe. I would like to get the plain text out of the variables. The list of variables is:
#The list of characters
vars <- c("hc", "prot", "gratot", "mo", "po", "sa", "alcoholg", "energiat", "fibra" , "colest" , "fit" , "linolenico")
vars <- sort(vars)
#The df where the variables are contained with the attributes is dat1
for (i in vars_dieta) {
x <- print(attr(dat1, "label"))
}
# or
lapply(vars_dieta, attr(dat1, which = "label"))
The database comes from spss after importing, with read_sav and as_factor, but as I paste right below, you will see exactly how the database is made up.
dat$hc
attr(,"label")
[1] "CFCA: Hidratos de carbono (g/día)"
attr(,"format.spss")
[1] "F13.9"
attr(,"display_width")
[1] 13
# The database
dat1 <- structure(list(hc = structure(c(230.693, 219.261, 293.859, 185.046,
290.647, 179.877, 163.61, 226.767, 237.187, 215.529, 231.871,
219.799, 313.474, 213.825, 266.067, 273.789, 331.974, 236.96,
240.23, 257.44, 240.026, 441.27, 298.091, 282.346, 338.679, 344.482,
205.039, 408.26, 209.583, 206.113, 145.448, 218.672, 182.319,
387.565, 239.381, 255.337, 156.895, 140.789, 228.158, 170.847,
145.242, 135.324, 173.325, 211.33, 190.705, 211.179, 119.748,
228.65, 174.01, 150.372, 170.041, 183.002, 175.521, 268.408,
177.078, 263.456, 214.696, 217.049, 210.993, 231.577, 178.468,
185.717, 178.53, 276.308, 240.677, 258.154, 252.059, 268.494,
256.204, 247.231, 142.259, 218.504, 244.195, 189.491, 266.341,
230.099, 170.482, 441.223, 258.654, 374.204, 259.538, 261.264,
255.508, 233.714, 228.232, 313.693, 486.645, 246.935, 198.881,
193.139, 214.894, 273.364, 312.362, 258.181, 297.027, 199.269,
274.294, 223.725, 226.847, 222.784, 190.2, 189.398, 151.763,
247.258, 141.653, 212.13, 220.985, 353.483, 280.084, 244.07,
214.19, 317.906, 323.38, 119.814, 282.184, 302.622, 250.576,
281.525, 254.08, 374.777, 232.324, 333.179, 485.481, 245.36,
275.62, 356.797, 222.547, 295.833, 232.076, 238.53, 208.007,
182.662, 268.2, 223.692, 271.954, 159.47, 231.784, 183.465, 152,
155.572, 143.51, 135.732, 229.224, 173.907, 172.249, 370.952,
160.821, 183.978, 159.327, 159.559, 89.741), label = "CFCA: Hidratos de carbono (g/día)", format.spss = "F13.9", display_width = 13L),
prot = structure(c(81.729, 82.578, 80.662, 68.878, 96.12,
81.239, 79.644, 63.994, 98.849, 105.171, 114.676, 102.529,
96.756, 134.653, 101.368, 117.145, 113.136, 82.581, 95.757,
111.079, 98.818, 126.634, 121.541, 114.376, 131.567, 85.932,
85.488, 124.433, 119.113, 118.75, 108.386, 101.582, 95.243,
155.193, 106.031, 97.692, 83.388, 73.258, 108.006, 70.92,
69.179, 76.258, 83.603, 65.39, 82.258, 77.735, 86.489, 73.553,
97.218, 79.117, 80.672, 87.096, 72.436, 97.012, 68.878, 101.447,
101.158, 89.058, 70.261, 84.105, 80.543, 66.761, 79.722,
112.796, 107.256, 129.607, 123.97, 95.669, 90.564, 97.101,
74.133, 80.542, 93.962, 68.558, 85.29, 88.855, 81.988, 99.175,
89.647, 88.89, 94.034, 67.229, 63.265, 81.417, 85.716, 82.415,
120.431, 82.453, 81.199, 110.601, 96.591, 122.377, 95.466,
95.698, 120.563, 75.446, 78.304, 105.92, 64.105, 55.356,
58.892, 61.881, 62.499, 63.884, 58.558, 76.469, 63.713, 85.228,
78.618, 75.469, 70.841, 87.631, 82.727, 53.779, 78.869, 76.518,
114.562, 120.435, 99.251, 91.892, 73.883, 101.086, 106.261,
109.385, 81.463, 133.594, 72.958, 110.315, 109.527, 107.269,
89.041, 100.081, 106.141, 126.01, 127.185, 98.987, 111.513,
113.386, 96.246, 99.381, 78.632, 98.67, 79.363, 103.707,
123.755, 179.161, 97.847, 113.164, 100.464, 87.237, 64.408
), label = "CFCA: proteinas (g/día)", format.spss = "F13.9", display_width = 13L),
gratot = structure(c(104.232, 83.461, 101.03, 78.998, 105.364,
96.057, 57.706, 55.549, 132.129, 117.275, 116.273, 85.622,
102.483, 161.421, 119.794, 121.244, 111.881, 106.379, 91.927,
107.036, 102.698, 172.983, 132.333, 117.561, 156.322, 113.514,
106.141, 157.12, 160.613, 131.885, 115.641, 87.151, 113.074,
169.398, 116.172, 107.255, 60.372, 55.361, 100.002, 57.939,
58.816, 66.906, 83.102, 96.74, 104.009, 83.002, 78.664, 84.8,
111.92, 68.625, 78.631, 71.015, 64.997, 109.804, 106.53,
108.722, 102.954, 99.649, 85.906, 87.83, 83.754, 112.611,
99.204, 157.618, 104.072, 132.26, 136.923, 164.095, 100.278,
118.911, 72.454, 62.62, 60.705, 46.047, 106.83, 86.41, 67.963,
69.052, 94.615, 78.451, 89.707, 82.243, 60.5, 89.102, 48.042,
77.455, 121.191, 81.699, 95.767, 102.041, 92.682, 86.262,
103.588, 68.942, 76.341, 51.316, 77.068, 80.433, 68.78, 81.286,
98.088, 56.282, 53.843, 90.833, 76.141, 55.707, 50.421, 73.055,
83.398, 90.634, 82.95, 81.377, 90.855, 49.397, 74.594, 77.508,
95.664, 75.191, 100.236, 97.844, 76.927, 64.636, 108.666,
114.747, 62.282, 98.975, 72.558, 108.54, 89.715, 124.178,
99.856, 78.046, 124.541, 84.947, 98.385, 48.166, 118.131,
71.302, 134.867, 105.288, 61.154, 101.843, 109.757, 98.237,
55.093, 138.009, 77.598, 81.632, 106.979, 76.08, 72.292), label = "CFCA: Lípidos (g/día)", format.spss = "F13.9", display_width = 13L),
mo = structure(c(54.8, 45.763, 51.364, 44.481, 56.14, 49.656,
30.347, 26.447, 73.413, 59.72, 57.939, 41.644, 54.739, 79.568,
61.446, 62.266, 48.146, 57.754, 42.661, 54.828, 53.614, 78.347,
65.4, 58.473, 75.786, 58.565, 56.633, 80.982, 75.763, 70.088,
61.392, 41.848, 56.472, 79.674, 57.469, 52.39, 29.865, 28.564,
54.617, 33.011, 18.661, 32.342, 46.648, 51.752, 51.708, 36.74,
40.648, 46.444, 55.587, 30.443, 33.82, 31.325, 26.621, 49.147,
56.262, 49.003, 56.169, 50.324, 51.022, 48.679, 37.02, 63.323,
51.706, 79.291, 55.189, 67.672, 75.131, 82.236, 54.141, 61.407,
36.539, 30.538, 26.777, 24.022, 55.786, 46.399, 33.433, 30.523,
51.145, 39.445, 49.722, 46.197, 28.939, 47.008, 25.045, 43.738,
61.391, 46.139, 50.753, 53.455, 43.379, 47.007, 52.43, 35.776,
36.048, 26.397, 43.523, 39.129, 40.633, 46.315, 54.141, 32.793,
26.321, 40.339, 33.766, 29.616, 25.062, 33.511, 45.581, 48.16,
45.625, 29.869, 40.757, 19.003, 35.363, 37.346, 45.731, 35.034,
42.321, 45.279, 43.946, 24.031, 56.197, 57.003, 30.526, 33.353,
33.134, 53.716, 41.885, 57.741, 42.988, 34.964, 56.097, 36.345,
42.367, 24.71, 58.373, 35.239, 69.874, 54.296, 30.208, 54.197,
58.138, 46.471, 21.91, 63.942, 32.339, 41.334, 45.217, 34.048,
42.398), label = "CFCA: AGM-monoinsaturados (g/día)", format.spss = "F12.9", display_width = 12L),
po = structure(c(13.33, 13.321, 16.063, 11.753, 14.948, 11.641,
9.145, 12.26, 17.466, 13.211, 18.659, 14.334, 14.554, 22.203,
14.017, 14.899, 26.048, 15.231, 12.436, 17.227, 16.979, 30.809,
16.298, 19.72, 30.809, 15.512, 13.803, 23.34, 16.04, 13.127,
15.66, 10.89, 17.408, 24.363, 19.932, 17.676, 7.368, 6.552,
17.436, 8.36, 14.107, 7.759, 10.817, 13.462, 25.005, 16.904,
9.119, 10.399, 12.868, 12.495, 13.177, 7.165, 12.531, 16.404,
18.677, 18.346, 14.118, 20.235, 11.059, 12.26, 12.437, 13.2,
15.365, 28.611, 15.215, 16.73, 16.36, 24.698, 13.754, 13.431,
9.601, 6.348, 12.711, 6.789, 17.808, 12.099, 7.431, 12.583,
9.842, 8.914, 9.623, 9.616, 8.48, 12.237, 6.183, 11.719,
20.61, 12.106, 12.66, 14.88, 15.447, 9.936, 13.01, 7.14,
10.035, 6.755, 11.023, 15.627, 8.948, 12.334, 9.725, 8.784,
8.925, 18.547, 20.497, 7.431, 11.012, 15.02, 13.073, 15.23,
12.423, 20.543, 21.123, 7.142, 12.601, 13.271, 17.365, 12.619,
13.814, 17.168, 8.895, 6.047, 14.735, 17.996, 11.188, 27.129,
19.92, 14.603, 12.444, 25.665, 21.717, 8.339, 34.086, 11.68,
17.653, 5.675, 20.291, 8.754, 16.352, 14.743, 9.161, 13.046,
19.189, 18.021, 11.064, 21.682, 13.118, 10.075, 25.505, 13.905,
9.629), label = "CFCA: AGP-poliinsaturados (g/día)", format.spss = "F12.9", display_width = 12L),
sa = structure(c(26.419, 15.449, 23.274, 15.415, 26.991,
25.3, 14.278, 9.319, 30.999, 34.65, 27.756, 23.167, 23.761,
52.353, 37.013, 34.729, 28.098, 26.978, 30.836, 22.715, 21.036,
47.56, 36.664, 29.111, 38.56, 28.2, 25.569, 47.028, 57.253,
41.479, 27.965, 25.899, 32.106, 55.22, 27.845, 24.767, 15.822,
16.866, 21.067, 14.298, 14.613, 19.191, 17.914, 23.831, 24.514,
18.95, 22.47, 20.153, 28.74, 16.715, 23.362, 22.008, 21.606,
31.881, 22.392, 28.19, 28.276, 21.181, 18.417, 19.656, 28.103,
29.084, 23.442, 45.928, 25.623, 43.162, 39.369, 41.407, 25.135,
34.893, 14.998, 20.177, 12.242, 10.124, 23.366, 19.374, 18.992,
18.709, 27.042, 21.674, 20.974, 20.976, 13.929, 21.588, 11.174,
14.925, 30.619, 17.289, 22.923, 24.58, 22.811, 20.756, 26.515,
19.116, 23.41, 13.677, 16.185, 21.159, 13.369, 17.582, 28.474,
14.502, 12.343, 24.443, 17.154, 14.598, 9.097, 17.957, 16.927,
17.992, 16.934, 22.683, 20.498, 16.458, 21.765, 23.57, 21.659,
18.849, 31.928, 26.417, 16.145, 24.575, 27.499, 27.699, 11.189,
21.917, 14.354, 29.687, 21.267, 33.812, 25.064, 22.22, 22.063,
23.902, 24.543, 11.745, 26.299, 17.712, 28.354, 28.072, 19.706,
33.988, 27.042, 30.612, 15.335, 38.856, 20.625, 22.137, 24.68,
22.618, 15.179), label = "CFCA: AGS-saturados (g/día)", format.spss = "F12.9", display_width = 12L),
alcoholg = structure(c(0, 0, 0, 5.919, 0, 4.943, 0.6, 10.23,
0.693, 1.461, 1.387, 0.6, 53.177, 4.987, 3.075, 28.561, 0,
0.6, 3.547, 0.6, 0, 12.699, 5.87, 4.384, 25.18, 26, 80.662,
9.599, 11.861, 2.086, 0, 4.384, 12.616, 4.28, 5.98, 28.393,
0, 1.293, 21.929, 4.04, 0.682, 1.975, 10.943, 7.525, 2.168,
1.293, 2.061, 27.061, 5.057, 14.819, 15.384, 22.456, 1.486,
27.333, 26, 0, 0.6, 0.693, 0, 6.025, 0.6, 0, 1.11, 27.701,
0, 4.457, 0, 7.958, 21.484, 0, 0, 0, 0.693, 0, 0, 1.486,
36.23, 30.384, 10.4, 8.038, 10.4, 4.457, 0, 0, 12.182, 1.775,
0, 11.733, 11.082, 0, 0, 0, 0, 0.693, 8.841, 0, 11.082, 20.63,
2.155, 7.071, 0, 0, 0, 5.343, 0, 11.082, 0, 55.557, 0.682,
4.457, 0, 10.23, 0, 1.486, 0.693, 21.578, 1.293, 0, 0, 41.25,
0, 0, 0, 0, 41.643, 0, 0, 12.211, 3.857, 12.757, 0.693, 0,
0, 0.6, 10.4, 0, 13.439, 0, 17.141, 20.63, 5.139, 1.461,
20.63, 0, 0.682, 0, 0, 11.082, 1.461, 0, 0.693), label = "CFCA: gramos de alcohol puro (vino tinto, otros vinos, cerveza y destilados)", format.spss = "F11.8", display_width = 11L),
energiat = structure(c(2187.775, 1958.507, 2407.359, 1768.105,
2495.343, 1943.577, 1496.567, 1734.6, 2538.161, 2348.509,
2442.351, 2064.112, 2935.506, 2881.607, 2569.41, 2854.857,
2787.371, 2239.773, 2196.123, 2441.603, 2279.663, 3917.352,
2910.616, 2675.63, 3464.147, 2925.282, 2682.011, 3612.038,
2843.332, 2501.013, 2056.106, 2096.061, 2216.22, 3725.579,
2469.059, 2576.162, 1504.484, 1363.487, 2398.173, 1516.796,
1391.808, 1462.309, 1852.227, 2030.214, 2043.108, 1911.726,
1547.352, 2161.435, 2127.59, 1639.314, 1818.225, 1876.721,
1587.201, 2641.25, 2124.59, 2438.109, 2194.199, 2126.127,
1898.165, 2095.374, 1794.028, 2023.409, 1933.615, 3168.886,
2328.38, 2772.581, 2736.422, 2989.209, 2439.969, 2447.533,
1517.656, 1759.764, 1903.823, 1446.623, 2367.995, 2063.907,
1875.16, 2995.753, 2317.543, 2614.696, 2294.448, 2085.359,
1819.591, 2062.44, 1773.441, 2293.951, 3519.021, 2134.981,
2059.795, 2133.326, 2080.077, 2359.319, 2563.605, 2040.847,
2419.323, 1560.702, 2181.58, 2186.888, 1797.909, 1893.632,
1879.162, 1511.654, 1341.633, 2099.468, 1486.111, 1733.326,
1592.581, 2801.242, 2190.162, 2125.063, 1886.674, 2426.152,
2442.117, 1149.346, 2120.413, 2365.176, 2330.579, 2284.563,
2315.447, 3036.019, 1917.17, 2318.788, 3344.965, 2451.706,
2280.373, 2852.342, 1835.044, 2686.926, 2200.847, 2590.097,
2091.746, 1833.39, 2618.234, 2167.53, 2554.821, 1467.321,
2530.448, 1829.123, 2326.773, 2111.813, 1474.929, 1864.425,
2366.571, 1994.585, 1684.627, 3442.536, 1733.056, 2000.833,
2012.205, 1671.908, 1272.081), label = "CFCA: energia total (Kcal)", format.spss = "F13.8", display_width = 13L),
fibra = structure(c(25.034, 30.364, 32.947, 34.616, 35.238,
21.252, 27.337, 36.418, 38.043, 36.198, 32.76, 32.022, 30.503,
16.376, 35.188, 35.138, 32.745, 38.222, 28.138, 42.888, 42.888,
33.714, 24.431, 22.557, 35.894, 37.879, 18.598, 24.916, 21.062,
32.809, 26.855, 29.216, 16.34, 35.149, 37.29, 26.698, 17.644,
11.694, 24.148, 23.661, 23.502, 16.07, 17.538, 22.796, 18.562,
17.9, 14.997, 19.233, 18.353, 20.376, 16.95, 17.797, 10.443,
25.176, 23.556, 26.164, 20.499, 35.479, 17.948, 17.575, 17.764,
16.905, 24.283, 19.878, 22.032, 30.288, 20.622, 24.863, 23.778,
28.2, 24.068, 16.944, 28.006, 17.243, 29.544, 24.548, 21.078,
27.06, 19.073, 28.445, 24.82, 26.972, 23.256, 22.856, 30.207,
54.289, 31.437, 19.357, 20.69, 16.265, 20.765, 48.651, 25.611,
19.73, 25.937, 22.969, 30.403, 25.573, 24.594, 17.904, 29.352,
28.023, 17.173, 22.587, 18.788, 16.125, 19.06, 42.654, 39.922,
32.427, 29.618, 22.848, 26.63, 21.103, 35.947, 28.197, 39.949,
46.188, 38.762, 26.383, 31.113, 26.897, 50.296, 26.654, 46.606,
38.366, 28.889, 29.061, 27.366, 24.467, 27.869, 20.865, 34.532,
24.214, 24.018, 19.901, 27.445, 24.431, 19.429, 16.79, 19.121,
13.611, 20.209, 20.882, 25.97, 59.214, 21.505, 26.201, 29.372,
19.278, 15.012), label = "CFCA: fibra (g/día)", format.spss = "F12.9", display_width = 12L),
colest = structure(c(292.292, 234.403, 311.061, 309.81, 389.591,
365.714, 210.857, 152.323, 375.468, 486.519, 459.621, 436.767,
760.616, 791.953, 477.64, 656.042, 417.236, 270.913, 424.126,
362.431, 357.637, 645.792, 433.148, 468.559, 636.255, 337.357,
427.762, 670.833, 554.944, 435.904, 459.898, 422.695, 374.214,
808.413, 448.603, 328.825, 360.321, 627.991, 496.343, 373.879,
330.504, 382.86, 408.308, 500.358, 443.013, 176.641, 476.749,
263.728, 425.362, 197.08, 357.832, 368.134, 299.696, 434.816,
323.69, 788.737, 418.956, 269.667, 423.579, 381.32, 295.858,
392.803, 329.393, 602.866, 450.496, 577.14, 551.928, 512.168,
387.182, 345.856, 203.316, 311.166, 344.272, 311.559, 305.653,
347.938, 267.418, 333.836, 315.937, 343.666, 306.224, 283.629,
248.75, 324.878, 197.84, 248.516, 395.779, 295.07, 295.512,
523.377, 387.157, 389.028, 398.556, 385.894, 550.661, 349.28,
348.609, 556.693, 290.876, 241.045, 222.341, 336.497, 252.388,
347.494, 312.018, 308.151, 141.061, 260.633, 182.94, 433.033,
218.847, 328.485, 270.994, 153.986, 308.897, 292.976, 312.58,
371.784, 402.76, 298.284, 174.367, 254.207, 470.33, 615.289,
253.661, 371.31, 119.607, 378.459, 301.708, 420.134, 363.415,
433.725, 479.324, 395.8, 411.285, 338.661, 303.392, 297.949,
434.388, 342.646, 303.311, 317.007, 251.65, 426.225, 718.114,
547.932, 393.587, 438.148, 445.87, 382.049, 320.85), label = "CFCA: colesterol (mg/día)", format.spss = "F13.9", display_width = 13L),
fit = structure(c(339.922, 315.542, 404.647, 364.088, 498.291,
244.423, 247.189, 375.655, 419.487, 388.218, 396.79, 326.238,
365.281, 353.282, 398.624, 365.927, 467.1, 475.742, 291.558,
425.783, 422.725, 698.814, 456.862, 424.385, 563.345, 391.33,
252.158, 441.997, 247.364, 388.587, 324.002, 277.878, 248.317,
530.604, 419.871, 403.888, 210.406, 143.809, 335.087, 244.036,
226.68, 206.883, 268.092, 360.66, 355.386, 320.801, 182.076,
273.722, 213.099, 244.464, 250.584, 223.602, 236.27, 354.855,
331.032, 433.879, 304.107, 361.363, 293.339, 259.354, 216.02,
293.871, 286.758, 405.324, 332.195, 383.396, 343.854, 407.924,
345.535, 340.41, 240.195, 238.605, 294.76, 195.426, 442.459,
279.637, 200.042, 389.739, 320.371, 373.194, 268.11, 350.152,
286.152, 312.187, 287.563, 414.003, 527.316, 272.864, 321.137,
294.131, 303.87, 361.447, 401.225, 290.593, 326.021, 221.007,
400.581, 330.183, 274.767, 221.96, 327.947, 331.23, 198.028,
403.069, 341.074, 203.165, 290.3, 396.478, 407.383, 435.858,
314.404, 454.332, 456.048, 172.123, 327.577, 357.056, 384.323,
442.078, 368.751, 327.79, 294.917, 300.832, 466.827, 347.018,
256.395, 572.791, 393.55, 477.508, 322.338, 402.796, 349.768,
216.704, 499.459, 276.158, 311.367, 184.701, 376.135, 234.138,
291.059, 243.338, 216.852, 216.491, 406.164, 306.246, 297.673,
484.018, 261.554, 263.217, 476.303, 279.066, 197.733), label = "CFCA: fitosteroles (g/día)", format.spss = "F13.9", display_width = 13L),
linolenico = structure(c(1.212, 0.981, 1.776, 1.011, 1.024,
0.938, 0.882, 1.248, 1.501, 1.785, 1.919, 1.322, 1.071, 2.138,
1.312, 1.527, 2.96, 0.984, 1.006, 1.864, 1.824, 2.527, 1.249,
1.6, 2.815, 1.25, 0.983, 1.648, 1.966, 1.581, 1.544, 1.039,
1.985, 2.962, 1.944, 1.736, 0.642, 0.73, 1.702, 0.886, 2.019,
0.673, 1.055, 1.143, 2.34, 0.963, 1.212, 1.005, 1.336, 1.637,
1.59, 0.704, 0.749, 1.286, 1.61, 1.552, 1.386, 1.857, 1.009,
1.073, 1.753, 1.075, 1.745, 2.108, 1.427, 1.861, 1.875, 2.068,
1.241, 1.516, 1.111, 0.615, 1.418, 0.496, 1.625, 1.103, 0.819,
1.483, 0.841, 0.745, 0.931, 0.951, 0.992, 0.996, 0.609, 0.796,
1.361, 1.063, 1.019, 1.095, 1.093, 1.085, 1.06, 0.811, 1.014,
0.673, 0.865, 1.199, 0.644, 0.836, 0.86, 0.557, 0.763, 0.665,
0.663, 0.635, 0.452, 1.514, 1.098, 1.187, 0.983, 0.786, 0.696,
0.783, 0.664, 0.797, 1.669, 1.136, 1.279, 1.19, 0.691, 0.726,
0.992, 0.971, 0.827, 0.926, 2.344, 0.833, 1.329, 3.173, 2.335,
0.926, 3.405, 1.474, 1.138, 0.52, 1.954, 0.896, 1.024, 1.538,
0.948, 1.456, 1.043, 1.857, 0.61, 2.193, 1.558, 1.047, 1.24,
1.267, 0.976), label = "CFCA: ac. linolenico (g/día)", format.spss = "F11.9", display_width = 11L)), row.names = c(NA,
-151L), class = c("tbl_df", "tbl", "data.frame"))
The label is an attribute of the single columns not of the dataframe. Hence do:
for (x in names(dat1)) {
print(attr(dat1[[x]], which = "label"))
}
#> [1] "CFCA: Hidratos de carbono (g/día)"
#> [1] "CFCA: proteinas (g/día)"
#> [1] "CFCA: Lípidos (g/día)"
#> [1] "CFCA: AGM-monoinsaturados (g/día)"
#> [1] "CFCA: AGP-poliinsaturados (g/día)"
#> [1] "CFCA: AGS-saturados (g/día)"
#> [1] "CFCA: gramos de alcohol puro (vino tinto, otros vinos, cerveza y destilados)"
#> [1] "CFCA: energia total (Kcal)"
#> [1] "CFCA: fibra (g/día)"
#> [1] "CFCA: colesterol (mg/día)"
#> [1] "CFCA: fitosteroles (g/día)"
#> [1] "CFCA: ac. linolenico (g/día)"

Change proportion of zoom in facet_zoom [ggplot2]

I would like to change the proportion of the graph covered by the "zoom". If we look at the following image we are at a ratio "graph:zoom" around 1:2 I would like instead a ratio 2:1. In other words, what I want is to reduce the height of the zoom. How should I proceed?
Here is my code
require(dplyr)
require(tidyverse)
require(ggforce)
HY <- DebitH %>%
ggplot(aes(x = Date, y = Débit_horaire)) +
geom_point(alpha = 6/10, size = 0.3, color = "blue") +
geom_line(alpha = 4/10, size = 0.5, color = "blue") +
labs(x = "Date", y = "Débit Horaire (m3/s)")+
theme_bw() +
theme(panel.grid.major.y = element_line(color = "grey",
size = 0.25,
linetype = 2),
legend.position = "none") +
facet_zoom(xlim = as.POSIXct(c("2021-01-15 00:00:00","2021-02-15 00:00:00")))
And here you will find a sample of the data
DebitH <-
structure(list(Date = structure(c(1610233200, 1610236800, 1610240400,
1610244000, 1610247600, 1610251200, 1610254800, 1610258400, 1610262000,
1610265600, 1610269200, 1610272800, 1610276400, 1610280000, 1610283600,
1610287200, 1610290800, 1610294400, 1610298000, 1610301600, 1610305200,
1610308800, 1610312400, 1610316000, 1610319600, 1610323200, 1610326800,
1610330400, 1610334000, 1610337600, 1610341200, 1610344800, 1610348400,
1610352000, 1610355600, 1610359200, 1610362800, 1610366400, 1610370000,
1610373600, 1610377200, 1610380800, 1610384400, 1610388000, 1610391600,
1610395200, 1610398800, 1610402400, 1610406000, 1610409600, 1610413200,
1610416800, 1610420400, 1610424000, 1610427600, 1610431200, 1610434800,
1610438400, 1610442000, 1610445600, 1610449200, 1610452800, 1610456400,
1610460000, 1610463600, 1610467200, 1610470800, 1610474400, 1610478000,
1610481600, 1610485200, 1610488800, 1610492400, 1610496000, 1610499600,
1610503200, 1610506800, 1610510400, 1610514000, 1610517600, 1610521200,
1610524800, 1610528400, 1610532000, 1610535600, 1610539200, 1610542800,
1610546400, 1610550000, 1610553600, 1610557200, 1610560800, 1610564400,
1610568000, 1610571600, 1610575200, 1610578800, 1610582400, 1610586000,
1610589600, 1610593200, 1610596800, 1610600400, 1610604000, 1610607600,
1610611200, 1610614800, 1610618400, 1610622000, 1610625600, 1610629200,
1610632800, 1610636400, 1610640000, 1610643600, 1610647200, 1610650800,
1610654400, 1610658000, 1610661600, 1610665200, 1610668800, 1610672400,
1610676000, 1610679600, 1610683200, 1610686800, 1610690400, 1610694000,
1610697600, 1610701200, 1610704800, 1610708400, 1610712000, 1610715600,
1610719200, 1610722800, 1610726400, 1610730000, 1610733600, 1610737200,
1610740800, 1610744400, 1610748000, 1610751600, 1610755200, 1610758800,
1610762400, 1610766000, 1610769600, 1610773200, 1610776800, 1610780400,
1610784000, 1610787600, 1610791200, 1610794800, 1610798400, 1610802000,
1610805600, 1610809200, 1610812800, 1610816400, 1610820000, 1610823600,
1610827200, 1610830800, 1610834400, 1610838000, 1610841600, 1610845200,
1610848800, 1610852400, 1610856000, 1610859600, 1610863200, 1610866800,
1610870400, 1610874000, 1610877600, 1610881200, 1610884800, 1610888400,
1610892000, 1610895600, 1610899200, 1610902800, 1610906400, 1610910000,
1610913600, 1610917200, 1610920800, 1610924400, 1610928000, 1610931600,
1610935200, 1610938800, 1610942400, 1610946000, 1610949600, 1610953200,
1610956800, 1610960400, 1610964000, 1610967600, 1610971200, 1610974800,
1610978400, 1610982000, 1610985600, 1610989200, 1610992800, 1610996400,
1.611e+09, 1611003600, 1611007200, 1611010800, 1611014400, 1611018000,
1611021600, 1611025200, 1611028800, 1611032400, 1611036000, 1611039600,
1611043200, 1611046800, 1611050400, 1611054000, 1611057600, 1611061200,
1611064800, 1611068400, 1611072000, 1611075600, 1611079200, 1611082800,
1611086400, 1611090000, 1611093600, 1611097200, 1611100800, 1611104400,
1611108000, 1611111600, 1611115200, 1611118800, 1611122400, 1611126000,
1611129600, 1611133200, 1611136800, 1611140400, 1611144000, 1611147600,
1611151200, 1611154800, 1611158400, 1611162000, 1611165600, 1611169200,
1611172800, 1611176400, 1611180000, 1611183600, 1611187200, 1611190800,
1611194400, 1611198000, 1611201600, 1611205200, 1611208800, 1611212400,
1611216000, 1611219600, 1611223200, 1611226800, 1611230400, 1611234000,
1611237600, 1611241200, 1611244800, 1611248400, 1611252000, 1611255600,
1611259200, 1611262800, 1611266400, 1611270000, 1611273600, 1611277200,
1611280800, 1611284400, 1611288000, 1611291600, 1611295200, 1611298800,
1611302400, 1611306000, 1611309600, 1611313200, 1611316800, 1611320400,
1611324000, 1611327600, 1611331200, 1611334800, 1611338400, 1611342000,
1611345600, 1611349200, 1611352800, 1611356400, 1611360000, 1611363600,
1611367200, 1611370800, 1611374400, 1611378000, 1611381600, 1611385200,
1611388800, 1611392400, 1611396000, 1611399600, 1611403200, 1611406800,
1611410400, 1611414000, 1611417600, 1611421200, 1611424800, 1611428400,
1611432000, 1611435600, 1611439200, 1611442800, 1611446400, 1611450000,
1611453600, 1611457200, 1611460800, 1611464400, 1611468000, 1611471600,
1611475200, 1611478800, 1611482400, 1611486000, 1611489600, 1611493200,
1611496800, 1611500400, 1611504000, 1611507600, 1611511200, 1611514800,
1611518400, 1611522000, 1611525600, 1611529200, 1611532800, 1611536400,
1611540000, 1611543600, 1611547200, 1611550800, 1611554400, 1611558000,
1611561600, 1611565200, 1611568800, 1611572400, 1611576000, 1611579600,
1611583200, 1611586800, 1611590400, 1611594000, 1611597600, 1611601200,
1611604800, 1611608400, 1611612000, 1611615600, 1611619200, 1611622800,
1611626400, 1611630000, 1611633600, 1611637200, 1611640800, 1611644400,
1611648000, 1611651600, 1611655200, 1611658800, 1611662400, 1611666000,
1611669600, 1611673200, 1611676800, 1611680400, 1611684000, 1611687600,
1611691200, 1611694800, 1611698400, 1611702000, 1611705600, 1611709200,
1611712800, 1611716400, 1611720000, 1611723600, 1611727200, 1611730800,
1611734400, 1611738000, 1611741600, 1611745200, 1611748800, 1611752400,
1611756000, 1611759600, 1611763200, 1611766800, 1611770400, 1611774000,
1611777600, 1611781200, 1611784800, 1611788400, 1611792000, 1611795600,
1611799200, 1611802800, 1611806400, 1611810000, 1611813600, 1611817200,
1611820800, 1611824400, 1611828000, 1611831600, 1611835200, 1611838800,
1611842400, 1611846000, 1611849600, 1611853200, 1611856800, 1611860400,
1611864000, 1611867600, 1611871200, 1611874800, 1611878400, 1611882000,
1611885600, 1611889200, 1611892800, 1611896400, 1611900000, 1611903600,
1611907200, 1611910800, 1611914400, 1611918000, 1611921600, 1611925200,
1611928800, 1611932400, 1611936000, 1611939600, 1611943200, 1611946800,
1611950400, 1611954000, 1611957600, 1611961200, 1611964800, 1611968400,
1611972000, 1611975600, 1611979200, 1611982800, 1611986400, 1611990000,
1611993600, 1611997200, 1612000800, 1612004400, 1612008000, 1612011600,
1612015200, 1612018800, 1612022400, 1612026000, 1612029600, 1612033200,
1612036800, 1612040400, 1612044000, 1612047600, 1612051200, 1612054800,
1612058400, 1612062000, 1612065600, 1612069200, 1612072800, 1612076400,
1612080000, 1612083600, 1612087200, 1612090800, 1612094400, 1612098000,
1612101600, 1612105200, 1612108800, 1612112400, 1612116000, 1612119600,
1612123200, 1612126800, 1612130400, 1612134000, 1612137600, 1612141200,
1612144800, 1612148400, 1612152000, 1612155600, 1612159200, 1612162800,
1612166400, 1612170000, 1612173600, 1612177200, 1612180800, 1612184400,
1612188000, 1612191600, 1612195200, 1612198800, 1612202400, 1612206000,
1612209600, 1612213200, 1612216800, 1612220400, 1612224000, 1612227600,
1612231200, 1612234800, 1612238400, 1612242000, 1612245600, 1612249200,
1612252800, 1612256400, 1612260000, 1612263600, 1612267200, 1612270800,
1612274400, 1612278000, 1612281600, 1612285200, 1612288800, 1612292400,
1612296000, 1612299600, 1612303200, 1612306800, 1612310400, 1612314000,
1612317600, 1612321200, 1612324800, 1612328400, 1612332000, 1612335600,
1612339200, 1612342800, 1612346400, 1612350000, 1612353600, 1612357200,
1612360800, 1612364400, 1612368000, 1612371600, 1612375200, 1612378800,
1612382400, 1612386000, 1612389600, 1612393200, 1612396800, 1612400400,
1612404000, 1612407600, 1612411200, 1612414800, 1612418400, 1612422000,
1612425600, 1612429200, 1612432800, 1612436400, 1612440000, 1612443600,
1612447200, 1612450800, 1612454400, 1612458000, 1612461600, 1612465200,
1612468800, 1612472400, 1612476000, 1612479600, 1612483200, 1612486800,
1612490400, 1612494000, 1612497600, 1612501200, 1612504800, 1612508400,
1612512000, 1612515600, 1612519200, 1612522800, 1612526400, 1612530000,
1612533600, 1612537200, 1612540800, 1612544400, 1612548000, 1612551600,
1612555200, 1612558800, 1612562400, 1612566000, 1612569600, 1612573200,
1612576800, 1612580400, 1612584000, 1612587600, 1612591200, 1612594800,
1612598400, 1612602000, 1612605600, 1612609200, 1612612800, 1612616400,
1612620000, 1612623600, 1612627200, 1612630800, 1612634400, 1612638000,
1612641600, 1612645200, 1612648800, 1612652400, 1612656000, 1612659600,
1612663200, 1612666800, 1612670400, 1612674000, 1612677600, 1612681200,
1612684800, 1612688400, 1612692000, 1612695600, 1612699200, 1612702800,
1612706400, 1612710000, 1612713600, 1612717200, 1612720800, 1612724400,
1612728000, 1612731600, 1612735200, 1612738800, 1612742400, 1612746000,
1612749600, 1612753200, 1612756800, 1612760400, 1612764000, 1612767600,
1612771200, 1612774800, 1612778400, 1612782000, 1612785600, 1612789200,
1612792800, 1612796400, 1612800000, 1612803600, 1612807200, 1612810800,
1612814400, 1612818000, 1612821600, 1612825200, 1612828800, 1612832400,
1612836000, 1612839600, 1612843200, 1612846800, 1612850400, 1612854000,
1612857600, 1612861200, 1612864800, 1612868400, 1612872000, 1612875600,
1612879200, 1612882800, 1612886400, 1612890000, 1612893600, 1612897200,
1612900800, 1612904400, 1612908000, 1612911600, 1612915200, 1612918800,
1612922400, 1612926000, 1612929600, 1612933200, 1612936800, 1612940400,
1612944000, 1612947600, 1612951200, 1612954800, 1612958400, 1612962000,
1612965600, 1612969200, 1612972800, 1612976400, 1612980000, 1612983600,
1612987200, 1612990800, 1612994400, 1612998000, 1613001600, 1613005200,
1613008800, 1613012400, 1613016000, 1613019600, 1613023200, 1613026800,
1613030400, 1613034000, 1613037600, 1613041200, 1613044800, 1613048400,
1613052000, 1613055600, 1613059200, 1613062800, 1613066400, 1613070000,
1613073600, 1613077200, 1613080800, 1613084400, 1613088000, 1613091600,
1613095200, 1613098800, 1613102400, 1613106000, 1613109600, 1613113200,
1613116800, 1613120400, 1613124000, 1613127600, 1613131200, 1613134800,
1613138400, 1613142000, 1613145600, 1613149200, 1613152800, 1613156400,
1613160000, 1613163600, 1613167200, 1613170800, 1613174400, 1613178000,
1613181600, 1613185200, 1613188800, 1613192400, 1613196000, 1613199600,
1613203200, 1613206800, 1613210400, 1613214000, 1613217600, 1613221200,
1613224800, 1613228400, 1613232000, 1613235600, 1613239200, 1613242800,
1613246400, 1613250000, 1613253600, 1613257200, 1613260800, 1613264400,
1613268000, 1613271600, 1613275200, 1613278800, 1613282400, 1613286000,
1613289600, 1613293200, 1613296800, 1613300400, 1613304000, 1613307600,
1613311200, 1613314800, 1613318400, 1613322000, 1613325600, 1613329200,
1613332800, 1613336400, 1613340000, 1613343600, 1613347200, 1613350800,
1613354400, 1613358000, 1613361600, 1613365200, 1613368800, 1613372400,
1613376000, 1613379600, 1613383200, 1613386800, 1613390400, 1613394000,
1613397600, 1613401200, 1613404800, 1613408400, 1613412000, 1613415600,
1613419200, 1613422800, 1613426400, 1613430000, 1613433600, 1613437200,
1613440800, 1613444400, 1613448000, 1613451600, 1613455200, 1613458800,
1613462400, 1613466000, 1613469600, 1613473200, 1613476800, 1613480400,
1613484000, 1613487600, 1613491200, 1613494800, 1613498400, 1613502000,
1613505600, 1613509200, 1613512800, 1613516400, 1613520000, 1613523600,
1613527200, 1613530800, 1613534400, 1613538000, 1613541600, 1613545200,
1613548800, 1613552400, 1613556000, 1613559600, 1613563200, 1613566800,
1613570400, 1613574000, 1613577600, 1613581200, 1613584800, 1613588400,
1613592000, 1613595600, 1613599200, 1613602800, 1613606400, 1613610000,
1613613600, 1613617200, 1613620800, 1613624400, 1613628000, 1613631600,
1613635200, 1613638800, 1613642400, 1613646000, 1613649600, 1613653200,
1613656800, 1613660400, 1613664000, 1613667600, 1613671200, 1613674800,
1613678400, 1613682000, 1613685600, 1613689200, 1613692800, 1613696400,
1613700000, 1613703600, 1613707200, 1613710800, 1613714400, 1613718000,
1613721600, 1613725200, 1613728800, 1613732400, 1613736000, 1613739600,
1613743200, 1613746800, 1613750400, 1613754000, 1613757600, 1613761200,
1613764800, 1613768400, 1613772000, 1613775600, 1613779200, 1613782800,
1613786400, 1613790000, 1613793600, 1613797200, 1613800800, 1613804400,
1613808000, 1613811600, 1613815200, 1613818800, 1613822400, 1613826000,
1613829600, 1613833200, 1613836800, 1613840400, 1613844000, 1613847600,
1613851200, 1613854800, 1613858400, 1613862000, 1613865600, 1613869200,
1613872800, 1613876400, 1613880000, 1613883600, 1613887200, 1613890800,
1613894400, 1613898000, 1613901600, 1613905200, 1613908800, 1613912400,
1613916000, 1613919600, 1613923200, 1613926800, 1613930400, 1613934000,
1613937600, 1613941200, 1613944800, 1613948400, 1613952000, 1613955600,
1613959200, 1613962800, 1613966400, 1613970000, 1613973600, 1613977200,
1613980800, 1613984400, 1613988000, 1613991600, 1613995200, 1613998800,
1614002400, 1614006000, 1614009600, 1614013200, 1614016800, 1614020400,
1614024000, 1614027600, 1614031200, 1614034800, 1614038400, 1614042000,
1614045600, 1614049200, 1614052800, 1614056400, 1614060000, 1614063600,
1614067200, 1614070800, 1614074400, 1614078000, 1614081600, 1614085200,
1614088800, 1614092400, 1614096000, 1614099600, 1614103200, 1614106800,
1614110400, 1614114000, 1614117600, 1614121200, 1614124800, 1614128400,
1614132000, 1614135600, 1614139200, 1614142800, 1614146400, 1614150000,
1614153600, 1614157200, 1614160800, 1614164400, 1614168000, 1614171600,
1614175200, 1614178800, 1614182400, 1614186000, 1614189600, 1614193200,
1614196800, 1614200400, 1614204000, 1614207600, 1614211200, 1614214800,
1614218400, 1614222000, 1614225600, 1614229200, 1614232800, 1614236400,
1614240000, 1614243600, 1614247200, 1614250800, 1614254400, 1614258000,
1614261600, 1614265200, 1614268800, 1614272400, 1614276000, 1614279600,
1614283200, 1614286800, 1614290400, 1614294000, 1614297600, 1614301200,
1614304800, 1614308400, 1614312000, 1614315600, 1614319200, 1614322800,
1614326400, 1614330000, 1614333600, 1614337200, 1614340800, 1614344400,
1614348000, 1614351600, 1614355200, 1614358800, 1614362400, 1614366000,
1614369600, 1614373200, 1614376800, 1614380400, 1614384000, 1614387600,
1614391200, 1614394800, 1614398400, 1614402000, 1614405600, 1614409200,
1614412800, 1614416400, 1614420000, 1614423600, 1614427200, 1614430800,
1614434400, 1614438000, 1614441600, 1614445200, 1614448800, 1614452400,
1614456000, 1614459600, 1614463200, 1614466800, 1614470400, 1614474000,
1614477600, 1614481200, 1614484800, 1614488400, 1614492000, 1614495600,
1614499200, 1614502800, 1614506400, 1614510000, 1614513600, 1614517200,
1614520800, 1614524400, 1614528000, 1614531600, 1614535200, 1614538800,
1614542400, 1614546000, 1614549600, 1614553200, 1614556800, 1614560400,
1614564000, 1614567600, 1614571200, 1614574800, 1614578400, 1614582000,
1614585600, 1614589200, 1614592800, 1614596400, 1614600000, 1614603600,
1614607200, 1614610800, 1614614400, 1614618000, 1614621600, 1614625200,
1614628800, 1614632400, 1614636000, 1614639600, 1614643200, 1614646800,
1614650400, 1614654000, 1614657600, 1614661200, 1614664800, 1614668400,
1614672000, 1614675600, 1614679200, 1614682800, 1614686400, 1614690000,
1614693600, 1614697200, 1614700800, 1614704400, 1614708000, 1614711600,
1614715200, 1614718800, 1614722400, 1614726000, 1614729600, 1614733200,
1614736800, 1614740400, 1614744000, 1614747600, 1614751200, 1614754800,
1614758400, 1614762000, 1614765600, 1614769200, 1614772800, 1614776400,
1614780000, 1614783600, 1614787200, 1614790800, 1614794400, 1614798000,
1614801600, 1614805200, 1614808800, 1614812400, 1614816000, 1614819600,
1614823200, 1614826800, 1614830400, 1614834000, 1614837600, 1614841200,
1614844800, 1614848400, 1614852000, 1614855600, 1614859200, 1614862800,
1614866400, 1614870000, 1614873600, 1614877200, 1614880800, 1614884400,
1614888000, 1614891600, 1614895200, 1614898800), tzone = "UTC", class = c("POSIXct",
"POSIXt")), Débit_horaire = c(5.052, 5.036, 5.009, 4.982, 4.958,
4.937, 4.917, 4.885, 4.858, 4.834, 4.804, 4.786, 4.769, 4.753,
4.741, 4.722, 4.713, 4.702, 4.689, 4.68, 4.669, 4.664, 4.658,
4.645, 4.623, 4.595, 4.567, 4.539, 4.51, 4.48, 4.443, 4.413,
4.381, 4.352, 4.327, 4.318, 4.291, 4.27, 4.268, 4.268, 4.265,
4.257, 4.254, 4.258, 4.27, 4.283, 4.295, 4.299, 4.303, 4.307,
4.305, 4.308, 4.32, 4.334, 4.355, 4.396, 4.457, 4.531, 4.624,
4.738, 4.879, 5.057, 5.28, 5.558, 5.863, 6.186, 6.562, 6.958,
7.327, 7.555, 7.638, 7.638, 7.577, 7.452, 7.291, 7.089, 6.867,
6.646, 6.469, 6.301, 6.161, 6.043, 5.946, 5.872, 5.807, 5.752,
5.702, 5.67, 5.658, 5.65, 5.647, 5.656, 5.692, 5.74, 5.786, 5.839,
5.91, 6.003, 6.124, 6.256, 6.374, 6.489, 6.596, 6.686, 6.72,
6.725, 6.706, 6.684, 6.646, 6.61, 6.578, 6.568, 6.554, 6.54,
6.529, 6.535, 6.538, 6.542, 6.553, 6.531, 6.547, 6.54, 6.534,
6.51, 6.504, 6.486, 6.462, 6.444, 6.429, 6.412, 6.395, 6.373,
6.365, 6.354, 6.344, 6.334, 6.326, 6.315, 6.303, 6.295, 6.295,
6.298, 6.296, 6.298, 6.289, 6.273, 6.258, 6.238, 6.225, 6.219,
6.21, 6.213, 6.216, 6.216, 6.22, 6.222, 6.229, 6.233, 6.236,
6.239, 6.238, 6.248, 6.258, 6.271, 6.292, 6.311, 6.33, 6.342,
6.348, 6.358, 6.36, 6.365, 6.366, 6.359, 6.353, 6.342, 6.333,
6.327, 6.33, 6.327, 6.319, 6.326, 6.332, 6.324, 6.34, 6.356,
6.373, 6.402, 6.443, 6.487, 6.537, 6.571, 6.582, 6.582, 6.563,
6.54, 6.515, 6.478, 6.442, 6.41, 6.372, 6.331, 6.304, 6.277,
6.246, 6.218, 6.205, 6.199, 6.193, 6.186, 6.187, 6.203, 6.233,
6.274, 6.315, 6.362, 6.4, 6.432, 6.459, 6.484, 6.512, 6.539,
6.544, 6.548, 6.543, 6.529, 6.525, 6.593, 6.638, 6.713, 6.831,
6.968, 7.142, 7.37, 7.656, 7.924, 8.117, 8.181, 8.262, 8.273,
8.25, 8.243, 8.183, 8.13, 8.073, 8.021, 7.953, 7.888, 7.819,
7.765, 7.733, 7.703, 7.675, 7.663, 7.661, 7.673, 7.701, 7.739,
7.8, 7.843, 7.902, 7.973, 8.06, 8.136, 8.216, 8.342, 8.429, 8.585,
8.694, 8.838, 9.026, 9.38, 9.815, 10.294, 10.786, 11.347, 11.859,
12.233, 12.637, 12.966, 13.283, 13.685, 13.908, 14.334, 14.721,
15.149, 15.581, 16.521, 17.244, 17.842, 18.501, 19.313, 20.18,
20.831, 21.707, 22.559, 23.352, 24.222, 25.223, 25.933, 26.78,
27.336, 28.231, 28.845, 29.255, 29.805, 30.559, 30.458, 30.79,
30.01, 30.098, 29.579, 29.405, 29.149, 28.992, 28.879, 28.596,
28.513, 27.866, 27.839, 27.721, 27.259, 27.385, 26.951, 26.759,
26.467, 26.188, 25.971, 25.564, 25.572, 25.392, 25.243, 25.029,
24.91, 24.723, 24.44, 24.286, 24.039, 23.907, 23.594, 23.454,
23.31, 23.152, 22.881, 22.773, 22.481, 22.202, 21.956, 21.718,
21.385, 21.212, 20.804, 20.526, 20.251, 19.879, 19.599, 19.357,
19.033, 18.914, 18.647, 18.546, 18.301, 18.163, 17.975, 17.763,
17.563, 17.43, 17.19, 17.061, 16.821, 16.673, 16.518, 16.312,
16.137, 16.007, 15.833, 15.698, 15.528, 15.349, 15.195, 15.025,
14.867, 14.718, 14.57, 14.357, 14.224, 14.021, 13.917, 13.79,
13.584, 13.405, 13.267, 13.115, 13, 12.925, 12.786, 12.678, 12.518,
12.44, 12.342, 12.221, 12.138, 12.006, 11.919, 11.84, 11.776,
11.701, 11.605, 11.54, 11.484, 11.371, 11.321, 11.252, 11.177,
11.123, 11.066, 11.028, 10.961, 10.917, 10.842, 10.808, 10.706,
10.66, 10.588, 10.534, 10.496, 10.439, 10.434, 10.407, 10.393,
10.326, 10.289, 10.243, 10.188, 10.134, 10.09, 10.07, 10.039,
10.089, 10.19, 10.407, 10.695, 11.179, 11.769, 12.402, 13.126,
13.95, 14.598, 15.244, 15.748, 16.186, 16.655, 16.938, 17.177,
17.321, 17.365, 17.337, 17.256, 17.256, 17.163, 17.1, 17.143,
17.363, 17.495, 17.883, 18.542, 19.075, 19.628, 20.129, 20.61,
20.944, 21.508, 22.023, 22.557, 23.16, 23.827, 24.4, 24.849,
25.24, 25.387, 25.282, 25.309, 25.296, 25.562, 25.898, 26, 26.274,
26.225, 26.295, 26.394, 26.274, 26.071, 26.072, 26.035, 26.211,
26.383, 26.303, 26.501, 26.474, 26.513, 26.728, 26.825, 27.036,
27.121, 27.443, 27.65, 27.775, 27.689, 27.79, 28.197, 27.82,
28.144, 27.998, 27.789, 27.686, 26.934, 26.954, 26.612, 26.222,
26.192, 25.722, 25.616, 25.531, 25.292, 25.189, 24.797, 24.832,
24.664, 24.525, 24.343, 24.419, 24.284, 24.044, 24.054, 23.818,
23.651, 23.619, 23.587, 23.589, 23.397, 23.314, 23.144, 22.834,
22.846, 22.571, 22.437, 22.204, 22.036, 21.734, 21.588, 21.283,
21.018, 20.798, 20.394, 20.138, 19.926, 19.679, 19.477, 19.19,
18.914, 18.683, 18.514, 18.243, 18.11, 17.981, 17.873, 17.723,
17.655, 17.56, 17.496, 17.444, 17.353, 17.342, 17.289, 17.221,
17.182, 17.05, 16.966, 16.87, 16.649, 16.514, 16.457, 16.305,
16.172, 16.121, 16.223, 16.198, 16.412, 16.695, 16.984, 17.443,
17.808, 18.403, 19.015, 19.509, 19.919, 20.47, 20.922, 21.258,
21.561, 21.636, 21.557, 21.321, 21.223, 20.979, 20.834, 20.559,
20.501, 20.434, 20.312, 20.226, 20.08, 19.971, 19.846, 19.8,
19.712, 19.652, 19.59, 19.502, 19.442, 19.378, 19.261, 19.197,
19.193, 19.159, 19.119, 19.053, 18.966, 18.916, 18.877, 18.774,
18.71, 18.623, 18.501, 18.422, 18.277, 18.145, 18.057, 17.947,
17.828, 17.73, 17.56, 17.472, 17.347, 17.187, 17.019, 16.896,
16.784, 16.676, 16.496, 16.395, 16.255, 16.154, 16.125, 16.037,
15.981, 15.889, 15.897, 15.811, 15.786, 15.723, 15.584, 15.535,
15.433, 15.333, 15.291, 15.213, 15.091, 15.02, 14.928, 14.845,
14.804, 14.916, 15.406, 15.627, 16, 16.414, 16.809, 17.147, 17.457,
17.723, 17.973, 18.175, 18.371, 18.551, 18.613, 18.581, 18.389,
18.036, 17.58, 17.179, 16.751, 16.425, 16.129, 15.928, 15.791,
15.61, 15.487, 15.389, 15.363, 15.355, 15.312, 15.326, 15.301,
15.324, 15.291, 15.266, 15.184, 15.102, 15.056, 15.008, 14.947,
14.899, 14.84, 14.82, 14.819, 14.781, 14.766, 14.768, 14.715,
14.696, 14.631, 14.54, 14.455, 14.344, 14.209, 14.103, 14.027,
13.931, 13.848, 13.769, 13.662, 13.611, 13.493, 13.424, 13.358,
13.286, 13.232, 13.132, 13.034, 12.98, 12.937, 12.86, 12.76,
12.694, 12.604, 12.561, 12.466, 12.361, 12.257, 12.138, 12.025,
11.95, 11.863, 11.767, 11.675, 11.554, 11.473, 11.38, 11.287,
11.198, 11.125, 11.074, 11.007, 10.96, 10.906, 10.85, 10.768,
10.693, 10.62, 10.513, 10.435, 10.33, 10.226, 10.147, 10.045,
9.956, 9.844, 9.758, 9.669, 9.596, 9.542, 9.488, 9.453, 9.423,
9.359, 9.331, 9.332, 9.307, 9.279, 9.242, 9.204, 9.147, 9.079,
8.982, 8.892, 8.814, 8.74, 8.659, 8.602, 8.531, 8.45, 8.412,
8.35, 8.284, 8.243, 8.202, 8.168, 8.133, 8.111, 8.08, 8.062,
8.044, 8.024, 8.007, 7.975, 7.924, 7.867, 7.782, 7.71, 7.624,
7.544, 7.463, 7.373, 7.297, 7.233, 7.166, 7.103, 7.052, 7.024,
6.985, 6.938, 6.917, 6.914, 6.909, 6.893, 6.9, 6.903, 6.917,
6.891, 6.853, 6.803, 6.749, 6.694, 6.639, 6.565, 6.483, 6.42,
6.37, 6.32, 6.282, 6.252, 6.226, 6.204, 6.214, 6.215, 6.166,
6.155, 6.134, 6.133, 6.158, 6.175, 6.181, 6.175, 6.167, 6.148,
6.116, 6.082, 6.046, 6.019, 5.983, 5.962, 5.933, 5.912, 5.891,
5.864, 5.85, 5.831, 5.822, 5.806, 5.806, 5.813, 5.828, 5.856,
5.876, 5.907, 5.931, 5.97, 6.023, 6.055, 6.072, 6.069, 6.06,
6.052, 6.028, 6.015, 5.987, 5.965, 5.937, 5.902, 5.869, 5.836,
5.798, 5.77, 5.756, 5.767, 5.812, 5.869, 5.935, 6.029, 6.119,
6.188, 6.235, 6.292, 6.332, 6.383, 6.455, 6.575, 6.634, 6.617,
6.543, 6.441, 6.336, 6.241, 6.162, 6.08, 6.011, 5.942, 5.885,
5.83, 5.785, 5.756, 5.729, 5.691, 5.666, 5.628, 5.581, 5.531,
5.478, 5.431, 5.383, 5.327, 5.278, 5.23, 5.193, 5.159, 5.119,
5.086, 5.057, 5.034, 5.016, 4.995, 4.912, 4.963, 4.987, 5.019,
5.099, 5.21, 5.313, 5.476, 5.692, 5.883, 6.152, 6.457, 6.633,
6.623, 6.445, 6.218, 5.996, 5.813, 5.667, 5.548, 5.461, 5.379,
5.303, 5.24, 5.168, 5.122, 5.078, 5.044, 5.004, 4.973, 4.943,
4.911, 4.875, 4.848, 4.823, 4.794, 4.772, 4.75, 4.727, 4.711,
4.693, 4.668, 4.652, 4.639, 4.627, 4.617, 4.608, 4.593, 4.573,
4.56, 4.549, 4.544, 4.541, 4.532, 4.512, 4.504, 4.493, 4.474,
4.465, 4.45, 4.438, 4.417, 4.407, 4.396, 4.387, 4.381, 4.375,
4.361, 4.351, 4.337, 4.334, 4.33, 4.325, 4.32, 4.311, 4.312,
4.305, 4.291, 4.274, 4.257, 4.248, 4.233, 4.23, 4.222, 4.216,
4.215, 4.2, 4.19, 4.184, 4.177, 4.172, 4.161, 4.152, 4.145, 4.141,
4.136, 4.133, 4.126, 4.114, 4.099, 4.087, 4.078, 4.064, 4.05,
4.043, 4.035, 4.025, 4.013, 4.002, 3.989, 3.979, 3.97, 3.966,
3.958, 3.943, 3.94, 3.932, 3.928, 3.918, 3.91, 3.901, 3.88, 3.874,
3.866, 3.849, 3.831, 3.818, 3.804, 3.795, 3.787, 3.781, 3.779,
3.773, 3.767, 3.763, 3.757, 3.754, 3.751, 3.745, 3.738, 3.729,
3.731, 3.724, 3.723, 3.72, 3.72, 3.709, 3.707, 3.709, 3.707,
3.685, 3.668, 3.667, 3.682, 3.671, 3.652, 3.644, 3.635, 3.632,
3.626, 3.617, 3.614, 3.604, 3.598, 3.591, 3.586, 3.58, 3.578,
3.57, 3.564, 3.558, 3.547, 3.534, 3.524, 3.514, 3.499, 3.481,
3.476, 3.459, 3.449, 3.44, 3.43, 3.414, 3.41, 3.411, 3.417, 3.431,
3.455, 3.496, 3.53, 3.568, 3.604, 3.645, 3.685, 3.717, 3.728,
3.727, 3.69, 3.635, 3.592, 3.558, 3.52, 3.498, 3.472, 3.44, 3.413,
3.393, 3.37, 3.353, 3.321, 3.287, 3.261, 3.24, 3.237, 3.23, 3.227,
3.225, 3.223, 3.233, 3.242, 3.247, 3.234, 3.208, 3.181, 3.154,
3.134, 3.123, 3.109, 3.097, 3.091, 3.091, 3.085, 3.08, 3.074,
3.073, 3.07, 3.046, 3.052, 3.053, 3.049, 3.046, 3.029, 3.028,
3.018, 3.003, 2.997, 2.981, 2.971, 2.957, 2.946, 2.937, 2.931,
2.926, 2.916, 2.907, 2.904, 2.893, 2.887, 2.876, 2.866, 2.861,
2.854, 2.851, 2.85, 2.855, 2.865, 2.86, 2.859, 2.855, 2.843,
2.838, 2.828, 2.806, 2.793, 2.782, 2.775, 2.77, 2.759, 2.752,
2.744, 2.737, 2.729, 2.723, 2.719, 2.711, 2.703, 2.693, 2.697,
2.7, 2.699, 2.695, 2.693, 2.693, 2.692, 2.69, 2.681, 2.67, 2.658,
2.642, 2.637, 2.63, 2.623, 2.614, 2.611, 2.604, 2.604, 2.598,
2.591, 2.593, 2.594, 2.591, 2.585, 2.602, 2.601, 2.597, 2.59,
2.583, 2.582, 2.569, 2.562, 2.551, 2.544, 2.536, 2.537, 2.523,
2.516, 2.513, 2.515, 2.516, 2.516, 2.519, 2.518, 2.517, 2.516,
2.52, 2.534, 2.566, 2.607, 2.625, 2.675, 2.703, 2.742, 2.782,
2.827, 2.956)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-1297L))
You could set the size of the zoom panel relative to the main plot via the argument zoom.size which defaults to 2, i.e. the zoom panel is twice the size of the main panel. Hence, to achieve your desired result set zoom.size=.5:
require(dplyr)
require(tidyverse)
require(ggforce)
DebitH %>%
ggplot(aes(x = Date, y = Débit_horaire)) +
geom_point(alpha = 6 / 10, size = 0.3, color = "blue") +
geom_line(alpha = 4 / 10, size = 0.5, color = "blue") +
labs(x = "Date", y = "Débit Horaire (m3/s)") +
theme_bw() +
theme(
panel.grid.major.y = element_line(
color = "grey",
size = 0.25,
linetype = 2
),
legend.position = "none"
) +
facet_zoom(xlim = as.POSIXct(c("2021-01-15 00:00:00", "2021-02-15 00:00:00")), zoom.size = .5)

Colour a Q-Q plot comparing two distributions by quartiles in R

I am trying to construct a Q-Q plot comparing two distributions, with the 99th percentile colored like the following example:
However I am not sure how to achieve this, here is a subset of my data:
dfw <- structure(list(Date.Time = structure(c(848502000, 848509200,
848512800, 848520000, 848523600, 848530800, 848534400, 848541600,
848545200, 848552400, 848556000, 848563200, 848566800, 848574000,
848577600, 848588400, 848595600, 848599200, 848606400, 848610000,
848617200, 848620800, 848628000, 848631600, 848638800, 848642400,
848649600, 848653200, 848660400, 848664000, 848674800, 848682000,
848685600, 848692800, 848696400, 848703600, 848707200, 848714400,
848718000, 848725200, 848728800, 848736000, 848739600, 848746800,
848750400, 848761200, 848768400, 848772000, 848779200, 848782800,
848790000, 848793600, 848800800, 848804400, 848811600, 848815200,
848822400, 848826000, 848833200, 848847600, 848854800, 848858400,
848865600, 848869200, 848876400, 848880000, 848887200, 848890800,
848898000, 848901600, 848908800, 848912400, 848919600, 848923200,
848934000, 848941200, 848944800, 848952000, 848955600, 848962800,
848966400, 848973600, 848977200, 848984400, 848988000, 848995200,
848998800, 849006000, 849009600, 853682400, 853686000, 853714800,
853718400, 853725600, 853729200, 853736400, 853750800, 853758000,
853761600, 853768800), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
Hs.Mod = c(1.5960001, 1.5600001, 1.5480001, 1.552, 1.552,
1.534, 1.5180001, 1.462, 1.4260001, 1.3740001, 1.36, 1.3340001,
1.3080001, 1.2720001, 1.256, 1.218, 1.212, 1.21, 1.2060001,
1.2160001, 1.248, 1.25, 1.25, 1.264, 1.3560001, 1.394, 1.3700001,
1.332, 1.2900001, 1.2800001, 1.268, 1.2800001, 1.2800001,
1.3240001, 1.3240001, 1.286, 1.2540001, 1.19, 1.172, 1.1700001,
1.1860001, 1.2340001, 1.274, 1.3640001, 1.4120001, 1.58,
1.6580001, 1.6660001, 1.682, 1.748, 1.9280001, 1.9800001,
2.026, 2.052, 2.214, 2.328, 2.4320002, 2.39, 2.2180002, 1.9080001,
1.792, 1.7400001, 1.7140001, 1.7140001, 1.692, 1.6680001,
1.608, 1.58, 1.536, 1.524, 1.5640001, 1.5760001, 1.6100001,
1.6240001, 1.6120001, 1.5920001, 1.58, 1.542, 1.5200001,
1.48, 1.4640001, 1.4260001, 1.406, 1.386, 1.3820001, 1.34,
1.312, 1.268, 1.248, 1.8080001, 1.7960001, 1.644, 1.6420001,
1.6600001, 1.6880001, 1.7820001, 2.138, 2.2740002, 2.2940001,
2.252), Hs.Obs = c(1.741, 1.524, 1.618, 1.658, 1.697, 1.822,
1.792, 1.463, 1.433, 1.376, 1.208, 1.299, 1.255, 1.304, 1.328,
1.182, 1.282, 1.293, 1.228, 1.281, 1.45, 1.356, 1.501, 1.5,
1.356, 1.477, 1.408, 1.544, 1.497, 1.768, 2.04, 2.074, 2.042,
2.147, 2.224, 2.022, 2.017, 2.047, 2.353, 2.597, 2.838, 2.67,
2.762, 2.687, 2.734, 2.738, 2.938, 2.795, 2.549, 2.669, 2.447,
2.676, 2.577, 2.383, 2.362, 2.284, 2.341, 2.33, 2.397, 2.498,
2.317, 2.373, 2.377, 2.362, 2.218, 2.226, 1.97, 2.087, 1.874,
2.116, 2.022, 1.886, 2.046, 1.879, 1.638, 1.677, 1.638, 1.647,
1.551, 1.596, 1.591, 1.384, 1.345, 1.522, 1.469, 1.503, 1.459,
1.327, 1.453, 2.448, 2.235, 2.104, 1.958, 2.118, 2.209, 2.034,
2.229, 2.505, 2.163, 2.372)), row.names = c(NA, 100L), class = "data.frame")
Code to make the Q-Q plot:
ggplot(data=dfw, aes(x=sort(Hs.Obs), y=sort(Hs.Mod))) + geom_point(shape = 1, size =2) + xlab('Obs') + ylab('Model')+
theme_bw()+
geom_abline(linetype=2)
Code attempting to colour the 99th percentile:
ggplot(data=dfw, aes(x=sort(Hs.Obs), y=sort(Hs.Mod), col=cut(Hs.Mod,quantile(Hs.Mod, probs = .99)))) +
geom_point(shape = 1, size =2) + xlab('Obs') + ylab('Model')+
theme_bw()+
geom_abline(linetype=2)
Resulting plot:
I'm looking for some help to sort this out, as the attempts I have tried aren't working.
Thanks in advance!
try this
dfw %>%
mutate(qq = quantile(Hs.Mod, probs = c(0.99)),
qq_gt99 = ifelse(qq<= Hs.Mod, 1, 0)) %>%
ggplot(aes(x=Hs.Mod, y = Hs.Obs, col= as.factor(qq_gt99))) + geom_point()
if you order the observations first
dfw %>%
mutate(mod_ordered = sort(Hs.Mod),
obs_ordered = sort(Hs.Obs),
qq = quantile(mod_ordered, probs = c(0.99)),
qq_gt99 = ifelse(qq<= mod_ordered, 1, 0)) %>%
ggplot(aes(x=mod_ordered, y = obs_ordered, col=
as.factor(qq_gt99))) + geom_point()
Im using the dplyr library to filter() the data by quantile():
The code:
library(dplyr)
library(ggplot2)
ggplot()+
geom_point(data=filter(dfw,Hs.Obs>quantile(dfw$Hs.Mod,.99)),aes(x=sort(Hs.Obs),y=sort(Hs.Mod), col="Cuantile 99%"))+
geom_point(data=filter(dfw,Hs.Obs<quantile(dfw$Hs.Mod,.99)),aes(x=sort(Hs.Obs),y=sort(Hs.Mod), col="Cuantile 1-98%"))+
geom_abline(linetype=2)+xlab('Obs') + ylab('Model')+ theme_bw()

Problem with ggplot: labels and error bars overlap

I made a barplot with error bars and labels written on the bars.
My problem is: I want the labels to appear on the bars and also next to the error bars. That is, I don't want labels and error bars to overlap.
An example with my code:
i <- data.frame(
nbr =c(15.18 ,11.53 ,13.37 ,9.2, 10.9, 12.23 ,9.53, 9.81, 7.86, 12.79,
22.03 ,17.64 ,18.1, 16.78 ,17.53 ,16.97 ,17.76 ,18.35 ,12.82 ,20.91,
22.09 ,19.18 ,17.54 ,18.45 ,19.83 ,16.99 ,19.69 ,19.45 ,13.07 ,21.41,
12.13 ,9.76, 10.79 ,10.74 ,12.43 ,9.65, 12.18 ,11.63 ,6.74, 12.31,
17.5, 14.75 ,15.2, 13.89 ,15.24 ,17.43 ,15.22 ,14.04,9.49, 15.86,
8.09, 5.86, 6.68, 7.34, 8.01, 6.35, 8.4, 7.4, 3.88, 6.92 ),
SD = c(4.46, 4.19, 2.27, 2.19, 5.10, 7.25, 8.42, 6.47, 6.04, 7.48, 6.38, 6.05, 3.58, 3.85,
6.94, 6.87, 6.32, 4.28, 4.10, 7.34, 7.46, 6.62, 4.28, 5.24, 8.00, 8.10, 7.73, 5.18,
5.53, 7.96, 7.46, 7.05, 4.47, 4.73, 8.15, 6.95, 5.88, 3.20, 4.01, 7.34, 7.24, 6.98,
5.98, 4.53, 4.22, 7.21, 4.02, 4.30, 1.96, 2.11, 4.98, 7.16, 8.45, 6.39, 6.20, 7.03,
6.10, 6.42, 3.77, 3.53),
x2=rep(c("a", "b", "c", "d", "e", "f", "g",
"h", "i", "j"),6),
s = c(rep(c(rep(c("3"),10),
rep(c("4"),10),
rep(c("5"),10),
rep(c("6"),10),
rep(c("7"),10),
rep(c("8"),10)),1)))
ii <- i[order(i$s, i$nbr ), ]
sn <- factor(x = 1:60, labels = ii$x2)
ii$sn <- sn
scale_x_reordered <- function(..., sep = "___") {
reg <- paste0(sep, ".+$")
ggplot2::scale_x_discrete(labels = function(x) gsub(reg, "", x), ...)
}
reorder_within <- function(x, by, within, fun = mean, sep = "___", ...) {
new_x <- paste(x, within, sep = sep)
stats::reorder(new_x, by, FUN = fun)
}
dummy2 <- data.frame(s = levels(i$s)[-1], Z = c( 4,16,16,8,4))
dummy2$s <- factor(dummy2$s)
ggplot(ii, aes(reorder_within(sn, nbr, s), nbr,
label =x2)) +
geom_bar(stat = 'identity') +
geom_text(aes(y = 0,fontface=2), angle = 90, hjust = -.05, size = 4)+
scale_x_reordered() +
facet_wrap(.~ s, scales = "free_x", ncol=2)+
#geom_text(aes(label=nbr), vjust=1.6, color="white", size=3.5)+
theme(axis.text.x = element_blank(),
axis.title=element_text(size=16),
axis.text=element_text(face = "bold"),
strip.text.x = element_text(size = 14,face="bold")
)+ geom_errorbar(aes(reorder_within(sn, nbr, s),ymin=nbr-SD, ymax=nbr+SD), width=.2, position=position_dodge(.9))
Example of expected parcel:
I want all the labels to be written next to the error bars on the bars.
Thanks for your help !
I found this solution and wanted to share it with you:
geom_text(aes(y = 0,fontface=2), angle = 90, vjust = -1, hjust = -.05, size = 4)

Remove margin inside plot pf ggplot2

this is my script and the associated plot:
library(ggplot2)
library(reshape)
df <- structure(list(ID = structure(1:19, .Label = c("2818/22/0834",
"2818/22/0851", "2818/22/0853", "2818/22/0886", "B0F", "B12T",
"B1T", "B21T", "B22F", "B26T", "B33F", "B4F", "P1", "P21", "P24",
"P25", "P27", "P28", "P29"), class = "factor"), K = c(0.089,
0.094, 0.096, 0.274, 0.09, 0.312, 0.33, 0.178, 0.05, 0.154, 0.083,
0.098, 0.035, 0.084, 0.053, 0.061, 0.043, 0.094, 0.101), Na = c(2.606,
3.822, 4.977, 2.522, 15.835, 83.108, 52.041, 41.448, 11.849,
40.531, 5.854, 10.151, 3.52, 8.445, 5.273, 7.246, 6.177, 14.813,
15.569), Cl = c(3.546, 6.181, 8.422, 3.733, 14.685, 96.911, 65.518,
79.01, 10.349, 53.361, 6.12, 10.832, 2.313, 10.312, 5.641, 8.708,
6.138, 12.302, 20.078), Mg = c(1.487, 1.773, 1.992, 1.143, 2.991,
1.678, 2.23, 3.288, 1.148, 2.428, 3.428, 2.729, 0.777, 2.554,
2.374, 4.075, 1.993, 1.881, 3.034), Ca = c(5.529, 6.205, 6.59,
4.099, 10.631, 4.564, 6.652, 13.374, 4.332, 10.542, 11.194, 10.053,
2.969, 7.73, 8.163, 11.539, 6.166, 5.968, 9.299), SO4 = c(0.663,
0.831, 0.607, 0.882, 9.013, 0.896, 0.652, 0.021, 1.446, 0.012,
8.832, 6.665, 1.003, 2.575, 3.685, 7.121, 3.64, 5.648, 2.397),
HCO3 = c(7.522, 5.498, 6.15, 5.242, 8.582, 4.067, 5.65, 9.364,
5.435, 8.068, 9.054, 8.326, 4.805, 7.235, 7.488, 9.234, 6.352,
6.98, 8.34)), .Names = c("ID", "K", "Na", "Cl", "Mg", "Ca",
"SO4", "HCO3"), class = "data.frame", row.names = c(NA, -19L))
df_melted<-melt(df, na.rm=T)
ggplot(df_melted, aes(variable, value, group=ID, color=ID))+
geom_point(size=2) +
geom_line() +
theme(legend.position="none") +
scale_y_log10(breaks=seq(0, 100, 10))
Is there a way to remove the spaces at the beginning and at the end of the plot? I tried with xlim but the problem is that the x variable is not a numerical variable, so, something like xlim(c("K", "HCO3")) doesn't work.
This is a discrete scale, but you can still used the expand argument as follows. Whether the output looks acceptable or not is another matter. Play with the c(0,0) values until you find something that suits. Using 0.1 for the second value gives a slightly better plot, in my view...
ggplot(df_melted, aes(variable, value, group=ID, color=ID))+
geom_point(size=2) +
geom_line() +
theme(legend.position="none") +
scale_y_log10(breaks=seq(0, 100, 10)) +
scale_x_discrete(expand = c(0,0))

Resources