Problems specifying limits and breaks in scale_y_continuous - r

I'm trying to specify the limits and breaks for primary y-axis and secondary y-axis. Even when I specify the limit and range it is not reflected in the plot.
This is my data:
structure(c(39.571, 36.571, 34, 36.143, 36, 36.571, 38.286, 34.429,
34.429, 34.714, 33, 30.857, 29.429, 26.429, 25.571, 23.714, 22,
21.714, 24.286, 29.857, 38, 48.429, 58.714, 70.857, 79, 87, 92,
99.714, 98.714, 99.571, 101.571, 104.714, 108.857, 112.286, 109,
109.714, 112.714, 112.143, 114.857, 110.857, 107.143, 107.286,
108.143, 107.857, 104, 101.286, 99.571, 105.143, 103.143, 103.286,
98.714, 96, 92.714, 91.286, 84, 83.143, 81.429, 81.286, 82.571,
82.857, 80.429, 77.857, 75.429, 75.143, 73.286, 74.571, 74.429,
77, 86.286, 94.857, 96.286, 112.143, 125.286, 137.714, 150.571,
155, 159.286, 177.714, 175.429, 181.286, 191.143, 181.429, 181.714,
186.857, 176.857, 174, 158.714, 137.143, 137.429, 131.571, 116.571,
103.571, 90.857, 81.571, 74.571, 67.143, 54.286, 46.571, 44,
42.571, 38.286, 37, 34.429, 32.857, 30, 27, 50779.714, 56225.286,
44278.714, 45877.857, 47517.571, 54886.857, 53798.714, 41703.429,
34562.286, 46345.429, 42748.571, 37479, 29665, 28040.714, 26415.857,
29700, 28627.286, 26928.429, 26814, 24773, 22764.143, 22051.571,
16954.286, 15219.571, 14114.571, 12616, 11874.143, 11393.143,
11292, 11069.571, 10708.714, 10678.143, 10497.714, 10555.571,
10725, 10590.286, 11400.714, 11464.571, 11619.571, 11902.143,
10061.857, 12137.714, 12106.429, 11766.143, 12201.143, 12847.714,
13207.143, 15696.857, 13885.286, 14280.714, 14310.143, 13927,
14401.286, 15009.429, 12961.571, 12393.429, 11985.714, 11933.286,
13572.714, 13333.286, 13202.714, 13256, 13780.714, 13920.429,
15253.429, 15070.286, 12243.429, 15389.857, 17777.143, 18269.857,
18231.571, 18068, 17882.286, 20907.571, 18093.286, 15252.286,
18318.143, 18315.571, 18917.714, 19338.429, 19521.143, 19757,
23133.286, 20423.429, 20495.143, 20244.857, 20255.429, 17110.571,
20420.714, 20438, 20552.143, 20564.714, 20735.286, 20657.429,
20560.857, 20434, 19940.143, 20008.714, 20002.286, 19366.429,
19262.429, 22270.429, 15850.143, 12969.286, 10271.857, 18361.714,
17048.571, 16244.571, 18427.143, 18974, 19465.143, 19474.857,
19754, 17078.286, 19794.571, 18497.571, 18808.571, 18728.714,
18802.714, 17188.857, 18340, 18294.857, 18484.286, 18384.714,
18432.143, 18322.429, 18488.714, 18470.857, 17062.143, 18342.143,
18043.571, 17790.143, 17786.286, 17617.714, 17584.286, 18783.714,
17439.143, 17673.429, 16980.714, 18735.143, 19837, 18584.857,
19227.286, 19486.429, 20792.571, 22399.286, 21780.143, 22322.857,
22465, 23116.714, 24210.286, 23845.286, 24647.143, 24631, 23450.286,
25279.571, 25612.143, 25757, 25226.714, 20842.429, 19674.571,
20525.286, 19775, 17391.143, 19574.857, 18560.571, 18557.286,
18185.714, 17893.857, 17653.286, 17493.714, 14296.429, 18005.857,
20428.714, 22131.714, 20787.714, 20700, 21965.857, 21809.143,
18382, 16884.286, 15740.429, 17245.571, 16895.143, 14755.571,
15246.571, 13380, 14490.429, 14149.429, 13948.571, 13610.571,
13204, 11294.429, 12238.143, 11733.143, 12460, 11127.143, 11095.857,
10853.429, 10403.571, 10139.143, 9854, 8584.571, 9430.571, 9142.286,
8862, 8399, 7955.571, 7721.571, 7414.714, 7153, 7221.857, 26971.286,
27864.429, 28658.714, 29754.857, 30715.286, 31865.714, 32252.714,
32683.571, 33662, 34006.714, 34503.143, 34946, 34580.286, 34775,
35072.571, 34658.429, 34846.857, 34589.571, 34066.286, 33711.286,
32905.571, 32273.429, 30992.857, 29788.857, 28763.857, 27492.857,
26285.857, 25187.143, 24251.286, 23697, 22962.286, 22136.286,
21531.714, 20779.857, 20528.571, 20143.286, 19499, 18362.714,
17474, 16704.857, 16540.571, 16404.857, 16168.571, 16168.857,
16856.429, 17032.857, 16557.857, 16044, 15639, 15473.857, 15256.286,
14733.429, 14705.714, 15225.143, 14382.286, 13501.571, 13174.714,
12873.857, 13201.571, 13978.429, 14431.714, 14634.571, 15392.857,
15709.571, 16304.571, 16893.714, 16170.714, 15502, 16666.429,
17292.143, 17539.571, 17377.714, 16727.286, 16559.714, 16361.857,
15838.143, 14969.429, 14440.143, 13905, 13590, 13134.571, 12775.857,
12350.286, 12219.429, 12181.429, 12194, 12425.857, 12469.429,
12459.429, 12371.143, 12317, 12226.429, 12093.143, 11806.143,
11705.571, 11798.429, 11902.143, 11958, 11964.286, 12101.571,
12068, 12279.571, 12233.857, 12245.857, 12163, 12073.714, 20339.571,
20375.429, 21129.286, 20890.429, 20450.571, 20450.571, 20450.571,
20074.429, 17407.429, 19043, 18700.571, 18537, 18537, 18537,
16520.857, 18400.714, 15376, 14907.714, 14019.857, 14019.857,
14019.857, 12250.286, 12117.286, 11389.286, 10825.857, 10211.143,
10211.143, 10211.143, 9367.286, 8800, 8672.714, 8363.857, 8062.714,
8062.714, 8062.714, 7734.429, 6554.857, 6618, 6307.714, 6561.143,
6561.143, 6561.143, 7079.429, 8554.857, 8741.286, 9337.857, 9523,
9523, 9523, 9623.571, 9670.143, 9857, 9932.857, 8245, 8245, 8245,
8594.857, 9085.571, 9704.143, 10473.429, 10473.429, 10473.429,
10473.429, 11347.286, 12720.286, 10332.286, 13805.571, 17442.143,
17442.143, 17442.143, 21848.286, 22096.571, 27649.286, 26723.286,
28829.143, 28829.143, 28829.143, 32095.571, 33360.286, 33747,
34958.286, 35342.286, 35342.286, 35342.286, 36704.429, 37010.714,
36826.286, 35475.143, 34794.143, 34794.143, 34794.143, 32774.714,
31721.714, 30480.429, 29774.857, 28410.143, 28410.143, 28410.143,
23754.286, 21945.429, 20019.429, 18289.857, 16292.143, 16292.143,
16292.143, 13885.857, 22754.429, 22347.857, 22418.143, 22573.286,
22416.714, 22853.857, 22470.857, 22818, 22871.286, 22556.143,
23892.286, 24462.857, 24733.857, 25362, 25364.571, 25308.714,
24829.429, 23317.857, 22311.857, 21318.286, 20416.857, 19566.857,
18321.714, 18124, 17355.571, 16576.571, 16000.857, 15071.857,
14638, 14945.571, 14656.429, 14284.143, 14507.143, 14462.571,
15206.571, 15541.714, 15383, 15439.714, 16313.286, 17085, 17938.571,
18105.571, 18906, 19789.286, 21027.286, 23093.857, 24061.857,
24875.429, 27381.571, 29258.714, 31881.857, 33894.286, 34410.857,
35017, 36094, 35581, 36715.714, 39050.571, 40600.857, 43012.429,
45963.143, 49258.143, 52495.857, 54990.571, 56106.571, 57866.143,
57403, 59507.857, 59828.571, 59809.857, 58003, 55797.429, 53669.714,
53100.714, 51342.571, 48675.429, 46338.857, 45101, 43359, 42120.429,
40573.714, 38350.286, 37239.429, 36016.714, 33821.571, 31923.429,
29977.286, 28667.857, 27071.286, 25606.714, 24332.286, 23821.429,
23355.571, 22476.429, 21322.571, 19902.429, 19182.857, 18427.714,
17780.571, 17142.714, 16265.857, 15238.571, 14666.286, 13950.857,
13249, 12625.857, 35721.1428, 38597.5428, 40037.2572, 42225.7716,
43869.8856, 46015.0284, 46602.9144, 48622.9716, 49383.6, 51783.2,
53799.6572, 56796, 59100.8, 60304.7428, 62707.2, 64033.2572,
65546.9144, 67076.4572, 68091.8856, 68753.6572, 69353.6572, 70009.6,
70672.8, 71233.8284, 66688.6284, 67262.9716, 65909.3716, 65532.0572,
64746.4, 65458.9144, 66555.3144, 72879.3144, 74252.4, 77641.3144,
79969.7144, 81933.0284, 84010.4572, 85141.2, 85618.1716, 85997.3716,
86092.9144, 86472.0572, 86389.7716, 85551.0284, 86927.0284, 87409.7716,
88116.1144, 86699.0856, 86705.9428, 87017.7716, 86334.9716, 85364.7428,
82740.9716, 73923.6, 75936.6856, 74116.0572, 72719.7716, 72813.4856,
72999.4856, 75328.3428, 78506.3428, 82702.8, 85723.8284, 86273.3716,
88253.1428, 89504.6284, 91951.6, 99934.4572, 97745.4284, 98015.4284,
99739.6, 99323.1428, 97949.0856, 95562.6284, 92629.0856, 89131.3716,
87081.2572, 83003.6572, 80189.1428, 77486.7428, 75091.3144, 72195.6,
70445.5428, 67798.1716, 68283.3144, 66546.9144, 64826.8572, 63430.5716,
62063.7716, 60461.0856, 59365.1428, 58407.6572, 56569.9428, 54798,
52201.8856, 50341.6, 48165.8284, 46890.6856, 44341.4284, 43251.3144,
41721.8856, 40702.7428, 38757.2, 37574.6856, 36166.5144, 34118.2856
), .Dim = c(106L, 7L), .Dimnames = list(NULL, c("China", "France",
"Germany", "Italy", "Spain", "United Kingdom", "US")), index = structure(c(18568,
18569, 18570, 18571, 18572, 18573, 18574, 18575, 18576, 18577,
18578, 18579, 18580, 18581, 18582, 18583, 18584, 18585, 18586,
18587, 18588, 18589, 18590, 18591, 18592, 18593, 18594, 18595,
18596, 18597, 18598, 18599, 18600, 18601, 18602, 18603, 18604,
18605, 18606, 18607, 18608, 18609, 18610, 18611, 18612, 18613,
18614, 18615, 18616, 18617, 18618, 18619, 18620, 18621, 18622,
18623, 18624, 18625, 18626, 18627, 18628, 18629, 18630, 18631,
18632, 18633, 18634, 18635, 18636, 18637, 18638, 18639, 18640,
18641, 18642, 18643, 18644, 18645, 18646, 18647, 18648, 18649,
18650, 18651, 18652, 18653, 18654, 18655, 18656, 18657, 18658,
18659, 18660, 18661, 18662, 18663, 18664, 18665, 18666, 18667,
18668, 18669, 18670, 18671, 18672, 18673), class = c("IDate",
"Date")), class = "zoo")
Ant this is my code (data=n) :
ggplot(aes(x = Index, y = Value, group = Series, colour = Series, linetype = Series),
data = fortify(n, melt = TRUE)) + geom_line(linetype = "solid" , size= 1) + xlab("") + ylab("")+
scale_y_continuous(labels = scales::label_number(accuracy =1,breaks = seq(from = 0,to= 80000, by=20000)), expand = c(0,0),
sec.axis = sec_axis(~ . * 2.5, labels = scales::label_number(accuracy =1),breaks= seq(from = 0,to = 250000, by=50000)))+
scale_x_date(date_label="%b/%y",date_breaks ="2 months")+
theme_classic()
This is the outcome
As you can see in the plot the limits for the primary y-axis goes from 25 000 to 75 000 , instead of going from 0 to 80 000 with 20 000 (increment of sequence). The same thing happen with the secondary y-axis, the limits for the secondary-y axis goes from 40 000 to 240 000 with 40 000(increment of sequence) instead of going from 0 to 250 000 with 50 000(increment of sequence).
Is there another way to specify the breaks and limits so that it is display in my plot? Thank you in advance.

You could specify the y-axis limits with coord_cartesian and modify your breaks in the primary y-axis.
df <- n %>% fortify(melt = TRUE) %>% as_tibble()
df %>%
ggplot(aes(x = Index, y = Value, group = Series, colour = Series, linetype = Series)) +
geom_line(linetype = "solid" , size= 1) +
xlab("") + ylab("") +
scale_y_continuous( breaks = seq(from = 0,to = 100000, by = 20000), ## Modified breaks instructions
labels = scales::label_number(accuracy = 1),
sec.axis = sec_axis(~ . * 2.5,
labels = scales::label_number(accuracy = 1),
breaks= seq(from = 0,to = 250000, by = 50000))) +
scale_x_date(date_label="%b/%y",date_breaks ="2 months") +
coord_cartesian(ylim = c(0,100000)) + ## Here is what you need to add
theme_classic()

Related

Saving output of splice() to a text file

I have the following script
library(zoo)
library(dplyr)
library(tidyverse)
dat<-read.csv("sep_2015.csv",header=F)
dat$date<-seq(from=as.Date("2015-09-01"),to=as.Date("2015-12-31"),by="day")
dat %>%
rownames_to_column() %>%
filter(V2 < 90 &
rollsum(V2 < 90, 7, fill = NA, align = "left") >=7 &
rollsum(V1 > 2, 7, fill = NA, align = "left") >=7 &
rollsum(V2 < 90, 20, fill = NA, align = "left") >= 15 ) %>%
slice(1)
The output is like this:
rowname V1 V2 date
1 41 5.583102 45.83082 2015-10-11
I am processing multiple files (from sep_1979 to sep_2020) with the following names:
.....
sep_2014.csv
sep_2015.csv
sep_2016.csv
....
Sample data (sep_2015.csv):
structure(list(V1 = c(1.781016934, 5.245650757, 5.253808428,
3.4500901, 1.904631502, 5.583155269, 5.29432007, 2.493240863,
2.356109739, 2.505279709, 3.100681377, 4.352358556, 9.021734968,
7.037204701, 1.946181317, 2.375415753, 4.818568252, 4.508796957,
5.009930139, 3.413858254, 2.642863447, 4.496943406, 1.101138556,
1.58130412, 5.907897257, 9.310602975, 12.8050039, 10.52127595,
10.58324383, 5.719938811, 5.65915423, 10.23184856, 8.916484733,
8.006767597, 7.623137477, 6.735120637, 3.913288259, 5.145787145,
5.516434408, 3.766181753, 5.583101736, 8.62718523, 8.55994889,
7.738160311, 9.428281922, 14.54588945, 21.1567956, 19.25095128,
11.18693546, 6.459297562, 8.113929381, 6.791538854, 2.30569946,
4.271416627, 7.043436661, 4.044279911, 4.841727481, 7.050044333,
8.646260752, 4.475859911, 9.545056312, 6.185563838, 7.545238783,
8.397502556, 6.124560982, 4.50970303, 6.682802556, 6.247809616,
5.986996325, 4.525555447, 8.719674593, 8.245588214, 6.028287485,
2.346912866, 2.842578055, 7.320520815, 6.337556706, 3.298882387,
4.512427524, 8.360934487, 9.222761141, 8.958611924, 7.982833144,
10.50259016, 8.866081434, 5.120412581, 11.46034903, 12.4205173,
10.25257175, 2.466221616, 2.692953769, 0.681709806, 1.362974813,
5.5663453, 9.601658711, 8.416295206, 3.412426219, 6.865540765,
11.76102145, 12.0902957, 6.870356614, 2.020952899, 4.363628479,
4.879838354, 4.059975788, 10.37719133, 10.37127041, 11.40995727,
10.7018141, 11.43079722, 6.58367129, 6.628593068, 2.18586382,
3.712579965, 2.874565445, 2.791741886, 4.21014224, 0.881548513,
8.251492289, 7.62408847, 5.940576571, 8.981119084), V2 =
c(121.0261069,
85.00304523, 81.87989778, 103.7505395, 210.2369147, 264.2405565,
282.9403406, 26.88123501, 19.2594093, 8.196100414, 45.26456729,
58.17439097, 87.16714037, 109.3834486, 114.840361, 38.91351028,
37.81463493, 24.49148024, 29.33726467, 13.51886983, 78.0736854,
41.91203091, 55.80655486, 338.9064404, 338.2241727, 4.297849857,
343.7324297, 266.8110122, 189.2300192, 118.7736246, 86.03606596,
120.0474015, 151.7655159, 85.86331853, 67.23147222, 51.7641341,
20.78357193, 271.0026858, 266.308435, 311.3070459, 45.83082398,
64.93928317, 63.04855403, 35.94151677, 43.73557838, 55.52061899,
55.9783368, 72.09428879, 81.11278896, 75.28379332, 56.17839757,
46.85929244, 27.56930947, 64.41308939, 75.61366497, 66.62037577,
43.60322015, 89.88800444, 101.8289993, 99.75479947, 80.43658344,
78.52117729, 83.81080108, 91.30824192, 96.42780607, 101.3614562,
118.3864844, 101.9711437, 103.8823967, 83.42613393, 74.27778556,
97.92986085, 113.0191678, 147.4666885, 134.7889272, 104.79725,
126.037476, 121.3500898, 83.21475735, 89.23421897, 92.92463803,
85.92837336, 80.02208464, 55.23356116, 52.95700516, 30.38190174,
64.76557575, 96.63946752, 86.73461083, 95.37929333, 62.17987768,
12.7633562, 273.7881121, 76.47255644, 113.7227032, 149.0157726,
78.67676081, 71.17172364, 89.33577561, 154.9133108, 221.6713695,
85.69030561, 78.51796584, 97.6857188, 78.00024078, 67.04777272,
66.22047934, 66.65994223, 68.62200021, 79.88214929, 84.2911525,
98.45874358, 175.7545009, 214.3253056, 277.4996992, 348.4682651,
97.83108644, 348.9214276, 49.4694104, 70.54130568, 79.26767621,
80.66615013), date = structure(c(16679, 16680, 16681, 16682,
16683, 16684, 16685, 16686, 16687, 16688, 16689, 16690, 16691,
16692, 16693, 16694, 16695, 16696, 16697, 16698, 16699, 16700,
16701, 16702, 16703, 16704, 16705, 16706, 16707, 16708, 16709,
16710, 16711, 16712, 16713, 16714, 16715, 16716, 16717, 16718,
16719, 16720, 16721, 16722, 16723, 16724, 16725, 16726, 16727,
16728, 16729, 16730, 16731, 16732, 16733, 16734, 16735, 16736,
16737, 16738, 16739, 16740, 16741, 16742, 16743, 16744, 16745,
16746, 16747, 16748, 16749, 16750, 16751, 16752, 16753, 16754,
16755, 16756, 16757, 16758, 16759, 16760, 16761, 16762, 16763,
16764, 16765, 16766, 16767, 16768, 16769, 16770, 16771, 16772,
16773, 16774, 16775, 16776, 16777, 16778, 16779, 16780, 16781,
16782, 16783, 16784, 16785, 16786, 16787, 16788, 16789, 16790,
16791, 16792, 16793, 16794, 16795, 16796, 16797, 16798, 16799,
16800), class = "Date")), row.names = c(NA, -122L), class = "data.frame")
I would like to:
(1) apply the above script for each file
(2) save the "date" from splice (1) into a text file
(3) the final output is a collection of dates from 1979 to 2020
How can I implement this in R? I'll appreciate any help on this.
You can try the following solution :
From each file name extract the year value.
Create sequence of dates for that year using paste and add it as a column in the data.
Filter the rows for the condition and select the 1st row from them.
Combine the result into one file.
library(tidyverse)
library(zoo)
all_files <- list.files(pattern = '^sep_.*\\.csv$')
map_df(all_files, function(file) {
year <- parse_number(file)
dates <- seq(as.Date(paste(year, 9, 1, sep = '-')),
as.Date(paste(year, 12, 31, sep = '-')), by = 'day')
dat <- read.csv(file,header=FALSE)
dat %>%
mutate(date = dates) %>%
filter(V2 < 90 &
rollsum(V2 < 90, 7, fill = NA, align = "left") >=7 &
rollsum(V1 > 2, 7, fill = NA, align = "left") >=7 &
rollsum(V2 < 90, 20, fill = NA, align = "left") >= 15) %>%
slice(1) %>%
transmute(date, file = file)
}) -> result
result should look something like this -
result
# date filename
#1979-10-10 sep_1979.csv
#1980-11-21 sep_1980.csv
#1981-09-10 sep_1981.csv
#
#
You can then write result into csv or text file as per your choice.
#CSV
write.csv(result, 'result.csv', row.names = FALSE)
#TEXT
write.table(result, 'result.txt', row.names = FALSE)

How to format date on x-axis to month and year in R

I've seen several questions posted about formatting the date and haven't been able to find one to resolve the issue I'm having here.
My data ranges from May-November from 2008-2015 and then drops May and goes from June-November from 2016-2018.
My goal is to create a plot to show the months sampled for each year and eliminate the months that were not sampled.
I asked a similar question here: How to plot mean density by year and month
But this answer does not resolve the date issue.
I've tried this code
MeanCPUE <- BD %>%
group_by(date) %>%
summarise(mean_cpue = mean (CPUE)) %>%
ungroup()
MeanCPUE
p <- ggplot(MeanCPUE, aes(x=date, y=mean_cpue))+
geom_line(aes()) +
geom_point(aes())+
labs(title = "Mean Density", y = "Mean Density (# fish/100m2)", x = "Date")+
theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
axis.text.x = element_text(angle = 90))
Which gives me this plot.. This is close, but it doesn't label the months and it's connected the sampled months with a line through the un-sampled months.
I'm trying to make something like this with the way the dates are displayed on the x-axis with all the sampled months vertical and the corresponding year horizontal below.
Is this possible with the gaps caused by un-sampled months that I have? Is there a better way to display data like this?
I'm using this data
structure(list(date = structure(c(14012, 14021, 14050, 14056,
14076, 14083, 14110, 14118, 14124, 14139, 14141, 14168, 14174,
14201, 14208, 14371, 14372, 14398, 14405, 14439, 14442, 14468,
14469, 14498, 14512, 14525, 14547, 14562, 14565, 14733, 14736,
14764, 14770, 14811, 14813, 14826, 14848, 14854, 14862, 14895,
14896, 14905, 14924, 14931, 15105, 15106, 15135, 15147, 15161,
15168, 15189, 15191, 15231, 15233, 15261, 15272, 15300, 15301,
15469, 15476, 15504, 15506, 15527, 15539, 15567, 15576, 15604,
15605, 15631, 15637, 15665, 15671, 15835, 15841, 15868, 15869,
15896, 15902, 15924, 15926, 15961, 15966, 15988, 15995, 16017,
16028, 16211, 16213, 16238, 16247, 16267, 16270, 16274, 16302,
16304, 16332, 16333, 16366, 16367, 16387, 16395, 16563, 16564,
16592, 16596, 16637, 16638, 16666, 16668, 16693, 16696, 16723,
16731, 16756, 16759, 16961, 16962, 16991, 16996, 17017, 17025,
17051, 17060, 17093, 17094, 17109, 17116, 17334, 17345, 17366,
17367, 17396, 17402, 17430, 17437, 17451, 17457, 17479, 17480,
17695, 17696, 17725, 17726, 17758, 17760, 17786, 17788, 17821,
17845, 17848), class = "Date"), mean_cpue = c(1.34147348124682,
1.86964964075215, 1.81794067339267, 0.665667643204071, 0.58288080140313,
1.21618562008796, 0.915544650452313, 2.08485242966155, 1.03716945493794,
1.32653950869479, 1.30387017192198, 0.818696953343472, 1.80344708020158,
0.462802005064552, 0.500089944234575, 0.468688145292134, 3.77629506069873,
2.06756426400247, 2.60938827682934, 0.936798217363299, 1.50723642250895,
1.72839753752143, 1.58423249977652, 1.85978443365114, 1.22923787251904,
1.86003452037686, 1.10727777349472, 1.4293872174252, 1.50010309934371,
3.06022031046633, 1.49412683285299, 2.14503371546005, 1.06420000907848,
1.1748728446591, 1.10022108873144, 1.44768795978073, 1.25036934931668,
2.13048237961316, 1.06834543832824, 1.8752166760057, 0.758904479222882,
0.961385073330007, 1.8663428674222, 0.745490766022096, 1.15427953668245,
1.98858189404051, 2.27561614501712, 1.71143613797583, 1.49915849827066,
0.841476235553629, 2.31692888870439, 1.35038151346165, 1.18518826020175,
1.22169567368232, 1.10179032565335, 0.694614793373563, 0.983229683310633,
1.12892772438535, 1.53364097932751, 0.787500879889251, 1.33114386888348,
0.75361846092619, 1.71920865710658, 1.35852187828517, 1.689863533171,
1.28266297991069, 1.86158062664132, 1.5293414824795, 1.0998008820738,
1.94141263432714, 0.342436942826791, 0.714994071540338, 5.62005947683777,
1.62440270638761, 3.82262996941896, 3.25889497807134, 1.54143260146352,
1.86769619502575, 2.8059505280639, 3.58304933614583, 1.20470762672598,
2.29674008364814, 0.689037082722997, 0.959492687511224, 0.567406828456012,
0.844493420881774, 0.513055729898873, 0.986197068455855, 1.86584317723012,
0.229688101851428, 1.25022486058644, 1.1701621335822, 0.742587041289875,
0.625081199656213, 1.29798096608674, 0.396058527329973, 1.16988984259697,
0.739772339659098, 0.39805108067041, 0.606186664268154, 0.300984338326838,
1.27916344040608, 5.75511524259318, 3.58466880968582, 1.02559789553853,
1.26985222650329, 1.54851107697843, 1.10318926209412, 1.3337665961789,
1.21571684379214, 1.18341136267535, 1.44015621029248, 0.669940304071688,
1.80318080838661, 0.919979963710556, 0.555260577151843, 2.32733965810763,
1.08046383335355, 0.621234946065283, 1.79207073432654, 1.3398375035128,
1.67582836446281, 1.3687859338262, 1.64687245759481, 0.880872177773362,
0.972535713988714, 0.83891596232892, 2.09933443707235, 1.24879096699177,
1.99677155577555, 1.67638127852795, 1.50710438838265, 2.27326799319906,
1.83813683299605, 2.34551745405533, 1.93879794527393, 1.74635992056851,
0.966061489637619, 1.1709324985094, 2.77527932681737, 4.63212808059003,
2.50472488366812, 2.19392784029982, 3.64096784095526, 1.91684726897895,
1.6466312330028, 2.84951794706046, 1.6841559743533, 1.62105139974506,
1.46007933985039)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-150L), .internal.selfref = <pointer: 0x000001d8f6d11ef0>, sorted = "NODCCODE")
Thanks in advance for any help!!
In the following, I have added group = year(date) to aesthetics so that line will connect only those points in the same year.
The
scale_x_date(date_labels = "%b-%Y") is to have monthly ticks - not sure if you still want this.
library(ggplot2)
library(dplyr)
library(lubridate)
ggplot(MeanCPUE, aes(x=date, y=mean_cpue, group = year(date)))+
geom_line() +
geom_point()+
labs(title = "Mean Density", y = "Mean Density (# fish/100m2)", x = "Date") +
theme_bw() +
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
axis.text.x = element_text(angle = 90)) +
scale_x_date(date_breaks = '1 year', date_labels = "%b-%Y")

maintain date class for x-axis with geom_smooth

I am trying to smooth three lines and shade the area between the min and max lines using this approach. My x-axis is a date class at the start, but I'm getting an error in the last step.
Invalid input: date_trans works with objects of class Date only
I'm losing the date class when smoothing into p1. How can I prevent this or otherwise pass a date to geom_ribbon in the final step?
library(tidyverse)
mmr <- structure(list(year = structure(c(3834, 3926, 3987, 4108, 4169,
4230, 4352, 4442, 4503, 4595, 4687, 4748, 4929, 4991, 5052, 5204,
5265, 5326, 5448, 5538, 5599, 5752, 5813, 5844, 6025, 6087, 6148,
6299, 6360, 6574, 6634, 6695, 6848, 6909, 7091, 7183, 7213, 7395,
7456, 7517, 7639, 7729, 7790, 7943, 8035, 8187, 8248, 8309, 8460,
8552, 8613, 8735, 8825, 8886, 9009, 9100, 9131, 9282, 9343, 9404,
9556, 9617, 9678, 9831, 9862, 9952, 10105, 10135, 10196, 10347,
10408, 10470, 10592, 10682, 10896, 10957, 11078, 11170, 11231,
11382, 11474, 11596, 11657, 11688, 11869, 11931, 11961, 12112,
12173, 12234, 12357, 12418, 12478, 12600, 12692, 12874, 12935,
12996, 13118, 13208, 13239, 13392, 13483, 13665, 13757, 13787,
13939), class = "Date"), mmrU = c(13.8231596, 13.7529708, 13.7148858,
13.6118164, 13.5638772, 13.5478975, 13.519933, 13.5012899, 13.474657,
13.4014166, 13.4173963, 13.3787786, 13.2882268, 13.2589306, 13.2354049,
13.1763686, 13.1470724, 13.1255442, 13.0618471, 13.032551, 13.0108008,
13.0139079, 13.0232294, 13.0352143, 13.0498623, 13.0498623, 13.0376556,
13.0232294, 13.0205662, 12.987275, 12.9739586, 12.9033814, 12.9792852,
12.99127, 13.0059181, 13.0059181, 13.0059181, 13.0205662, 13.0130202,
13.0034767, 12.9606421, 12.9406675, 12.9277949, 12.8727536, 12.8203756,
12.7728802, 12.7582321, 12.7569005, 12.7422524, 12.7382575, 12.7171414,
12.6397157, 12.601098, 12.5737993, 12.6583588, 12.7382575, 12.7979151,
12.9473257, 12.9965966, 13.0327729, 13.1191079, 13.1617205, 13.2061087,
13.332171, 13.39609, 13.4586773, 13.6477709, 13.7676189, 13.8408594,
14.0619124, 14.1604541, 14.2246394, 14.3815071, 14.4851535, 14.7996436,
14.9234866, 15.0436305, 15.3022444, 15.4277361, 15.7198102, 15.9745982,
16.2684478, 16.4362351, 16.5409911, 16.8277386, 16.9728879, 17.0634398,
17.3204472, 17.4616016, 17.5578129, 17.819814, 17.9623, 18.0631721,
18.3045327, 18.4784455, 18.6694034, 18.7612869, 18.8153517, 18.9250792,
18.9770134, 19.0189602, 19.0675652, 19.07955, 19.1008563, 19.0582437,
19.0292138, 18.9410589), mmr = c(12.1431454, 12.11401197, 12.05086241,
12.02612341, 11.96590348, 11.93823486, 11.8828976, 11.8601117,
11.8275604, 11.8047745, 11.7784079, 11.7461821, 11.6955796, 11.7256748,
11.6689467, 11.6396505, 11.5997012, 11.6033632, 11.5384455, 11.5144759,
11.4532202, 11.4532202, 11.5411088, 11.5198025, 11.4971645, 11.5118126,
11.5118126, 11.5345985, 11.5586865, 11.4532202, 11.4532202, 11.4692,
11.5144759, 11.5201829, 11.5494791, 11.525129, 11.5662198, 11.570405,
11.4665367, 11.4984962, 11.4345772, 11.4692, 11.4092759, 11.3893013,
11.3653317, 11.3799798, 11.4406647, 11.3014127, 11.2721165, 11.2833024,
11.2720179, 11.2161875, 11.1675824, 11.1749065, 11.1928097, 11.2546572,
11.3295252, 11.397883, 11.568574, 11.5020472, 11.5437721, 11.5792826,
11.6846601, 11.7494372, 11.8112847, 11.8868038, 12.0684401, 12.1749717,
12.2844995, 12.3747184, 12.4756571, 12.5664753, 12.7422524, 12.8274777,
13.114314, 13.2216445, 13.3164576, 13.575862, 13.7156847, 13.8555074,
14.1513989, 14.2922869, 14.4147983, 14.5559526, 14.8196183, 14.9421296,
15.0779574, 15.203132, 15.3283066, 15.4521496, 15.686519, 15.7970456,
15.9248835, 16.0292844, 16.21678, 16.3945102, 16.4693782, 16.5442463,
16.6028386, 16.661431, 16.7265336, 16.7734075, 16.8304019, 16.8407888,
16.781464, 16.7411817, 16.6532932), mmrL = c(10.68619282, 10.63692196,
10.5963068, 10.49976255, 10.45848156, 10.44250182, 10.4092107,
10.39189932, 10.38390945, 10.31732721, 10.28270445, 10.25695931,
10.20813234, 10.21612221, 10.22278043, 10.25207662, 10.26139813,
10.24719392, 10.21079563, 10.19481589, 10.17883615, 10.17883615,
10.17883615, 10.17883615, 10.1921526, 10.19481589, 10.20813234,
10.20946398, 10.22011714, 10.19481589, 10.18283109, 10.17883615,
10.17883615, 10.17883615, 10.17883615, 10.17484122, 10.16418806,
10.14554503, 10.13356023, 10.12024378, 10.09893747, 10.08828431,
10.0762995, 10.06165141, 10.04700332, 10.02436536, 9.999064105,
9.9810869, 9.936476799, 9.911175548, 9.890756995, 9.840598374,
9.815297123, 9.79798574, 9.880547718, 9.947129958, 9.997199802,
10.1149172, 10.14021845, 10.14953997, 10.18948931, 10.21345892,
10.24963527, 10.36659807, 10.42519044, 10.47179801, 10.63958525,
10.74478519, 10.82335224, 10.99261909, 11.11726527, 11.15293432,
11.27611147, 11.33869877, 11.52113411, 11.58638471, 11.6377862,
11.82075419, 11.96190854, 12.31346277, 12.45195383, 12.52985505,
12.74491568, 12.85810549, 12.93633962, 13.16971038, 13.29887992,
13.38676848, 13.61581138, 13.71834803, 13.78226698, 13.95005423,
14.03927443, 14.10159541, 14.27497556, 14.42971269, 14.57725893,
14.60788676, 14.63185637, 14.69843861, 14.72373986, 14.73439302,
14.73439302, 14.73439302, 14.73683436, 14.674469, 14.60744288
)), row.names = c(NA, -107L), class = "data.frame")
# smooth lines
p1 <-
mmr %>%
ggplot(.) +
geom_smooth(aes(x=year, y=mmrL)) +
geom_smooth(aes(x=year, y=mmr)) +
geom_smooth(aes(x=year, y=mmrU)) +
ylim(0, 25) +
scale_x_date(date_breaks="2 years", date_labels = "%Y")
# build plot object for rendering
pp1 <- ggplot_build(p1)
# extract data from the upper and lower lines
df2 <- data.frame(x = pp1$data[[1]]$x,
ymin = pp1$data[[2]]$y,
ymax = pp1$data[[3]]$y)
# use the lm data to add the ribbon to the plot
p1 + geom_ribbon(data = df2, aes(x = x, ymin = ymin, ymax = ymax), fill = "grey", alpha = 0.4)
The issue is that pp1$data[[1]]$x is no longer of date format. We may instead use
df2 <- data.frame(x = as.Date(pp1$data[[1]]$x, origin = "1970-01-01"),
ymin = pp1$data[[1]]$y,
ymax = pp1$data[[3]]$y)
where I've also adjusted ymin. This gives

Extract dataframe name in interactive R shiny app

I have a dataframe df that I use to interactively plot in an R shiny app. To define the axis xlim and ylim ranges of the plot I use an observeEvent function to define the xlim based on a dateRange field.
observeEvent(input$Number,{
ranges1$x <- input$dateRange_df
ranges1$y <- NULL
ranges2$x <- input$dateRange_df
ranges2$y <- NULL
})
How can I set the ylim (ranges1$y which is currently set to NULL in the example) to range e.g. over the minimum value of df -0.5 and maximum value of df + 0.5? Normally in a plot I would define the ylim like this:
ylim <- c(min(df$'0'-0.5), max(df$'0'+0.5))
but in order to make it interactive I have to make use of the placeholder input$Number
ranges1$y <- c(min(df$input$Number)-0.5, max(df$input$Number)+0.5)
does not work. How can I access the dataframe with this placeholder?
EDIT
Based on Stéphane Laurent's comment I changed my code to the following:
ranges1$y <- c(min(df[[input$Number]]$Y)-0.5, max(df[[input$Number]]$Y)+0.5)
df <- list(`0` = structure(data.frame(Date = structure(c(17737, 17738, 17739, 17740, 17741, 17742, 17743, 17744, 17745, 17746, 17747, 17748, 17749, 17750, 17751, 17752, 17753, 17754, 17755, 17757, 17758, 17759, 17760, 17761, 17762, 17763, 17764, 17766, 17767, 17768, 17769, 17770, 17772, 17773, 17774, 17775, 17776, 17777, 17778, 17779, 17780, 17781, 17782, 17783, 17784, 17785, 17786, 17787, 17789, 17790, 17791, 17792, 17793, 17794, 17795, 17796, 17797, 17798, 17799, 17800, 17801, 17802, 17803, 17804, 17805, 17806, 17807, 17808, 17809, 17810, 17811, 17812, 17813, 17814, 17815, 17816, 17817, 17818, 17819, 17820, 17821, 17822, 17823, 17824, 17825, 17826, 17827, 17828, 17829, 17830, 17831, 17833, 17834, 17835, 17836, 17837, 17838, 17839, 17840, 17841, 17843, 17844, 17846, 17847, 17848, 17849, 17850, 17851, 17852, 17853, 17854, 17856, 17858, 17860, 17862, 17864, 17866, 17868, 17870, 17872, 17874, 17876, 17878, 17880, 17882, 17884, 17888, 17922, 17924), class = "Date"),
X = c(2791162.3550637, 2791162.35592537, 2791162.34294051, 2791162.34864573, 2791162.34433394, 2791162.34740308, 2791162.34313007, 2791162.34487983, 2791162.34646726, 2791162.34200201, 2791162.34749474, 2791162.34255676, 2791162.34304754, 2791162.3425003, 2791162.34330067, 2791162.34278867, 2791162.34246682, 2791162.3438409, 2791162.34466809, 2791162.34875856, 2791162.34477708, 2791162.34662692, 2791162.34633941, 2791162.34701495, 2791162.3437676, 2791162.34370942, 2791162.34553501, 2791162.34325701, 2791162.34759088, 2791162.34038494, 2791162.33862812, 2791162.33991438, 2791162.34720632, 2791162.34631027, 2791164.24372202, 2791162.34461334, 2791162.34142775, 2791162.33764027, 2791162.33759566, 2791162.34250817, 2791162.33088489, 2791162.33793524, 2791162.3389292, 2791162.33744799, 2791162.33913297, 2791162.34362382, 2791162.33397091, 2791162.33889572, 2791162.3420609, 2791162.34256534, 2791162.34295388, 2791162.34360599, 2791162.33906351, 2791162.3410493, 2791162.34179139, 2791162.33807938, 2791162.34030308, 2791162.33740247, 2791162.33686627, 2791162.33465613, 2791162.33364715, 2791162.33327762, 2791162.33233307, 2791162.33092723, 2791162.32979484, 2791162.33032923, 2791162.32840836, 2791160.1523217, 2791162.33247584, 2791162.3277548, 2791162.32251429, 2791162.32674239, 2791162.33025114, 2791162.32962545, 2791162.32878667, 2791162.32817898, 2791162.32769788, 2791162.32821961, 2791162.32480343, 2791162.31161777, 2791162.32399605, 2791162.32336013, 2791162.32222764, 2791162.32393696, 2791162.3249891, 2791162.32406214, 2791162.32114112, 2791162.32277927, 2791162.32680753, 2791162.32359466, 2791162.31715986, 2791162.43273995, 2791162.43609669, 2791162.43228038, 2791162.43026514, 2791162.43019254, 2791162.42065433, 2791165.78288008, 2791162.42082946, 2791162.42138195, 2791162.41389611, 2791162.41563357, 2791162.41845497, 2791162.41773703, 2791162.41787637, 2791162.4187504, 2791162.41651641, 2791162.4153813, 2791162.41653668, 2791162.41481415, 2791162.41964469, 2791162.41477361, 2791162.41328494, 2791162.41301816, 2791162.41301417, 2791162.4045612, 2791162.40596568, 2791162.38755189, 2791162.39311443, 2791162.40093757, 2791162.38179792, 2791162.38089839, 2791162.38206784, 2791162.38178915, 2791162.3808269, 2791162.38363636, 2791162.38168147, 2791162.38247946, 2791162.38038856),
Y = c(1152645.1057075, 1152645.10716654, 1152645.0986932, 1152645.10973225, 1152645.10268387, 1152645.10860166, 1152645.10285369, 1152645.10415144, 1152645.10949053, 1152645.10592819, 1152645.11141594, 1152645.10374256, 1152645.10746764, 1152645.1063395, 1152645.10800735, 1152645.10815158, 1152645.10766793, 1152645.10480103, 1152645.11599135, 1152645.11151243, 1152645.10463071, 1152645.10960652, 1152645.10975319, 1152645.11164281, 1152645.11013769, 1152645.11160747, 1152645.11252802, 1152645.10673275, 1152645.11099244, 1152645.10262804, 1152645.10838721, 1152645.11149681, 1152645.11408256,1152645.11280042, 1152644.37773542, 1152645.11690802, 1152645.11770136, 1152645.1125467, 1152645.11574864, 1152645.11438099, 1152645.12331471, 1152645.1152969, 1152645.11450451, 1152645.11354352, 1152645.11153381, 1152645.11427941, 1152645.11192852, 1152645.11176822, 1152645.12555403, 1152645.12672117, 1152645.12572363, 1152645.12744387, 1152645.12433327, 1152645.12610127, 1152645.12706131, 1152645.12156846, 1152645.12355019, 1152645.12250923, 1152645.11961065, 1152645.11727393, 1152645.12012378, 1152645.1200921, 1152645.11721301, 1152645.11850732, 1152645.11513263, 1152645.12240832, 1152645.12009354, 1152645.187559, 1152645.12752105, 1152645.12267048, 1152645.11869222, 1152645.12166756, 1152645.12614825, 1152645.12081258, 1152645.12522161, 1152645.12731805, 1152645.1251948, 1152645.1250067, 1152645.11414578, 1152645.12608457, 1152645.11437481, 1152645.11610481, 1152645.119979, 1152645.11283147, 1152645.1149272, 1152645.11784599, 1152645.11696344, 1152645.1241658, 1152645.12738042, 1152645.12265048, 1152645.13113493, 1152644.90542479, 1152644.90531955, 1152644.9137409, 1152644.91079601, 1152644.91118295, 1152644.93155628, 1152644.85931671, 1152644.92505568, 1152644.92365586, 1152644.9062049, 1152644.9072195, 1152644.91269895, 1152644.91120019, 1152644.91528026, 1152644.91661213, 1152644.91545375, 1152644.91216822, 1152644.91436215, 1152644.91334168, 1152644.9132155, 1152644.91259903, 1152644.91009809, 1152644.90909567, 1152644.91015364, 1152644.90557376, 1152644.90806689, 1152644.94956111, 1152644.94274402, 1152644.91456334, 1152644.96352573, 1152644.95826583, 1152644.95863937, 1152644.95850146, 1152644.96287929, 1152644.9612743, 1152644.96621915, 1152644.94007565, 1152644.96446478))),
`4` = structure(data.frame(Date = structure(c(17739, 17740, 17741,17742, 17743, 17744, 17745, 17746, 17747, 17748, 17749, 17750, 17751, 17752, 17753, 17754, 17755, 17757, 17758, 17759, 17760, 17761, 17762, 17763, 17764, 17766, 17767, 17768, 17769, 17770, 17772, 17773, 17774, 17775, 17776, 17777, 17778, 17779, 17780, 17781, 17782, 17783, 17784, 17785, 17786, 17787, 17789, 17790, 17791, 17792, 17793, 17794, 17795, 17796, 17797, 17798, 17799, 17800, 17801, 17802, 17803, 17804, 17805, 17806, 17807, 17808, 17809, 17810, 17811, 17812, 17813, 17814, 17815, 17816, 17817, 17818, 17819, 17820, 17821, 17822, 17823, 17824, 17825, 17826, 17827, 17828, 17829, 17830, 17831, 17832, 17833, 17834, 17835, 17836, 17837, 17838, 17839, 17840, 17841, 17843, 17844, 17846, 17847, 17848, 17849, 17850, 17851, 17852, 17853, 17854, 17856, 17858, 17860, 17862, 17864, 17866, 17868, 17870, 17872, 17874, 17876, 17878, 17880, 17882, 17884, 17888, 17890, 17892, 17894, 17896, 17898, 17900, 17904, 17910, 17912, 17914, 17916, 17918, 17920, 17922, 17924), class = "Date"),
X = c(2790824.18374809, 2790824.18858669, 2790824.18446804, 2790824.1888521, 2790824.18384519, 2790824.18617796, 2790824.18771725, 2790824.18443985, 2790824.18748079, 2790824.18308143, 2790824.18520752, 2790824.18257565, 2790824.18457724, 2790824.18600788, 2790824.18375888, 2790824.1847667, 2790824.18760882, 2790824.19052547, 2790824.18875072, 2790824.19017255, 2790824.19245601, 2790824.19081026, 2790824.19017255, 2790824.18495934, 2790824.18864228, 2790824.18773663, 2790824.18766243, 2790824.18412512, 2790824.18165805, 2790824.18411731, 2790824.1886425, 2790824.18711752, 2790825.7650905, 2790824.1887942, 2790824.18584957, 2790824.18555477, 2790824.18344282, 2790824.18663601, 2790824.18287909, 2790824.18046056, 2790824.18249738, 2790824.18103211, 2790824.18272872, 2790824.18214583, 2790824.18129456, 2790824.18119768, 2790824.19036244, 2790824.19282126, 2790824.19331333, 2790824.19533039, 2790824.18985039, 2790824.19113479, 2790824.1932507, 2790824.18973607, 2790824.18726316, 2790824.18820932, 2790824.18617466, 2790824.18598988, 2790824.18462304, 2790824.18537468, 2790824.18511984, 2790824.1834283, 2790824.1861561, 2790824.18377319, 2790824.18455121, 2790823.77616338, 2790824.18794645, 2790824.18470526, 2790824.18367412, 2790824.18490804, 2790824.18672408, 2790824.18804839, 2790824.18506072, 2790824.18476163, 2790824.18430831, 2790824.18437524, 2790824.18377374, 2790824.17576408, 2790824.18188471, 2790824.17497523, 2790824.17361097, 2790824.17991796, 2790824.18053179, 2790824.18515782, 2790824.18380764, 2790824.18674445, 2790824.18748905, 2790824.18360762, 2790824.17562747, 2790824.17512053, 2790824.29807747, 2790824.29977056, 2790824.29613796, 2790824.29415652, 2790824.28809071, 2790824.28362793, 2790824.28864387, 2790824.28424219, 2790824.27961904, 2790824.28421589, 2790824.28469113, 2790824.28450756, 2790824.28310669, 2790824.28250442, 2790824.28508087, 2790824.28348406, 2790824.28309668, 2790824.28288763, 2790824.28368468, 2790824.28541184, 2790824.2850932, 2790824.28369521, 2790824.27851666, 2790824.28289038, 2790824.28139771, 2790824.27608701, 2790824.25548963, 2790824.2638138, 2790824.27164658, 2790824.25281592, 2790824.25130503, 2790824.25128763, 2790824.25101947, 2790824.2521007, 2790824.25603123, 2790824.25821979, 2790824.25660884, 2790824.27185727, 2790824.26655444, 2790824.25487382, 2790824.24117284, 2790824.24537759, 2790824.23911296, 2790824.23060396, 2790824.25309687, 2790824.26019989, 2790824.26512535, 2790824.2679404, 2790824.26690672, 2790824.25377196, 2790824.26113459),
Y = c(1152771.25205373, 1152771.26044974, 1152771.25753943, 1152771.25978308, 1152771.2558363, 1152771.25737316, 1152771.26221167, 1152771.26008521, 1152771.26175994, 1152771.25406342, 1152771.25888205, 1152771.25536511, 1152771.25642348, 1152771.25771622, 1152771.25637725, 1152771.25125447, 1152771.26784701, 1152771.26538719, 1152771.25912007, 1152771.26242374, 1152771.2644979, 1152771.26553601, 1152771.26377529, 1152771.26209083, 1152771.26141445, 1152771.26246472, 1152771.2604169, 1152771.26242945, 1152771.25709542, 1152771.26461715, 1152771.26770187, 1152771.26539248, 1152770.44825641, 1152771.26541274, 1152771.26600154, 1152771.26392009, 1152771.26404865, 1152771.26264474, 1152771.26493265, 1152771.25979324, 1152771.26063346, 1152771.26042017, 1152771.25733911, 1152771.25714285, 1152771.25710758, 1152771.25825841, 1152771.26857731, 1152771.26889403, 1152771.26763328, 1152771.27012269, 1152771.26569212, 1152771.26643379, 1152771.26831453, 1152771.26453117, 1152771.2659026, 1152771.26211273, 1152771.26160017, 1152771.25949032, 1152771.25648998, 1152771.25802036, 1152771.2564697, 1152771.25572519, 1152771.26994309, 1152771.25856009, 1152771.25577385, 1152771.21190108, 1152771.26022874, 1152771.25793114, 1152771.25677626, 1152771.2583099, 1152771.26039744, 1152771.25265629, 1152771.25645023, 1152771.25709743, 1152771.25675924, 1152771.25475927, 1152771.25495515, 1152771.25653707, 1152771.25558733, 1152771.24623797, 1152771.24770836, 1152771.2549661, 1152771.25507885, 1152771.25924985, 1152771.2628605, 1152771.26486285, 1152771.26706399, 1152771.26320436, 1152771.25670899, 1152771.25969191, 1152771.04282095, 1152771.0422443, 1152771.04935005, 1152771.0453424, 1152771.03681296, 1152771.04392157, 1152771.04540486, 1152771.04963515, 1152771.02783388, 1152771.03612569, 1152771.03824284, 1152771.04113289, 1152771.04169045, 1152771.04252987, 1152771.04051328, 1152771.04079713, 1152771.03747317, 1152771.03739855, 1152771.03942934, 1152771.03740992, 1152771.0364367, 1152771.03294287, 1152771.03230096, 1152771.03823554, 1152771.03634142, 1152771.03405967, 1152771.07550426, 1152771.06546547, 1152771.03697854, 1152771.08281639, 1152771.07903828, 1152771.08392586, 1152771.0829198, 1152771.08307592, 1152771.0774657, 1152771.09114143, 1152771.07792026, 1152771.05372315, 1152771.0585986, 1152771.07327235, 1152771.08565988, 1152771.08055774, 1152771.09170355, 1152771.09050087, 1152771.05223544, 1152771.05222645, 1152771.05033638, 1152771.04887938, 1152771.04816407, 1152771.05337247, 1152771.06691153))))
fmt_decimals <- function(decimals=0){
function(x) format(x,nsmall = decimals,scientific = FALSE)
}
ui <- fluidPage(
titlePanel("TestApp", windowTitle = "Test Shiny App"),
tabsetPanel(tabPanel("Tab1",
wellPanel(fluidRow(column(selectInput("Number", "Datset number:", names(df)), width = 1),
column(dateRangeInput('dateRange_df',
label = 'Select the time interval for plots',
start = "2018-07-20",
separator = " - ", format = "dd/mm/yy",
startview = 'year' , weekstart = 1), width = 4, offset = 3))),
fluidRow(column(plotOutput("plot1", height = 500, dblclick = "plot1_dblclick", brush = brushOpts(id = "plot1_brush", resetOnNew = TRUE)), width = 6),
column(plotOutput("plot2", height = 500, dblclick = "plot2_dblclick", brush = brushOpts(id = "plot2_brush", resetOnNew = TRUE)), width = 6)))))
server <- function(input,output, session){
observeEvent(input$dateRange_df, {
ranges1$x <- input$dateRange_df
ranges2$x <- input$dateRange_df
})
observeEvent(input$Number,{
ranges1$x <- input$dateRange_df
ranges1$y <- c(min(df[[input$Number]]$Y)-0.5, max(df[[input$Number]])+0.5)
ranges2$x <- input$dateRange_df
ranges2$y <- c(min(df[[input$Number]]$Y)-0.5, max(df[[input$Number]])+0.5)
})
# Plot1
ranges1 <- reactiveValues(x = NULL, y = NULL)
output$plot1 <- renderPlot({
if (!is.null(ranges1$x)) {
ranges1$x <- as.Date(ranges1$x, origin = "1970-01-01")
}
if(input$Number == "0"){
col = rgb(165,0,38, max = 255)
}
else if(input$Number == "4"){
col = rgb(215,48,39, max = 255)
}
labels_dec1 <- fmt_decimals(2)
p <- ggplot(df[[input$Number]], aes(x = Date, y = X)) +
geom_point(colour = col) + coord_cartesian(xlim = ranges1$x, ylim = ranges1$y, expand =FALSE)
p <- p + labs(x = "", y = "X [m]", title = paste("Plot1 - Number ", input$Number))
p <- p + theme(panel.background = element_rect(fill = "grey99"))
p <- p + scale_y_continuous(labels = labels_dec1)
p + ggtitle(paste("Plot1 - Dataset number", input$Number))
})
observeEvent(input$plot1_dblclick, {
brush <- input$plot1_brush
if (!is.null(brush)) {
ranges1$x <- c(brush$xmin, brush$xmax)
ranges1$y <- c(brush$ymin, brush$ymax)
} else {
ranges1$x <- input$dateRange_df
ranges1$y <- NULL
}
})
# Plot2
ranges2 <- reactiveValues(x = NULL, y = NULL)
output$plot2 <- renderPlot({
if (!is.null(ranges2$x)) {
ranges2$x <- as.Date(ranges2$x, origin = "1970-01-01")
}
if(input$Number == "0"){
col = rgb(165,0,38, max = 255)
}
else if(input$Number == "4"){
col = rgb(215,48,39, max = 255)
}
labels_dec2 <- fmt_decimals(2)
p <- ggplot(df[[input$Number]], aes(x = Date, y = Y)) +
geom_point(colour = col) + coord_cartesian(xlim = ranges2$x, ylim = ranges2$y, expand =FALSE)
p <- p + labs(x = "", y = "Y [m]", title = paste("Plot 2 - Number ", input$Number))
p <- p + theme(panel.background = element_rect(fill = "grey99"))
p <- p + scale_y_continuous(labels = labels_dec2)
p + ggtitle(paste("Plot2 - Dataset number ", input$Number))
})
observeEvent(input$plot2_dblclick, {
brush <- input$plot2_brush
if (!is.null(brush)) {
ranges2$x <- c(brush$xmin, brush$xmax)
ranges2$y <- c(brush$ymin, brush$ymax)
} else {
ranges2$x <- input$dateRange_df
ranges2$y <- NULL
}
})
output$brush_info1 <- renderPrint({
brushedPoints(df[[input$Number]], input$plot1_brush)
})
}
shinyApp(ui, server)

Set default value of Reactive Values in an R Shiny app

I have built a shiny app that displays time series data in a series of plots (simplified version of the app can be found below). I made the plots zoomable using this template: https://gallery.shinyapps.io/105-plot-interaction-zoom/
This works all fine, however I was wondering if it is possible to set default values for reactiveValues (e.g. for ranges1 and ranges2 examples below)?
In other words: I would like to set the initial default values for the x axis to the values specified in the dateRangeInput field. When the user interacts with the plot (e.g. zooms in or out) the axis values should then be changed according to the reactive value of ranges below enabling the zoom interaction.
Is this possible to achieve? I attempted to create a simplified, reproducible version of my app. The code can be found below:
df <- list(`0` = structure(data.frame(Date = structure(c(17737, 17738, 17739, 17740, 17741, 17742, 17743, 17744, 17745, 17746, 17747, 17748, 17749, 17750, 17751, 17752, 17753, 17754, 17755, 17757, 17758, 17759, 17760, 17761, 17762, 17763, 17764, 17766, 17767, 17768, 17769, 17770, 17772, 17773, 17774, 17775, 17776, 17777, 17778, 17779, 17780, 17781, 17782, 17783, 17784, 17785, 17786, 17787, 17789, 17790, 17791, 17792, 17793, 17794, 17795, 17796, 17797, 17798, 17799, 17800, 17801, 17802, 17803, 17804, 17805, 17806, 17807, 17808, 17809, 17810, 17811, 17812, 17813, 17814, 17815, 17816, 17817, 17818, 17819, 17820, 17821, 17822, 17823, 17824, 17825, 17826, 17827, 17828, 17829, 17830, 17831, 17833, 17834, 17835, 17836, 17837, 17838, 17839, 17840, 17841, 17843, 17844, 17846, 17847, 17848, 17849, 17850, 17851, 17852, 17853, 17854, 17856, 17858, 17860, 17862, 17864, 17866, 17868, 17870, 17872, 17874, 17876, 17878, 17880, 17882, 17884, 17888, 17922, 17924), class = "Date"),
X = c(2791162.3550637, 2791162.35592537, 2791162.34294051, 2791162.34864573, 2791162.34433394, 2791162.34740308, 2791162.34313007, 2791162.34487983, 2791162.34646726, 2791162.34200201, 2791162.34749474, 2791162.34255676, 2791162.34304754, 2791162.3425003, 2791162.34330067, 2791162.34278867, 2791162.34246682, 2791162.3438409, 2791162.34466809, 2791162.34875856, 2791162.34477708, 2791162.34662692, 2791162.34633941, 2791162.34701495, 2791162.3437676, 2791162.34370942, 2791162.34553501, 2791162.34325701, 2791162.34759088, 2791162.34038494, 2791162.33862812, 2791162.33991438, 2791162.34720632, 2791162.34631027, 2791164.24372202, 2791162.34461334, 2791162.34142775, 2791162.33764027, 2791162.33759566, 2791162.34250817, 2791162.33088489, 2791162.33793524, 2791162.3389292, 2791162.33744799, 2791162.33913297, 2791162.34362382, 2791162.33397091, 2791162.33889572, 2791162.3420609, 2791162.34256534, 2791162.34295388, 2791162.34360599, 2791162.33906351, 2791162.3410493, 2791162.34179139, 2791162.33807938, 2791162.34030308, 2791162.33740247, 2791162.33686627, 2791162.33465613, 2791162.33364715, 2791162.33327762, 2791162.33233307, 2791162.33092723, 2791162.32979484, 2791162.33032923, 2791162.32840836, 2791160.1523217, 2791162.33247584, 2791162.3277548, 2791162.32251429, 2791162.32674239, 2791162.33025114, 2791162.32962545, 2791162.32878667, 2791162.32817898, 2791162.32769788, 2791162.32821961, 2791162.32480343, 2791162.31161777, 2791162.32399605, 2791162.32336013, 2791162.32222764, 2791162.32393696, 2791162.3249891, 2791162.32406214, 2791162.32114112, 2791162.32277927, 2791162.32680753, 2791162.32359466, 2791162.31715986, 2791162.43273995, 2791162.43609669, 2791162.43228038, 2791162.43026514, 2791162.43019254, 2791162.42065433, 2791165.78288008, 2791162.42082946, 2791162.42138195, 2791162.41389611, 2791162.41563357, 2791162.41845497, 2791162.41773703, 2791162.41787637, 2791162.4187504, 2791162.41651641, 2791162.4153813, 2791162.41653668, 2791162.41481415, 2791162.41964469, 2791162.41477361, 2791162.41328494, 2791162.41301816, 2791162.41301417, 2791162.4045612, 2791162.40596568, 2791162.38755189, 2791162.39311443, 2791162.40093757, 2791162.38179792, 2791162.38089839, 2791162.38206784, 2791162.38178915, 2791162.3808269, 2791162.38363636, 2791162.38168147, 2791162.38247946, 2791162.38038856),
Y = c(1152645.1057075, 1152645.10716654, 1152645.0986932, 1152645.10973225, 1152645.10268387, 1152645.10860166, 1152645.10285369, 1152645.10415144, 1152645.10949053, 1152645.10592819, 1152645.11141594, 1152645.10374256, 1152645.10746764, 1152645.1063395, 1152645.10800735, 1152645.10815158, 1152645.10766793, 1152645.10480103, 1152645.11599135, 1152645.11151243, 1152645.10463071, 1152645.10960652, 1152645.10975319, 1152645.11164281, 1152645.11013769, 1152645.11160747, 1152645.11252802, 1152645.10673275, 1152645.11099244, 1152645.10262804, 1152645.10838721, 1152645.11149681, 1152645.11408256,1152645.11280042, 1152644.37773542, 1152645.11690802, 1152645.11770136, 1152645.1125467, 1152645.11574864, 1152645.11438099, 1152645.12331471, 1152645.1152969, 1152645.11450451, 1152645.11354352, 1152645.11153381, 1152645.11427941, 1152645.11192852, 1152645.11176822, 1152645.12555403, 1152645.12672117, 1152645.12572363, 1152645.12744387, 1152645.12433327, 1152645.12610127, 1152645.12706131, 1152645.12156846, 1152645.12355019, 1152645.12250923, 1152645.11961065, 1152645.11727393, 1152645.12012378, 1152645.1200921, 1152645.11721301, 1152645.11850732, 1152645.11513263, 1152645.12240832, 1152645.12009354, 1152645.187559, 1152645.12752105, 1152645.12267048, 1152645.11869222, 1152645.12166756, 1152645.12614825, 1152645.12081258, 1152645.12522161, 1152645.12731805, 1152645.1251948, 1152645.1250067, 1152645.11414578, 1152645.12608457, 1152645.11437481, 1152645.11610481, 1152645.119979, 1152645.11283147, 1152645.1149272, 1152645.11784599, 1152645.11696344, 1152645.1241658, 1152645.12738042, 1152645.12265048, 1152645.13113493, 1152644.90542479, 1152644.90531955, 1152644.9137409, 1152644.91079601, 1152644.91118295, 1152644.93155628, 1152644.85931671, 1152644.92505568, 1152644.92365586, 1152644.9062049, 1152644.9072195, 1152644.91269895, 1152644.91120019, 1152644.91528026, 1152644.91661213, 1152644.91545375, 1152644.91216822, 1152644.91436215, 1152644.91334168, 1152644.9132155, 1152644.91259903, 1152644.91009809, 1152644.90909567, 1152644.91015364, 1152644.90557376, 1152644.90806689, 1152644.94956111, 1152644.94274402, 1152644.91456334, 1152644.96352573, 1152644.95826583, 1152644.95863937, 1152644.95850146, 1152644.96287929, 1152644.9612743, 1152644.96621915, 1152644.94007565, 1152644.96446478))),
`4` = structure(data.frame(Date = structure(c(17739, 17740, 17741,17742, 17743, 17744, 17745, 17746, 17747, 17748, 17749, 17750, 17751, 17752, 17753, 17754, 17755, 17757, 17758, 17759, 17760, 17761, 17762, 17763, 17764, 17766, 17767, 17768, 17769, 17770, 17772, 17773, 17774, 17775, 17776, 17777, 17778, 17779, 17780, 17781, 17782, 17783, 17784, 17785, 17786, 17787, 17789, 17790, 17791, 17792, 17793, 17794, 17795, 17796, 17797, 17798, 17799, 17800, 17801, 17802, 17803, 17804, 17805, 17806, 17807, 17808, 17809, 17810, 17811, 17812, 17813, 17814, 17815, 17816, 17817, 17818, 17819, 17820, 17821, 17822, 17823, 17824, 17825, 17826, 17827, 17828, 17829, 17830, 17831, 17832, 17833, 17834, 17835, 17836, 17837, 17838, 17839, 17840, 17841, 17843, 17844, 17846, 17847, 17848, 17849, 17850, 17851, 17852, 17853, 17854, 17856, 17858, 17860, 17862, 17864, 17866, 17868, 17870, 17872, 17874, 17876, 17878, 17880, 17882, 17884, 17888, 17890, 17892, 17894, 17896, 17898, 17900, 17904, 17910, 17912, 17914, 17916, 17918, 17920, 17922, 17924), class = "Date"),
X = c(2790824.18374809, 2790824.18858669, 2790824.18446804, 2790824.1888521, 2790824.18384519, 2790824.18617796, 2790824.18771725, 2790824.18443985, 2790824.18748079, 2790824.18308143, 2790824.18520752, 2790824.18257565, 2790824.18457724, 2790824.18600788, 2790824.18375888, 2790824.1847667, 2790824.18760882, 2790824.19052547, 2790824.18875072, 2790824.19017255, 2790824.19245601, 2790824.19081026, 2790824.19017255, 2790824.18495934, 2790824.18864228, 2790824.18773663, 2790824.18766243, 2790824.18412512, 2790824.18165805, 2790824.18411731, 2790824.1886425, 2790824.18711752, 2790825.7650905, 2790824.1887942, 2790824.18584957, 2790824.18555477, 2790824.18344282, 2790824.18663601, 2790824.18287909, 2790824.18046056, 2790824.18249738, 2790824.18103211, 2790824.18272872, 2790824.18214583, 2790824.18129456, 2790824.18119768, 2790824.19036244, 2790824.19282126, 2790824.19331333, 2790824.19533039, 2790824.18985039, 2790824.19113479, 2790824.1932507, 2790824.18973607, 2790824.18726316, 2790824.18820932, 2790824.18617466, 2790824.18598988, 2790824.18462304, 2790824.18537468, 2790824.18511984, 2790824.1834283, 2790824.1861561, 2790824.18377319, 2790824.18455121, 2790823.77616338, 2790824.18794645, 2790824.18470526, 2790824.18367412, 2790824.18490804, 2790824.18672408, 2790824.18804839, 2790824.18506072, 2790824.18476163, 2790824.18430831, 2790824.18437524, 2790824.18377374, 2790824.17576408, 2790824.18188471, 2790824.17497523, 2790824.17361097, 2790824.17991796, 2790824.18053179, 2790824.18515782, 2790824.18380764, 2790824.18674445, 2790824.18748905, 2790824.18360762, 2790824.17562747, 2790824.17512053, 2790824.29807747, 2790824.29977056, 2790824.29613796, 2790824.29415652, 2790824.28809071, 2790824.28362793, 2790824.28864387, 2790824.28424219, 2790824.27961904, 2790824.28421589, 2790824.28469113, 2790824.28450756, 2790824.28310669, 2790824.28250442, 2790824.28508087, 2790824.28348406, 2790824.28309668, 2790824.28288763, 2790824.28368468, 2790824.28541184, 2790824.2850932, 2790824.28369521, 2790824.27851666, 2790824.28289038, 2790824.28139771, 2790824.27608701, 2790824.25548963, 2790824.2638138, 2790824.27164658, 2790824.25281592, 2790824.25130503, 2790824.25128763, 2790824.25101947, 2790824.2521007, 2790824.25603123, 2790824.25821979, 2790824.25660884, 2790824.27185727, 2790824.26655444, 2790824.25487382, 2790824.24117284, 2790824.24537759, 2790824.23911296, 2790824.23060396, 2790824.25309687, 2790824.26019989, 2790824.26512535, 2790824.2679404, 2790824.26690672, 2790824.25377196, 2790824.26113459),
Y = c(1152771.25205373, 1152771.26044974, 1152771.25753943, 1152771.25978308, 1152771.2558363, 1152771.25737316, 1152771.26221167, 1152771.26008521, 1152771.26175994, 1152771.25406342, 1152771.25888205, 1152771.25536511, 1152771.25642348, 1152771.25771622, 1152771.25637725, 1152771.25125447, 1152771.26784701, 1152771.26538719, 1152771.25912007, 1152771.26242374, 1152771.2644979, 1152771.26553601, 1152771.26377529, 1152771.26209083, 1152771.26141445, 1152771.26246472, 1152771.2604169, 1152771.26242945, 1152771.25709542, 1152771.26461715, 1152771.26770187, 1152771.26539248, 1152770.44825641, 1152771.26541274, 1152771.26600154, 1152771.26392009, 1152771.26404865, 1152771.26264474, 1152771.26493265, 1152771.25979324, 1152771.26063346, 1152771.26042017, 1152771.25733911, 1152771.25714285, 1152771.25710758, 1152771.25825841, 1152771.26857731, 1152771.26889403, 1152771.26763328, 1152771.27012269, 1152771.26569212, 1152771.26643379, 1152771.26831453, 1152771.26453117, 1152771.2659026, 1152771.26211273, 1152771.26160017, 1152771.25949032, 1152771.25648998, 1152771.25802036, 1152771.2564697, 1152771.25572519, 1152771.26994309, 1152771.25856009, 1152771.25577385, 1152771.21190108, 1152771.26022874, 1152771.25793114, 1152771.25677626, 1152771.2583099, 1152771.26039744, 1152771.25265629, 1152771.25645023, 1152771.25709743, 1152771.25675924, 1152771.25475927, 1152771.25495515, 1152771.25653707, 1152771.25558733, 1152771.24623797, 1152771.24770836, 1152771.2549661, 1152771.25507885, 1152771.25924985, 1152771.2628605, 1152771.26486285, 1152771.26706399, 1152771.26320436, 1152771.25670899, 1152771.25969191, 1152771.04282095, 1152771.0422443, 1152771.04935005, 1152771.0453424, 1152771.03681296, 1152771.04392157, 1152771.04540486, 1152771.04963515, 1152771.02783388, 1152771.03612569, 1152771.03824284, 1152771.04113289, 1152771.04169045, 1152771.04252987, 1152771.04051328, 1152771.04079713, 1152771.03747317, 1152771.03739855, 1152771.03942934, 1152771.03740992, 1152771.0364367, 1152771.03294287, 1152771.03230096, 1152771.03823554, 1152771.03634142, 1152771.03405967, 1152771.07550426, 1152771.06546547, 1152771.03697854, 1152771.08281639, 1152771.07903828, 1152771.08392586, 1152771.0829198, 1152771.08307592, 1152771.0774657, 1152771.09114143, 1152771.07792026, 1152771.05372315, 1152771.0585986, 1152771.07327235, 1152771.08565988, 1152771.08055774, 1152771.09170355, 1152771.09050087, 1152771.05223544, 1152771.05222645, 1152771.05033638, 1152771.04887938, 1152771.04816407, 1152771.05337247, 1152771.06691153))))
ui <- fluidPage(
titlePanel("TestApp", windowTitle = "Test Shiny App"),
tabsetPanel(tabPanel("Tab1",
wellPanel(fluidRow(column(selectInput("Number", "Datset number:", names(df)), width = 1),
column(dateRangeInput('dateRange_df',
label = 'Select the time interval for plots',
start = "2018-07-25",
separator = " - ", format = "dd/mm/yy",
startview = 'year' , weekstart = 1), width = 4, offset = 3))),
fluidRow(column(plotOutput("plot1", height = 500, dblclick = "plot1_dblclick", brush = brushOpts(id = "plot1_brush", resetOnNew = TRUE)), width = 6),
column(plotOutput("plot2", height = 500, dblclick = "plot7_dblclick", brush = brushOpts(id = "plot7_brush", resetOnNew = TRUE)), width = 6)))))
server <- function(input,output, session){
# Plot1
ranges1 <- reactiveValues(x = NULL, y = NULL)
output$plot1 <- renderPlot({
if (!is.null(ranges1$x)) {
ranges1$x <- as.Date(ranges1$x, origin = "1970-01-01")
}
if(input$Number == "0"){
col = rgb(165,0,38, max = 255)
}
else if(input$Number == "4"){
col = rgb(215,48,39, max = 255)
}
p <- ggplot(df[[input$Number]], aes(x = Date, y = X)) +
geom_point(colour = col) + coord_cartesian(xlim = ranges1$x, ylim = ranges1$y, expand =FALSE)
p + labs(x = "", y = "X [m]", title = paste("Plot1 - Number ", input$Number))
p + theme(panel.background = element_rect(fill = "grey99"))
p + ggtitle(paste("Plot1 - Dataset number", input$Number))
})
observeEvent(input$plot1_dblclick, {
brush <- input$plot1_brush
if (!is.null(brush)) {
ranges1$x <- c(brush$xmin, brush$xmax)
ranges1$y <- c(brush$ymin, brush$ymax)
} else {
ranges1$x <- NULL
ranges1$y <- NULL
}
})
# Plot2
ranges2 <- reactiveValues(x = NULL, y = NULL)
output$plot2 <- renderPlot({
if (!is.null(ranges2$x)) {
ranges2$x <- as.Date(ranges2$x, origin = "1970-01-01")
}
if(input$Number == "0"){
col = rgb(165,0,38, max = 255)
}
else if(input$Number == "4"){
col = rgb(215,48,39, max = 255)
}
p <- ggplot(df[[input$Number]], aes(x = Date, y = Y)) +
geom_point(colour = col) + coord_cartesian(xlim = ranges2$x, ylim = ranges2$y, expand =FALSE)
p + labs(x = "", y = "Y [m]", title = paste("Plot 2 - Number ", input$Number))
p + theme(panel.background = element_rect(fill = "grey99"))
p + ggtitle(paste("Plot2 - Dataset number ", input$Number))
})
observeEvent(input$plot2_dblclick, {
brush <- input$plot2_brush
if (!is.null(brush)) {
ranges2$x <- c(brush$xmin, brush$xmax)
ranges2$y <- c(brush$ymin, brush$ymax)
} else {
ranges2$x <- NULL
ranges2$y <- NULL
}
})
}
shinyApp(ui, server)
Add an observer to your dateRangeInput that changes the reactive values to match the user input. As long as the user input does not change, the reactive values can change through plot manipulation; when the user inputs a new date range, the zoom resets.
observeEvent(input$dateRange_df, {
ranges1$x <- input$dateRange_df
ranges2$x <- input$dateRange_df
})

Resources