Plot time series without straight lines - r

I am trying to plot some time series but since I have data only for the summer I get these straight lines. Any idea how to fix that? The code I used: Any idea would be helpful!
ggplot(ba, aes(x=date1, y=pc1)) +
geom_line(color="turquoise4") +
theme_minimal() +
labs(x="", y="Loading", title="Correlation of PC1 and original series") +
theme(plot.title = element_text(hjust=0.5, size=20, face="bold"))+
scale_x_date(date_labels= ("%Y"))
And the plot:
The data: structure(list(date1 = structure(c(10712, 10713, 10714, 10715,
10716, 10717, 10718, 10719, 10720, 10721, 10722, 10723, 10724,
10725, 10726, 10727, 10728, 10729, 10730, 10731, 10732, 10733,
10734, 10735, 10736, 10737, 10738, 10739, 10740, 10741, 10742,
10743, 10744, 10745, 10746, 10747, 10748, 10749, 10750, 10751,
10752, 10753, 10754, 10755, 10756, 10757, 10758, 10759, 10760,
10761, 10762, 10763, 10764, 10765, 10766, 10767, 10768, 10769,
10770, 10771, 10772, 10773, 10774, 10775, 10776, 10777, 10778,
10779, 10780, 10781, 10782, 10783, 10784, 10785, 10786, 10787,
10788, 10789, 10790, 10791, 10792, 10793, 10794, 10795, 10796,
10797, 10798, 10799, 10800, 10801, 10802, 10803, 10804, 10805,
10806, 10807, 10808, 10809, 10810, 10811, 10812, 10813, 10814,
10815, 10816, 10817, 10818, 10819, 10820, 10821, 10822, 10823,
10824, 10825, 10826, 10827, 10828, 10829, 10830, 10831, 10832,
10833, 10834, 10835, 10836, 10837, 10838, 10839, 10840, 10841,
10842, 10843, 10844, 10845, 10846, 10847, 10848, 10849, 10850,
10851, 10852, 10853, 10854, 10855, 10856, 10857, 10858, 10859,
10860, 10861, 10862, 10863, 10864, 11078, 11079, 11080, 11081,
11082, 11083, 11084, 11085, 11086, 11087, 11088, 11089, 11090,
11091, 11092, 11093, 11094, 11095, 11096, 11097, 11098, 11099,
11100, 11101, 11102, 11103, 11104, 11105, 11106, 11107, 11108,
11109, 11110, 11111, 11112, 11113, 11114, 11115, 11116, 11117,
11118, 11119, 11120, 11121, 11122, 11123, 11124), class = "Date"),
pc1 = c(2.64462123197862, 2.4380313244096, 2.21417935009087,
2.02249236956036, 1.75829175459456, 1.58770371446918, 1.62230139615394,
1.73502227021784, 1.75083678213192, 1.64509065138032, 1.57921033180313,
1.70228767677341, 1.77303175099386, 1.78384290706931, 1.86580160595479,
1.9106874120324, 1.73936455049801, 1.50577059168685, 1.24226003967481,
1.07813468676617, 1.06276891964951, 1.09622663209529, 1.07692457712675,
0.978692818737612, 1.06365064520783, 1.2525349982313, 1.08237838015766,
0.645239033194787, 0.479482241789711, 0.683701830568681,
0.792197472275541, 0.631531270886538, 0.520337262457156,
0.667200695099021, 0.767559380073353, 0.7856163663635, 0.737745147101418,
0.654712633988225, 0.440140874164089, 0.111631055132755,
-0.22450806112272, -0.444238159039355, -0.584576558346287,
-0.444097467542865, -0.227821057355029, -0.120935149111578,
-0.0932195161137341, 0.037283855810637, 0.206479031035409,
0.173515424607062, 0.234536409515456, 0.317957256707112,
0.290090191780606, 0.0607339338833623, -0.27556992053308,
-0.3586166955826, -0.3534130521313, -0.501651666926942, -0.571570071652576,
-0.79110428934397, -0.985635595643097, -0.994138228085185,
-0.839909782593256, -0.699274458194957, -0.580683825031177,
-0.530811870371419, -0.4746353951302, -0.489386570992314,
-0.787222651887671, -1.1059671054324, -1.17983265148469,
-1.1058432515423, -0.970485807735322, -0.679713450749357,
-0.516950863200668, -0.495312393712548, -0.673645368786615,
-0.792675131421433, -0.692021409445821, -0.611096320716252,
-0.676712376641795, -0.723244566814595, -0.621986199057006,
-0.563969216349158, -0.649311354664407, -0.679237194242732,
-0.624476984795223, -0.738344795218295, -0.877867797047079,
-0.879375052767018, -0.84262582765393, -0.845707036138972,
-0.959691974084994, -1.06904324062176, -0.97905489332525,
-0.847145240762566, -0.86837819324592, -0.935323976060101,
-0.796486491787169, -0.461073031709012, -0.275818888900351,
-0.513613296467615, -0.786611502858454, -0.799843667083875,
-0.632676241199403, -0.468611824279096, -0.534017599627378,
-0.501551518704511, -0.239313348556757, -0.208935210151003,
-0.510483950549102, -0.62974750963569, -0.399113422985878,
-0.072812659658845, 0.0377885597304766, -0.0102829082610216,
-0.0571349366394233, -0.101917027852624, -0.202941574141862,
-0.22849727264844, -0.125157862652187, 0.168703915373856,
0.43626132948925, 0.446099489882147, 0.435379929023588, 0.236210503991287,
-0.122289033919648, -0.288101855449495, -0.186400543130663,
0.0316721901308679, 0.121240481805255, -0.0753698973566349,
-0.384779730900963, -0.531179497125517, -0.373632181420806,
-0.0148926315001478, 0.146040939981569, 0.13371186468668,
0.200262938351445, 0.465073170745138, 0.506805629621484,
0.345398737766814, 0.171110245173291, 0.176555396235594,
0.262743070740985, 0.398601589660576, 0.433248104072272,
0.453883432665361, 0.604637145172226, 0.843278371818699,
1.13506306230201, 1.42652005730684, 1.63221068108998, 1.86442509826484,
1.97067279998339, 2.0139860665512, 2.13720187260212, 2.31355711206366,
2.32477728002809, 2.36236228869303, 2.24108767618426, 2.12991693141636,
2.11677885248848, 2.01466853738993, 1.77967782944265, 1.48938981000699,
1.34042958586002, 1.33016846412245, 1.31770813627339, 1.26104969519401,
1.37385446004522, 1.61517275597383, 1.84510291043685, 1.91280500843462,
1.84897419443657, 1.52674793906846, 1.29429812528379, 1.06717755247561,
0.910500917679731, 0.904461327314293, 1.05380123048097, 1.08631739987863,
1.04843964584885, 1.10153891962662, 1.15936307711726, 1.20129772010444,
1.18746954945955, 1.00056619329093, 0.725225823060771, 0.573790799694267,
0.655776789864271, 0.780033607405981, 0.664875593837605,
0.452000300833336, 0.394589410057676, 0.402170545544567,
0.403979206396259, 0.395485848597801, 0.433314713756909,
0.437960603442615)), row.names = c(NA, 200L), class = "data.frame")strong text

1) Using the input in the Note at the end expand the dates to include the missing ones using NA's for them. Then plot.
library(ggplot2)
library(zoo)
z <- read.zoo(ba)
zz <- merge(z, zoo(, seq(start(z), end(z), 1)))
autoplot(zz) + xlab("")
2) Another approach is to use distinct facets for each year.
library(ggplot2)
breaks <- unique(as.Date(cut(ba$date1, "month")))
ba2 <- transform(ba, year = as.integer(format(date1, "%Y")))
p <- ggplot(ba2, aes(date1, pc1)) +
geom_line() +
facet_grid(cols = vars(year), scales = "free_x", space = "free_x")
p + scale_x_date(breaks = breaks, date_labels = "%b")
(continued after image)
or to remove strip text and only place the year below each facet use p from above with
breaks <- as.Date(tapply(format(ba$date1), format(ba$date1, "%Y"), min))
p +
scale_x_date(breaks = breaks, date_labels = "%Y") +
theme(strip.text.x = element_blank())
3) The facet idea could also be implemented in lattice.
library(lattice)
ba3 <- transform(ba, year = format(date1, "%Y"))
xyplot(pc1 ~ date1 | year, ba3, type = "l",
scales = list(x = list(relation = "free")), layout = c(NA, 1))
Note
There was a problem with the dput output in the question so the following was used.
ba <-
structure(list(date1 = structure(c(10712, 10713, 10714, 10715,
10716, 10717, 10718, 10719, 10720, 10721, 10722, 10723, 10724,
10725, 10726, 10727, 10728, 10729, 10730, 10731, 10732, 10733,
10734, 10735, 10736, 10737, 10738, 10739, 10740, 10741, 10742,
10743, 10744, 10745, 10746, 10747, 10748, 10749, 10750, 10751,
10752, 10753, 10754, 10755, 10756, 10757, 10758, 10759, 10760,
10761, 10762, 10763, 10764, 10765, 10766, 10767, 10768, 10769,
10770, 10771, 10772, 10773, 10774, 10775, 10776, 10777, 10778,
10779, 10780, 10781, 10782, 10783, 10784, 10785, 10786, 10787,
10788, 10789, 10790, 10791, 10792, 10793, 10794, 10795, 10796,
10797, 10798, 10799, 10800, 10801, 10802, 10803, 10804, 10805,
10806, 10807, 10808, 10809, 10810, 10811, 10812, 10813, 10814,
10815, 10816, 10817, 10818, 10819, 10820, 10821, 10822, 10823,
10824, 10825, 10826, 10827, 10828, 10829, 10830, 10831, 10832,
10833, 10834, 10835, 10836, 10837, 10838, 10839, 10840, 10841,
10842, 10843, 10844, 10845, 10846, 10847, 10848, 10849, 10850,
10851, 10852, 10853, 10854, 10855, 10856, 10857, 10858, 10859,
10860, 10861, 10862, 10863, 10864, 11078, 11079, 11080, 11081,
11082, 11083, 11084, 11085, 11086, 11087, 11088, 11089, 11090,
11091, 11092, 11093, 11094, 11095, 11096, 11097, 11098, 11099,
11100, 11101, 11102, 11103, 11104, 11105, 11106, 11107, 11108,
11109, 11110, 11111, 11112, 11113, 11114, 11115, 11116, 11117,
11118, 11119, 11120, 11121, 11122, 11123, 11124), class = "Date"),
pc1 = c(2.64462123197862, 2.4380313244096, 2.21417935009087,
2.02249236956036, 1.75829175459456, 1.58770371446918, 1.62230139615394,
1.73502227021784, 1.75083678213192, 1.64509065138032, 1.57921033180313,
1.70228767677341, 1.77303175099386, 1.78384290706931, 1.86580160595479,
1.9106874120324, 1.73936455049801, 1.50577059168685, 1.24226003967481,
1.07813468676617, 1.06276891964951, 1.09622663209529, 1.07692457712675,
0.978692818737612, 1.06365064520783, 1.2525349982313, 1.08237838015766,
0.645239033194787, 0.479482241789711, 0.683701830568681,
0.792197472275541, 0.631531270886538, 0.520337262457156,
0.667200695099021, 0.767559380073353, 0.7856163663635, 0.737745147101418,
0.654712633988225, 0.440140874164089, 0.111631055132755,
-0.22450806112272, -0.444238159039355, -0.584576558346287,
-0.444097467542865, -0.227821057355029, -0.120935149111578,
-0.0932195161137341, 0.037283855810637, 0.206479031035409,
0.173515424607062, 0.234536409515456, 0.317957256707112,
0.290090191780606, 0.0607339338833623, -0.27556992053308,
-0.3586166955826, -0.3534130521313, -0.501651666926942, -0.571570071652576,
-0.79110428934397, -0.985635595643097, -0.994138228085185,
-0.839909782593256, -0.699274458194957, -0.580683825031177,
-0.530811870371419, -0.4746353951302, -0.489386570992314,
-0.787222651887671, -1.1059671054324, -1.17983265148469,
-1.1058432515423, -0.970485807735322, -0.679713450749357,
-0.516950863200668, -0.495312393712548, -0.673645368786615,
-0.792675131421433, -0.692021409445821, -0.611096320716252,
-0.676712376641795, -0.723244566814595, -0.621986199057006,
-0.563969216349158, -0.649311354664407, -0.679237194242732,
-0.624476984795223, -0.738344795218295, -0.877867797047079,
-0.879375052767018, -0.84262582765393, -0.845707036138972,
-0.959691974084994, -1.06904324062176, -0.97905489332525,
-0.847145240762566, -0.86837819324592, -0.935323976060101,
-0.796486491787169, -0.461073031709012, -0.275818888900351,
-0.513613296467615, -0.786611502858454, -0.799843667083875,
-0.632676241199403, -0.468611824279096, -0.534017599627378,
-0.501551518704511, -0.239313348556757, -0.208935210151003,
-0.510483950549102, -0.62974750963569, -0.399113422985878,
-0.072812659658845, 0.0377885597304766, -0.0102829082610216,
-0.0571349366394233, -0.101917027852624, -0.202941574141862,
-0.22849727264844, -0.125157862652187, 0.168703915373856,
0.43626132948925, 0.446099489882147, 0.435379929023588, 0.236210503991287,
-0.122289033919648, -0.288101855449495, -0.186400543130663,
0.0316721901308679, 0.121240481805255, -0.0753698973566349,
-0.384779730900963, -0.531179497125517, -0.373632181420806,
-0.0148926315001478, 0.146040939981569, 0.13371186468668,
0.200262938351445, 0.465073170745138, 0.506805629621484,
0.345398737766814, 0.171110245173291, 0.176555396235594,
0.262743070740985, 0.398601589660576, 0.433248104072272,
0.453883432665361, 0.604637145172226, 0.843278371818699,
1.13506306230201, 1.42652005730684, 1.63221068108998, 1.86442509826484,
1.97067279998339, 2.0139860665512, 2.13720187260212, 2.31355711206366,
2.32477728002809, 2.36236228869303, 2.24108767618426, 2.12991693141636,
2.11677885248848, 2.01466853738993, 1.77967782944265, 1.48938981000699,
1.34042958586002, 1.33016846412245, 1.31770813627339, 1.26104969519401,
1.37385446004522, 1.61517275597383, 1.84510291043685, 1.91280500843462,
1.84897419443657, 1.52674793906846, 1.29429812528379, 1.06717755247561,
0.910500917679731, 0.904461327314293, 1.05380123048097, 1.08631739987863,
1.04843964584885, 1.10153891962662, 1.15936307711726, 1.20129772010444,
1.18746954945955, 1.00056619329093, 0.725225823060771, 0.573790799694267,
0.655776789864271, 0.780033607405981, 0.664875593837605,
0.452000300833336, 0.394589410057676, 0.402170545544567,
0.403979206396259, 0.395485848597801, 0.433314713756909,
0.437960603442615)), row.names = c(NA, -200L), class = "data.frame")

Related

How to overlay a 2d density plot on top of a map

I need some help in overlaying a 2d density plot on top of a ggmap plot. I don't really know how to procede. Any help is welcome.
This is a subset of my data, including start and end coordinates for each individual ride of a fictional bike sharing company:
df <- structure(list(start_lat = c(41.94018, 41.890762, 41.845695,
41.857813, 41.9287386666667, 42.0044803333333, 41.879255, 41.886835,
41.874734, 41.95469, 41.95, 41.8809518333333, 41.96590013976,
41.909668, 41.931248, 41.96167, 41.912133, 41.87947235235, 41.936266,
41.922695, 41.9101756666667, 41.86, 41.91468, 41.892278, 42.03,
41.911386, 41.9716, 41.93, 41.940195, 41.9560855, 41.915784,
41.93314, 41.943739, 41.8671848333333, 41.87464, 41.882242, 41.926277,
41.96167, 41.76, 41.883668, 41.967096, 41.8, 41.9024035, 41.939743,
41.9093960065, 41.915983, 41.87772613, 41.8984238333333, 41.8836331666667,
41.925905, 41.967096, 41.92, 41.884576228, 41.838499, 41.9028846666667,
41.89993001, 41.8, 41.866095, 41.97, 41.9093960065, 41.88, 41.8922376666667,
41.81, 41.9438251666667, 41.883668, 41.9207793333333, 41.954383,
41.9434726666667, 41.8945555, 41.911386, 41.88917683258, 41.86722595682,
41.8531223333333, 41.92, 41.919936, 41.90096, 41.894722, 41.872187,
41.881892, 41.920082, 41.897448, 41.88917683258, 41.9, 41.925858,
41.89, 41.8908470406238, 41.85, 41.890173, 41.92556258, 41.885637,
41.9030376666667, 41.93314, 41.838198, 41.892278, 41.93, 41.894722,
41.90345, 41.6922943333333, 41.9080621666667, 42.025784), start_lng = c(-87.65304,
-87.631697, -87.6225141666667, -87.62455, -87.6538015, -87.6615086666667,
-87.639904, -87.62232, -87.6498425, -87.67393, -87.71, -87.6167566666667,
-87.6936384935, -87.648128, -87.644336, -87.65464, -87.634656,
-87.6256886059, -87.652662, -87.697153, -87.6823075, -87.62,
-87.64332, -87.612043, -87.71, -87.638677, -87.650154, -87.74,
-87.6529666666667, -87.668857, -87.634581, -87.64776, -87.66402,
-87.6260033333333, -87.65703, -87.641066, -87.630834, -87.65464,
-87.58, -87.64867, -87.667429, -87.58, -87.6277486666667, -87.658865,
-87.6776919292, -87.677335, -87.65478743, -87.6223878333333,
-87.629143, -87.64926, -87.667429, -87.7, -87.63188991, -87.6080766666667,
-87.6874035, -87.63443007, -87.59, -87.607267, -87.71, -87.6776919292,
-87.63, -87.6119485, -87.61, -87.671138, -87.64867, -87.6637163333333,
-87.648043, -87.6796343333333, -87.6534645, -87.638677, -87.6385057718,
-87.6153553902, -87.6318963333333, -87.74, -87.64883, -87.623777,
-87.634362, -87.661501, -87.648789, -87.677855, -87.628722, -87.6385057718,
-87.62, -87.638973, -87.66, -87.6186168193817, -87.72, -87.626185,
-87.65840426, -87.641823, -87.631299, -87.64776, -87.645143,
-87.612043, -87.71, -87.634362, -87.667747, -87.6426485, -87.6315093333333,
-87.684107), end_lat = c(41.918306, 41.886875, 41.8456825, 41.8530845574128,
41.890831, 41.99, 41.885637, 41.881319815, 41.88, 41.961068,
41.93, 41.880958, 41.966399801841, 41.89766, 41.9267559875, 41.9578665241517,
41.911386, 41.867888, 41.95078, 41.932588, 41.9245285, 41.8776751666667,
41.9105780349, 41.9239313113662, 42.0192226666667, 41.904613,
41.9947796884, 41.96, 41.9296915, 41.94, 41.94, 41.907066, 41.923931,
41.8707831666667, 41.87772613, 41.872187, 41.892278, 41.961004,
41.7689161666667, 41.8793563587, 41.95078, 41.79, 41.882242,
41.932225, 41.912133, 41.9093960065, 41.8810317, 41.9, 41.89,
41.912133, 41.926277, 41.93190196886, 41.874053, 41.8368228333333,
41.882754, 41.894666, 41.76, 41.882134, 41.96, 41.89637337, 41.87,
41.8787191666667, 41.8, 41.91, 41.917805, 41.88, 41.926277, 41.93,
41.8990156666667, 41.890762, 41.8854833079, 41.874754, 41.85,
41.94, 41.920771, 41.894345, 41.94334, 41.871737, 41.88338, 41.92154,
41.882134, 41.902997, 41.876243, 41.892278, 41.89, 41.886024,
41.86, 41.8918473721099, 41.9093960065, 41.8854833079, 41.89,
41.92883, 41.834734, 41.891466, 41.9296816666667, 41.902973,
41.918491153687, 41.75, 41.9218326666667, 41.9840446107), end_lng = c(-87.636282,
-87.62603, -87.6224476666667, -87.6319313049316, -87.6313945,
-87.66, -87.641823, -87.6295209193, -87.65, -87.695439, -87.71,
-87.616743, -87.6887042820454, -87.62351, -87.6344287848, -87.6495051383972,
-87.638677, -87.623041, -87.659172, -87.636427, -87.658447, -87.6240391666667,
-87.6494219288, -87.6358245313168, -87.6736431666667, -87.640552,
-87.6602845349, -87.69, -87.7080808333333, -87.67, -87.68, -87.667252,
-87.635825, -87.6257745, -87.65478743, -87.661501, -87.612043,
-87.649603, -87.634775, -87.6297910363, -87.659172, -87.6, -87.641066,
-87.658617, -87.634656, -87.6776919292, -87.62408432, -87.62,
-87.63, -87.634656, -87.630834, -87.7011951301, -87.627716, -87.6133453333333,
-87.6259215, -87.638437, -87.55, -87.625125, -87.69, -87.66098386,
-87.62, -87.6355345, -87.59, -87.66, -87.682437, -87.63, -87.630834,
-87.71, -87.6299358333333, -87.631697, -87.6523048564, -87.649807,
-87.64, -87.73, -87.663712, -87.622798, -87.67097, -87.65103,
-87.64117, -87.653818, -87.625125, -87.683825, -87.624426, -87.612043,
-87.65, -87.624117, -87.72, -87.6205801963806, -87.6776919292,
-87.6523048564, -87.63, -87.668507, -87.625813, -87.626761, -87.7081071666667,
-87.63128, -87.6974228024483, -87.64, -87.6439593333333, -87.6602738295
)), row.names = c(NA, -100L), class = "data.frame")
Next, i extracted min and max values for latitude and longitude and then used those values as limits for my map plot:
library(ggmap)
library(ggplot2)
map_lim <- data.frame(
min_lat = min(df[, c("start_lat", "end_lat")]),
max_lat = max(df[, c("start_lat", "end_lat")]),
min_lng = min(df[, c("start_lng", "end_lng")]),
max_lng = max(df[, c("start_lng", "end_lng")])
)
map_lim
## min_lat max_lat min_lng max_lng
## 1 41.69229 42.03 -87.74 -87.55
map <- get_stamenmap(
bbox = c(left = map_lim$min_lng, right = map_lim$max_lng, bottom = map_lim$min_lat, top = map_lim$max_lat)
)
ggmap(map)
This is the resulting map:
Now i want to add a 2d density plot based on the same set of coordinates on top of this map, but i don't know how to do it. This is my 2d density plot code.
density2d <- ggplot(df, aes(x = start_lng, y = start_lat)) +
coord_equal(xlim = c(map_lim$min_lng, map_lim$max_lng), ylim = c(map_lim$min_lat, map_lim$max_lat)) +
xlab("Longitude") +
ylab("Latitude") +
stat_density2d(aes(fill = ..level..), alpha = 0.5, geom = "polygon") +
scale_fill_viridis_c()
density2d
You simply add the density layer to your map:
ggmap(map) +
stat_density2d(data = df, aes(x = start_lng, y = start_lat,
fill = ..level..), alpha = 0.5, geom = "polygon") +
scale_fill_viridis_c()

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")

Add a 3d surface at point 0 in plotly

I have a 3D plot using plotly which can take on negative and positive values. I would like to add a see through 3D flat surface at point 0 (see through here means setting alpha = 0.3 as in ggplot2) in order to emphasize the positive/negative values a little more. Alternatively adding a "grid" as in the background through the point 0 would be useful.
Code:
library(plotly)
library(dplyr)
df %>%
select(date, everything()) %>%
plot_ly(
x = colnames(subset(., select = c(2:4))), # probably not the most efficient method
y = ~date,
z = data.matrix(subset(., select = c(2:4))),
type = "surface",
colors = c("darkblue", "yellow", "darkred")
)
Data:
df <- structure(list(wind = c(0.938535690307617, 0.976551234722137,
0.954551994800568, 0.920722126960754, 0.889751732349396, 0.701366603374481,
0.718878328800201, 0.685763895511627, 0.677822828292847, 0.876205325126648,
0.67054146528244, 0.738650739192963, 0.725420415401459, 0.851324200630188,
0.6589714884758, 0.880357265472412, 0.997677683830261, 0.968335390090942,
0.882899045944214, 1.09453165531158, 1.06842839717865, 0.819347560405731,
0.995919525623322, 0.695173263549805, 0.860199570655823, 0.977508246898651,
0.807886302471161, 0.778182446956635, 0.886279463768005, 0.879809498786926,
0.903579652309418, 1.09579062461853, 1.03768181800842, 0.802022874355316,
0.928451955318451, 0.744936227798462, 0.621560990810394, 0.706887602806091,
0.749234974384308, 0.746754884719849, 0.65381270647049, 0.710927248001099,
0.517793655395508, 0.477172255516052, 0.631662607192993, 0.55011123418808,
0.584086775779724, 0.617783904075623, 0.438043504953384, 0.577566087245941,
0.684398949146271, 1.00848543643951, 0.957233726978302, 0.84071296453476,
0.90149587392807, 0.888661623001099, 0.923080563545227, 0.945727407932281,
0.965020060539246, 1.07903909683228, 0.870955109596252, 0.934546709060669,
0.93314516544342, 0.926109850406647, 0.981102645397186, 0.770903468132019,
0.829349219799042, 0.980291783809662, 1.00809383392334, 0.821328639984131,
0.89130437374115, 0.886775732040405, 0.896964132785797, 1.08184945583344,
1.17395043373108, 1.11537420749664, 1.15379846096039, 1.20203125476837,
1.12583827972412, 1.10014677047729, 0.891100168228149, 1.01484513282776,
1.01270127296448, 0.895487844944, 0.877909421920776, 1.11780989170074,
1.18047833442688, 1.14531397819519, 1.28634309768677, 1.23702371120453,
1.01186645030975, 1.15563869476318, 1.0918824672699, 1.2023059129715,
1.11806273460388, 1.11690294742584, 1.13390302658081, 1.10064888000488,
1.1418149471283, 1.06309700012207), holiday = c(-0.323977619409561,
-0.32814821600914, -0.335260361433029, -0.355545252561569, -0.347469061613083,
-0.349574476480484, -0.331554502248764, -0.351303607225418, -0.342486947774887,
-0.34791961312294, -0.342795491218567, -0.344868391752243, -0.344075173139572,
-0.342763870954514, -0.365125387907028, -0.301046937704086, -0.303365021944046,
-0.301353365182877, -0.316689401865005, -0.312132269144058, -0.317412197589874,
-0.331865310668945, -0.306569844484329, -0.306523144245148, -0.328399240970612,
-0.328934848308563, -0.340233236551285, -0.310919582843781, -0.352815061807632,
-0.324335247278214, -0.333071410655975, -0.25605234503746, -0.28141376376152,
-0.267955660820007, -0.261935144662857, -0.23365119099617, -0.204256281256676,
-0.271868377923965, -0.266900211572647, -0.268673747777939, -0.246791422367096,
-0.261210560798645, -0.275096118450165, -0.272033154964447, -0.307673662900925,
-0.316463977098465, -0.315838783979416, -0.278884381055832, -0.29575651884079,
-0.300951153039932, -0.275152236223221, -0.292507320642471, -0.283683449029922,
-0.333169460296631, -0.323910266160965, -0.317758291959763, -0.318425863981247,
-0.318803340196609, -0.316879868507385, -0.321441829204559, -0.342198520898819,
-0.336640536785126, -0.334549427032471, -0.304668426513672, -0.281678169965744,
-0.287939876317978, -0.278673946857452, -0.299236595630646, -0.295697629451752,
-0.297617554664612, -0.300621718168259, -0.294943511486053, -0.291149199008942,
-0.263380706310272, -0.264828890562057, -0.260132044553757, -0.294039487838745,
-0.284087061882019, -0.27564924955368, -0.275646597146988, -0.294898957014084,
-0.290615618228912, -0.264040410518646, -0.267250239849091, -0.27454400062561,
-0.22650308907032, -0.220207497477531, -0.233633011579514, -0.205283910036087,
-0.201104089617729, -0.235431581735611, -0.252072185277939, -0.257701843976974,
-0.233586445450783, -0.239779070019722, -0.233227252960205, -0.26639312505722,
-0.256356805562973, -0.248022571206093, -0.285306513309479),
month_10 = c(0.109010718762875, 0.112969301640987, 0.110368527472019,
0.107052445411682, 0.0947703272104263, 0.109930463135242,
0.106371931731701, 0.114236004650593, 0.102411419153214,
0.0184143912047148, 0.0301767271012068, 0.0376978516578674,
0.0472327470779419, 0.0622735135257244, 0.043902475386858,
0.0611664243042469, 0.0618763938546181, 0.0555795393884182,
0.0623081848025322, 0.0636096075177193, 0.0675770491361618,
0.0629641935229301, 0.0288578178733587, 0.025121470913291,
0.0300221722573042, 0.0611664243042469, 0.0659252777695656,
0.0159557648003101, 0.0376978516578674, 0.032174538820982,
0.0317839048802853, 0.0626140907406807, 0.0577763170003891,
0.0571180321276188, 0.0571180321276188, 0.0590739175677299,
0.0760120898485184, 0.0670360922813416, 0.0699110627174377,
0.0717969089746475, 0.0640148967504501, 0.0670360922813416,
0.164964601397514, 0.152724280953407, 0.115071900188923,
0.129654854536057, 0.122471310198307, 0.0768138542771339,
0.0400041155517101, 0.0573667995631695, 0.0685276389122009,
0.0337212830781937, 0.0616994015872478, 0.0589617975056171,
0.059433214366436, 0.0567467175424099, 0.0661386772990227,
0.0704409778118134, 0.0611028559505939, 0.0579087659716606,
0.0622764676809311, 0.0538184903562069, 0.0553129874169827,
0.058912742882967, 0.059433214366436, 0.0575473643839359,
0.0575473643839359, 0.058104183524847, 0.055678017437458,
0.0575473643839359, 0.059433214366436, 0.0603105537593365,
0.0589617975056171, 0.0601625964045525, 0.0577935017645359,
0.0574705749750137, 0.0550192892551422, 0.0475285314023495,
0.0577935017645359, 0.0543489865958691, 0.0526755712926388,
0.0553129874169827, 0.0589617975056171, 0.0593133755028248,
0.0547068528831005, 0.0676216259598732, 0.0511428378522396,
0.0564789660274982, 0.0641900449991226, 0.0666491389274597,
0.0390096306800842, 0.058005329221487, 0.0449355207383633,
0.062143836170435, 0.0591179206967354, 0.0637796893715858,
0.0633300691843033, 0.0702810436487198, 0.0647093132138252,
0.0427459664642811), month_12 = c(0.0733551606535912, 0.0707022771239281,
0.0707631036639214, 0.0718683376908302, 0.071540854871273,
0.0993443354964256, 0.0973133370280266, 0.0668289735913277,
0.0731536969542503, 0.0717423185706139, 0.0750747397542,
0.0754749700427055, 0.0746353641152382, 0.0912141725420952,
0.100288398563862, 0.07671108096838, 0.0708827450871468,
0.0708827450871468, 0.0741974636912346, 0.0761196836829185,
0.0648202076554298, 0.114817388355732, 0.100519739091396,
0.100288398563862, 0.102664910256863, 0.0566539540886879,
0.0648743882775307, 0.0706770494580269, 0.0746353641152382,
0.0750747397542, 0.0750747397542, 0.113439425826073, 0.127338454127312,
0.114467553794384, 0.112322382628918, 0.136203452944756,
0.131634846329689, 0.144591823220253, 0.139709115028381,
0.140504062175751, 0.145617410540581, 0.166768744587898,
0.151759415864944, 0.151587069034576, 0.156320676207542,
0.131974145770073, 0.135847419500351, 0.159762278199196,
0.163790658116341, 0.158604919910431, 0.127949997782707,
0.109744042158127, 0.101787634193897, 0.0582009926438332,
0.0671374276280403, 0.0669510439038277, 0.074992410838604,
0.074992410838604, 0.0787725821137428, 0.0696783438324928,
0.0965322777628899, 0.0666131302714348, 0.0666131302714348,
0.0925824269652367, 0.0875711515545845, 0.0873847678303719,
0.0873847678303719, 0.0711322501301765, 0.0696783438324928,
0.0667495802044868, 0.0667495802044868, 0.0685289725661278,
0.0582009926438332, 0.0599979534745216, 0.0702869072556496,
0.068938173353672, 0.0610831864178181, 0.074992410838604,
0.0702869072556496, 0.0683296099305153, 0.0671374276280403,
0.0666131302714348, 0.0815844461321831, 0.070037417113781,
0.068938173353672, 0.0862899348139763, 0.0824234709143639,
0.0761196836829185, 0.0663527771830559, 0.0751885995268822,
0.072908379137516, 0.0599117167294025, 0.0743952021002769,
0.0666681602597237, 0.0667495802044868, 0.0748060271143913,
0.0800538137555122, 0.0787725821137428, 0.0741974636912346,
0.0741937384009361), date = structure(c(14610, 14611, 14612,
14613, 14614, 14615, 14616, 14617, 14618, 14619, 14620, 14621,
14622, 14623, 14624, 14625, 14626, 14627, 14628, 14629, 14630,
14631, 14632, 14633, 14634, 14635, 14636, 14637, 14638, 14639,
14640, 14641, 14642, 14643, 14644, 14645, 14646, 14647, 14648,
14649, 14650, 14651, 14652, 14653, 14654, 14655, 14656, 14657,
14658, 14659, 14660, 14661, 14662, 14663, 14664, 14665, 14666,
14667, 14668, 14669, 14670, 14671, 14672, 14673, 14674, 14675,
14676, 14677, 14678, 14679, 14680, 14681, 14682, 14683, 14684,
14685, 14686, 14687, 14688, 14689, 14690, 14691, 14692, 14693,
14694, 14695, 14696, 14697, 14698, 14699, 14700, 14701, 14702,
14703, 14704, 14705, 14706, 14707, 14708, 14709), class = "Date")), row.names = c(NA,
100L), class = "data.frame")
Adding a plane at the value of 0 for z:
library(plotly)
library(dplyr)
# I separated your df wrangling to better understand the data
df <- df %>%
select(date, everything())
# Code you posted
df %>%
plot_ly(
x = colnames(subset(., select = c(2:4))), # probably not the most efficient method
y = ~date,
z = data.matrix(subset(., select = c(2:4))),
type = "surface",
colors = c("darkblue", "yellow", "darkred")
) %>%
# The surface added by using y & x from df
# and making z a matrix of zeros 3 rows by 100 columns.
add_surface(z = matrix(0, ncol = 3, nrow = 100),
y = df$date,
x = colnames(subset(df, select = c(2:4))),
opacity = .8)
Your plot:
With added zero z plane:

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

axis ticks with scale_x_break: more ticks with only some labeled

So I have a plot where I want the x-axis to show specific dates starting at the 16-06-2016 through to the 04-08-2016, with week dates labeled on the x-axis. This so far I have managed to do - however, I would also like there to be blank tick marks per day, as well as the week labels - but I am not sure I can apply multiple scale_x_date(breaks =) conditions.
Any help on how to add the additional tick marks would be appreciated!
Dummy data set to play with:
library(ggplot2)
library(reshape2)
#some data
df <- structure(list(Date = structure(c(16968, 16969, 16970, 16971,
16972, 16973, 16974, 16975, 16976, 16977, 16978, 16979, 16980,
16981, 16982, 16983, 16984, 16985, 16986, 16987, 16988, 16989,
16990, 16991, 16992, 16993, 16994, 16995, 16996, 16997, 16998,
16999, 17000, 17001, 17002, 17003, 17004, 17005, 17006, 17007,
17008, 17009, 17010, 17011, 17012, 17013, 17014, 17015, 17016
), class = "Date"), Tc = c("0.0964", "0.0780", "0.1265", "0.1503",
"0.1548", "0.1028", "0.1112", "0.1283", "0.0956", "0.0847", "0.0785",
"0.0859", "0.0879", "0.1203", "0.1677", "0.2174", "", "", "0.1496",
"0.1080", "0.1101", "0.1289", "0.0942", "0.0835", "0.0851", "0.0881",
"0.1216", "0.0766", "0.0744", "0.0626", "", "0.1116", "", "0.0862",
"", "0.1210", "", "", "0.1074", "", "0.1527", "", "0.1513", "",
"0.1246", "", "0.1415", "", "0.0827")), .Names = c("Date", "Tc"
), class = "data.frame", row.names = 3:51)
# melt data frame
df <- melt(df, id.vars = c("Date"))
#basic plot
plot1 <- ggplot(df[!is.na(df$value), ],
aes(x=Date, y=value, color=variable, group = variable,shape
= variable, linetype = variable, fill = variable))
# points
plot1 <- plot1 + geom_line(lwd =3)+geom_point(size=17, stroke =2)
break.vec <- c(as.Date("2016-06-16"),
seq(from=as.Date("2016-06-16"), to=as.Date("2016-08-04"),
by="week"))
plot1 <- plot1 + scale_x_date(breaks = break.vec, date_labels = "%d-%m", limits=range(break.vec))
Ok thank you for the input! This is what I went with from the comments, although I am sure there might be a more concise way to do so - but it does the job!
break.vec <- seq(from=as.Date("2016-06-16"), to=as.Date("2016-08-04"),
by="day")
plot1 <- plot1 + scale_x_date(breaks = break.vec,
labels=c("16-06","","","","","","","23-06","","","","","","", "30-06",
"","","","","","", "07-07", "","","","","","", "14-07",
"","","","","","","21-07", "","","","","","", "28-07",
"","","","","","", "04-08"),expand = c(0.05,0))

Resources