How to specify x/y axis label values outside chart - r

I want to add labels to the x and y axis where geom_segment lines end at. Something like this:
The code I'm working with:
ggplot(exp, aes(voltage)) +
geom_line(aes(y = current , colour = "current")) +
geom_segment(x = 0,
xend = 4.44993305,
y = 0.039496649, yend = max(
0.039496649)) +
geom_segment(x = 4.44993305,
xend = 4.44993305,
y = 0, yend = max(
0.039496649))+
geom_text(x = 4.44993305 + 0.1,
y = 0.039496649+0.0020, label = "MPP", check_overlap = TRUE) +
scale_colour_manual(values = c("red", "forestgreen")) +
annotate(geom = "point", x = 4.44993305, y = 0.039496649, colour = "orange", size = 3) +
labs(colour = "", x = "Voltage(V)", y = "Current(mA)", title = "P-V Curve") +
scale_y_continuous(expand = c(-0.05, 0), limits = c(0, 0.05)) +
scale_x_continuous(expand = c(0, 0), limits = c(0, 6))
reproducible code:
structure(list(current = c(0.04465198144317, 0.04463520273566,
0.04460114613175, 0.04457135125995, 0.0445214137435, 0.04453720897436,
0.04449214786291, 0.04451920464635, 0.04460586234927, 0.04455019906163,
0.04449771717191, 0.04447644948959, 0.04445287585258, 0.04446478188038,
0.04446309804916, 0.04443653672934, 0.04444691166282, 0.0444468036294,
0.04446209222078, 0.04440823569894, 0.04440270736814, 0.04439539834857,
0.04439954087138, 0.04436922073364, 0.04438590630889, 0.0443644374609,
0.04435379058123, 0.04435301944613, 0.04435969889164, 0.04429738968611,
0.04429103061557, 0.04427329823375, 0.04427855834365, 0.04435616359115,
0.04431574419141, 0.04425121843815, 0.04425748437643, 0.04426665976644,
0.04420702531934, 0.04421706870198, 0.04422586038709, 0.04420934617519,
0.04419368878007, 0.04420448094606, 0.04418215155602, 0.0441633015871,
0.04416632652283, 0.04419567808509, 0.0441645719111, 0.04414548352361,
0.0441205650568, 0.04409914091229, 0.04407843202353, 0.04408247023821,
0.0440976023674, 0.04407130554318, 0.04406511038542, 0.04406157508492,
0.04411358013749, 0.04408717527986, 0.04403834789991, 0.04397377744317,
0.04399513080716, 0.04394119232893, 0.04393403977156, 0.04395672306418,
0.04391529783607, 0.04388456046581, 0.04384018108249, 0.04377613589168,
0.04371620714664, 0.04360201209784, 0.04351374134421, 0.04337716847658,
0.04323555156589, 0.0430811829865, 0.04289939254522, 0.04273791983724,
0.04255214333534, 0.04230800643563, 0.04205309599638, 0.04180316627026,
0.04153088480234, 0.04128103330731, 0.04098862782121, 0.0406355522573,
0.04026722535491, 0.03988171741366, 0.03949664905667, 0.03904519975185,
0.03858072310686, 0.03809594735503, 0.03754813969135, 0.03698132932186,
0.03636532649398, 0.03573113679886, 0.03502003848553, 0.03427068144083,
0.03349439799786, 0.03265217319131, 0.03173480927944, 0.03077974915504,
0.0297338180244, 0.02861846797168, 0.02742668054998, 0.02613251283765,
0.02471670508385, 0.02320869639516, 0.02159749343991, 0.01984822377563,
0.01790586858988, 0.01580262370408, 0.01354998257011, 0.0110809514299,
0.008333564735949, 0.005422144662589, 0.002235329709947), voltage = c(0.0497902818024,
0.1001076325774, 0.1500261873007, 0.200014218688, 0.2499825656414,
0.2999092638493, 0.3499111533165, 0.3998158872128, 0.4498016238213,
0.5000447034836, 0.5500398874283, 0.6000450849533, 0.6499763131142,
0.6999563574791, 0.749892115593, 0.7998710870743, 0.8501480221748,
0.9000863432884, 0.9500870704651, 1.000000834465, 1.0499948263168,
1.0999064445496, 1.1499096155167, 1.1998677253723, 1.2501357793808,
1.3001333475113, 1.3500553369522, 1.4000434875488, 1.4499852657318,
1.4999231100082, 1.549925327301, 1.6001867055893, 1.6501588821411,
1.7000889778137, 1.7500828504562, 1.800025343895, 1.8499475717545,
1.899978518486, 1.949893116951, 2.0002207756042, 2.0501630306244,
2.1001031398773, 2.1501016616821, 2.2000172138214, 2.2499938011169,
2.299911737442, 2.3502452373505, 2.4001979827881, 2.4501132965088,
2.5001027584076, 2.5500218868256, 2.5999536514282, 2.6498956680298,
2.6998517513275, 2.7501895427704, 2.8001124858856, 2.8500940799713,
2.9000113010406, 2.9500093460083, 2.9999513626099, 3.0498661994934,
3.1002125740051, 3.1501111984253, 3.2001020908356, 3.2500350475311,
3.3000183105469, 3.3500154018402, 3.399943113327, 3.4499311447144,
3.5001940727234, 3.550187587738, 3.6001205444336, 3.6500551700592,
3.7000305652618, 3.7499585151672, 3.7999482154846, 3.8501899242401,
3.9001405239105, 3.9500863552094, 4.0000004768372, 4.0499835014343,
4.0999245643616, 4.1499009132385, 4.1998748779297, 4.2501282691956,
4.300087928772, 4.3500247001648, 4.3999924659729, 4.449933052063,
4.4998655319214, 4.5498738288879, 4.6001214981079, 4.6501264572144,
4.7000713348389, 4.7500491142273, 4.8000221252441, 4.8499178886414,
4.8999338150024, 4.9498329162598, 5.0001378059387, 5.0500822067261,
5.1000084877014, 5.1500005722046, 5.1999025344849, 5.2499060630798,
5.2998147010803, 5.3501005172729, 5.4000744819641, 5.4499711990356,
5.4999628067017, 5.5498695373535, 5.5998673439026, 5.6497716903687,
5.6997165679932, 5.7502884864807, 5.8000655174255, 5.8499555587769
), PP = c(0.00222323473909097, 0.00446832447547921, 0.00669133990338782,
0.0089149039981273, 0.0111295772335824, 0.0133571215574027, 0.0155682987722391,
0.0177994853036886, 0.020063789316651, 0.0222770910799081, 0.0244755193440537,
0.0266878749124022, 0.0288933163539832, 0.0311234067610935, 0.0333425266619036,
0.0355435009395142, 0.0377864540419245, 0.0400059609496442, 0.04224285894479,
0.0444082727560584, 0.0466226130110058, 0.0488307847519388, 0.0510554589725266,
0.0532371959582141, 0.0554884095769874, 0.0576796845864956, 0.0598800716882498,
0.0620961560286796, 0.0643209097851773, 0.0664426785032353, 0.068647790123336,
0.070845543246237, 0.073066656339177, 0.0754094248194155, 0.0775562239145906,
0.0796533146869037, 0.0818740257541394, 0.0841057026413645, 0.0861989743910597,
0.0884438994540186, 0.090670223963168, 0.092844186714439, 0.0950209236818901,
0.0972506190093721, 0.0994095671210524, 0.101571695684362, 0.103801698561548,
0.106078377387785, 0.108208204874005, 0.110368245128615, 0.112508406553953,
0.114655722439755, 0.116803246072698, 0.119016134475474, 0.121276764892071,
0.123404612920738, 0.125589710242767, 0.127779065687917, 0.130135473691482,
0.132259381554438, 0.134311068741467, 0.136328057755817, 0.138589554231821,
0.140616301445618, 0.142787169037195, 0.145057990983433, 0.147116924127234,
0.149205009137113, 0.151245606106401, 0.153224971374792, 0.155200735994985,
0.156972499532076, 0.158827556562052, 0.160496849197857, 0.16213152475246,
0.163706264410516, 0.165170808933627, 0.166683893064858, 0.168084640773841,
0.169232045916551, 0.170314344969572, 0.171389828259531, 0.172349056768834,
0.17337517472235, 0.174206925818462, 0.1747364477406, 0.175163424900961,
0.175479256150164, 0.175757444083009, 0.175698148550336, 0.175537422363473,
0.17524598641866, 0.174603597797929, 0.173814885869911, 0.172737086901316,
0.171510247194652, 0.169844311111882, 0.167924070855098, 0.165791673720114,
0.163265365619927, 0.160263395675946, 0.156976981940024, 0.153129179839487,
0.148813244139014, 0.143987496509493, 0.138497475713147, 0.132236856654388,
0.125328689163156, 0.11770571721887, 0.109164492545057, 0.0993752346268299,
0.0884925964284587, 0.0765543079295968, 0.063158282454129, 0.0479204013524691,
0.0314487942879752, 0.0130765794624036)), row.names = c(NA, -117L
), class = c("tbl_df", "tbl", "data.frame"))

I made a package specifically for this. lemon
library(lemon)
ggplot(exp, aes(voltage)) +
... +
annotate_x_axis('IM', x = 4.44993305, side = 'bottom', print_value = FALSE) +
annotate_y_axis('M', y = 0.039496649, side='left', print_value = FALSE)
(where the ... is placeholder for the rest of your ggplot-commands)

Related

Is there a way to use slip data based on a name to then use alongside sec.axi

I'm using the below code in order to plot two veriables on the same graph to compare them.
Some of them are in te AW group and the others are in the EWC group.
Data1 %>%
pivot_longer(
cols = -1,
names_to = c("try", "exp"),
names_pattern = "(.)(.)")%>%
ggplot(aes(x = exp, y = value, group = exp)) +
geom_point(aes (shape = exp, colour = exp))+
geom_smooth(alpha = 0, aes(colour = exp))+
stat_summary(fun.y = mean,
fun.ymin = function(x) mean(x) - sd(x),
fun.ymax = function(x) mean(x) + sd(x),
aes(color=exp),
geom = "errorbar") +
scale_y_continuous(expand = c(0, 0), breaks = seq ( 0,700, by = 200), name = "AW (%)",
sec.axis = sec_axis(~. + 10, name = "EWC (%)"))
Data that I'm working with:
structure(list(Sample = c("AW DL", "AW", "AW ambient temp", "AW DL ambient temp",
"EWC DL", "EWC", "EWC DL ambient temp", "EWC ambient temp"),
A1 = c(418.181818181874, 288.888888888889, 319.999999999996,
173.333333333325, 80.701754385967, 74.2857142857143, 63.4146341463404,
76.190476190476), B1 = c(483.333333333305, 517.647058823533,
565.384615384621, 375.000000000032, 82.857142857142, 83.8095238095239,
78.947368421054, 84.9710982658961), C1 = c(606.06060606057,
542.10526315789, 496.551724137933, 587.500000000377, 85.8369098712439,
84.4262295081966, 85.4545454545534, 83.2369942196532), A2 = c(368.750000000047,
46.428571428571, 216.39344262295, 104.651162790703, 78.6666666666688,
31.7073170731706, 51.136363636365, 68.3937823834196), B2 = c(417.857142857153,
123.913043478263, 213.63636363636, 180.769230769273, 80.6896551724142,
55.3398058252432, 64.3835616438409, 68.1159420289851), C2 = c(283.928571428547,
169.230769230771, 271.428571428566, 95.2380952380727, 73.9534883720913,
62.8571428571431, 48.7804878048721, 73.0769230769227), A3 = c(564.10256410254,
194.285714285712, 314.999999999998, 362.162162162103, 84.9420849420844,
66.0194174757279, 78.3625730994124, 75.9036144578312), B3 = c(656.249999999929,
26.4705882352953, 263.492063492065, 443.243243243154, 86.776859504131,
20.9302325581403, 81.592039800992, 72.4890829694324), C3 = c(634.883720930251,
330.555555555559, 304.444444444446, 416.666666666644, 86.3924050632915,
76.7741935483873, 80.6451612903217, 75.2747252747254), A4 = c(260.00000000002,
96.3636363636384, 285.714285714287, 174.509803921584, 72.2222222222237,
49.0740740740746, 63.5714285714306, 74.0740740740742), B4 = c(196.721311475417,
41.4285714285729, 245.762711864405, 190.566037735832, 66.2983425414373,
29.29292929293, 65.5844155844135, 71.0784313725489), C4 = c(262.264150943415,
58.6206896551738, 194.444444444444, 214.634146341482, 72.3958333333348,
36.956521739131, 68.2170542635678, 66.0377358490565)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -8L))
I found some examples of an if statements, I am just stuck on getting them to work.
Thank you.

loess() doesn't smooth subsequently but over pooled data

I have time-series of 2d obsverations that I'm trying to smooth to take out some of the observation variability. I've been applying loess(), but just noticed it doesn't seem to smooth as a function of time but just across the entire pooled coordinates. Am I missing something? Is there a different function I should be using?
df<-structure(list(timestamp = structure(c(1586488380, 1586488440,
1586488560, 1586488620, 1586488680, 1586488740, 1586488800, 1586488860,
1586489520, 1586489580, 1586489700, 1586489820, 1586489880, 1586489940,
1586490000, 1586490060, 1586490120, 1586490180, 1586490240, 1586490300,
1586490360, 1586490420, 1586490480, 1586490540, 1586490600, 1586490660,
1586490720, 1586490780, 1586490840, 1586490900, 1586490960, 1586491020,
1586491200, 1586491260, 1586491320, 1586491380, 1586491440, 1586491500,
1586491560, 1586491620, 1586491680, 1586491740, 1586491800, 1586491860,
1586491920, 1586491980, 1586492040, 1586492100, 1586492160, 1586492220,
1586492280, 1586492340, 1586492400, 1586492460, 1586492520, 1586492580,
1586492640, 1586492700, 1586492760, 1586492820, 1586492880, 1586492940,
1586493000, 1586493060, 1586493120, 1586493180, 1586493240, 1586493300,
1586493360, 1586493420, 1586493480, 1586493540, 1586493600, 1586493660,
1586493720, 1586493780, 1586493840, 1586493900, 1586493960, 1586494020,
1586494200, 1586494260, 1586494320, 1586494380, 1586494440, 1586494500,
1586494560, 1586494620, 1586494680, 1586494740, 1586494800, 1586494860,
1586494920, 1586494980, 1586495040, 1586495100, 1586495160, 1586495220,
1586495280, 1586495340, 1586495400, 1586495460, 1586495520, 1586495580,
1586495640, 1586495700, 1586495760, 1586495820, 1586495880, 1586495940,
1586496000, 1586496060, 1586496120, 1586496180, 1586496240, 1586496300,
1586496360, 1586496420, 1586496480, 1586496540, 1586496600, 1586496660,
1586496720, 1586496780, 1586496840, 1586496900, 1586496960, 1586497020,
1586497080, 1586497140, 1586497200, 1586497260, 1586497320, 1586497380,
1586497440, 1586497500, 1586497560, 1586497620, 1586497680, 1586497740,
1586497800, 1586497860, 1586497920, 1586497980, 1586498040, 1586498100,
1586498160, 1586498220, 1586498280, 1586498340), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), easting = c(740.582355718548, 740.582355718548,
739.726374785548, 739.611045841548, 739.508690311548, 739.398269506548,
739.278804356548, 739.627760514548, 737.913640733548, 738.088450601548,
738.551491861548, 738.957133488548, 739.137345557548, 739.304664573548,
739.460440784548, 739.605842807548, 739.741887116548, 739.719077482548,
739.369420509548, 738.973489249548, 738.521335985548, 739.279305656548,
739.993757669548, 740.085239162548, 740.172262825548, 740.255157063548,
740.334219013548, 740.409718260548, 740.481900024548, 740.550987811548,
740.573883125548, 740.252267406548, 739.261723439548, 738.935233921548,
738.774921432548, 738.615895069548, 738.451107559548, 738.280235586548,
738.493740162548, 738.717501067548, 738.925752666548, 739.120074872548,
739.301840012548, 739.472245999548, 739.632343117548, 739.890965132548,
740.098495936548, 740.293354354548, 740.476683179548, 740.649491986548,
740.678160672548, 740.443560695548, 740.398855065548, 740.451032168548,
740.467918582548, 740.160041067548, 739.819912921548, 739.641686751548,
739.457020461548, 739.265544595548, 739.193281488548, 739.227252654548,
738.995761471548, 738.822890745548, 738.775446949548, 738.726816309548,
738.676941805548, 738.625762928548, 739.254178659548, 739.727445331548,
740.019566884548, 740.129316037548, 740.255273807548, 740.442527947548,
740.615257127548, 740.775140223548, 740.923607252548, 741.055065964548,
741.139279130548, 740.907234314548, 739.290829342548, 739.115359714548,
740.664354207548, 740.589899151548, 740.578913554548, 740.571708783548,
740.568311076548, 740.568740893548, 740.527920123548, 740.358565457548,
740.392277156548, 740.370981239548, 740.289917518548, 740.214089159548,
740.192635592548, 740.176850935548, 740.160315351548, 740.143068630548,
740.103174446548, 740.008327647548, 740.058271768548, 740.205384482548,
740.211048172548, 740.150159818548, 740.122028309548, 740.230164637548,
740.271076846548, 740.075087486548, 739.768752873548, 739.586722485548,
739.940259334548, 740.233576255548, 740.473614136548, 740.495703912548,
740.341935547548, 740.186820856548, 740.204435025548, 740.299218490548,
740.318343269548, 740.238895133548, 739.999671854548, 740.062183564548,
740.196345466548, 740.329697802548, 740.418193609548, 740.311257937548,
740.270203214548, 740.209679752548, 740.146590442548, 740.079785501548,
740.170176300548, 740.268945921548, 740.217498771548, 740.133923060548,
740.117921377548, 740.177771453548, 740.140658663548, 740.080204534548,
740.108449333548, 740.145621912548, 740.182429420548, 740.010376475548,
739.819150336548, 739.616854492548, 739.369690457548, 739.104183601548,
738.938020260548, 738.843359187548, 738.802644324548, 738.761524527548
), northing = c(2307.15134120986, 2307.15134120986, 2307.60836846986,
2307.72110371186, 2307.83015111886, 2307.94605545486, 2308.06963872386,
2307.94323643186, 2308.11539257586, 2307.98516105286, 2307.67209087786,
2307.39795736686, 2307.27544716286, 2307.16124100486, 2307.05447137086,
2306.95438746086, 2306.86033624586, 2306.85049579286, 2307.02449397686,
2307.22230535086, 2307.44905018086, 2306.99878407786, 2306.57790074586,
2306.51052483586, 2306.44607531386, 2306.38433869586, 2306.32512182186,
2306.26824947086, 2306.21356227886, 2306.16091500786, 2306.12077142386,
2306.17964098286, 2306.35042514386, 2306.42512419786, 2306.46180283886,
2306.49818722086, 2306.53588972286, 2306.57498431686, 2306.47587680786,
2306.37665531786, 2306.28431106486, 2306.19814347186, 2306.11754401386,
2306.04198150286, 2305.97099021786, 2305.88981694586, 2305.83737042086,
2305.78847203986, 2305.74280289286, 2305.70008126986, 2305.72436278986,
2305.79211047386, 2305.75001607586, 2305.66373079386, 2305.59454921786,
2305.66723079486, 2305.74826733386, 2305.75631243686, 2305.76412484986,
2305.77169383886, 2305.76318962486, 2305.74988901286, 2305.84052390686,
2305.91140100786, 2305.95786180686, 2306.00518488486, 2306.05340722986,
2306.10256811686, 2305.87083530186, 2305.67745118186, 2305.55808577486,
2305.51324056486, 2305.46177226186, 2305.38525730986, 2305.31467748186,
2305.24934676386, 2305.18868082786, 2305.13496475486, 2305.08628958686,
2305.07937405386, 2305.33244795286, 2305.26640414086, 2304.97847050686,
2304.88865807586, 2304.78121096686, 2304.67333694586, 2304.56517670086,
2304.45687199986, 2304.26299422086, 2303.81398617786, 2303.81772073686,
2303.82576399386, 2303.82937144386, 2303.85166918186, 2303.85679798586,
2303.85841996086, 2303.85934299186, 2303.85962745886, 2303.81418344986,
2303.71268455886, 2303.70396413486, 2303.71187848686, 2303.71241867586,
2303.72409686386, 2303.75626565486, 2303.81432053886, 2303.80604508386,
2303.57280991386, 2303.21896587386, 2303.06912105986, 2303.28351126486,
2303.45378473786, 2303.49375232686, 2303.50460433986, 2303.48302188886,
2303.48174695086, 2303.52692291386, 2303.61686633486, 2303.41139580686,
2303.32679365886, 2303.06984393086, 2303.15017783486, 2303.29293566086,
2303.44528532286, 2303.48203523086, 2302.89274879786, 2302.81440275486,
2302.73512963586, 2302.65410710886, 2302.56757728186, 2302.77320543086,
2303.00846800486, 2303.01932301786, 2302.97477011386, 2303.03742546386,
2303.13970331386, 2303.07628123386, 2302.97297111586, 2303.02123867886,
2303.08476293486, 2303.14766331986, 2303.17026860886, 2303.18249014186,
2303.19025912386, 2303.15218828686, 2303.11064943486, 2302.98849464786,
2302.87632040886, 2302.82807292386, 2302.77934558786)), row.names = 5905:6054, class = "data.frame")
df.fitted<-loess(northing ~ easting, span = .5, data = df)
df$northing.fitted<-df.fitted$fitted
ggplot(df, aes(x=easting,y=northing)) +
geom_path(color='orangered2') +
geom_point(aes(y=northing.fitted))
So, instead of smoothing the "cluster", I'd like to use a rolling average smoothing each x/y pair as a function of time.
You need to regress both easting and northing as functions of time to get smoother x, y values:
df$numtime <- as.numeric(df$timestamp)
df.fitted.northing <-loess(northing ~ numtime, span = .5, data = df)
df.fitted.easting <- loess(easting ~ numtime, span = .5, data = df)
newdat <- data.frame(numtime = seq(min(df$numtime), max(df$numtime), len = 1000))
newdat$northing <- predict(df.fitted.northing, newdat)
newdat$easting <- predict(df.fitted.easting, newdat)
ggplot(df, aes(easting, northing)) +
geom_path(aes(color = "original path"), alpha = 0.6, size = 0.5,
arrow = arrow(length = unit(0.1, "inches"))) +
geom_point(aes(color = "original path"), alpha = 0.6, size = 1) +
geom_path(data = newdat, size = 1, aes(color = "smoothed"),
arrow = arrow(length = unit(0.1, "inches"))) +
coord_equal() +
theme_light() +
scale_color_manual(values = c("original path" = "orangered2",
"smoothed" = "deepskyblue4"), name = "")

Remove specific markers from legend

Sorry if this question has already been answered but I could not find the solution to what I am after. I have a plot that uses both geom_line and geom_point. The result of this is that in the legend, it adds both a line and a point when they should have one or the other. I want to keep the circles for the data tg1 and tg2 and remove the line and then do the opposite to the data full i.e. keep the line but remove the circle. I have seen that something like this works where you want to remove dots from all of the legend entries but nothing to only do specifics Removing ggplot2's geom_point icons from the legend. Can anyone help? Thanks.
#code for plot
library(ggplot2)
library(tidypaleo)
ggplot(LGRSL, aes(x =mmsl , y = Age))+
coord_flip()+
theme_classic(12)+
geom_point(data=tg1,aes(x=mmslc,y=Year,col="Fort Denison 1"),pch=1,size=2)+
geom_point(data=tg2,aes(x=mmslc,y=Year,col="Fort Denison 2"),pch=1,size=2)+
geom_lineh(data = full, aes(x=Lutregalammslc,y=Year,col="Full budget"))+
scale_colour_manual(values=c("grey15","grey50","black"))
## data
## tg1
structure(list(Year = 1886:1891, SLR = c(6919L, 6935L, 6923L,
6955L, 6956L, 6957L), mmsl = c(-0.158, -0.142, -0.154, -0.122,
-0.121, -0.12), m = c(6.919, 6.935, 6.923, 6.955, 6.956, 6.957
), GIA.correction = c(-0.02814, -0.02793, -0.02772, -0.02751,
-0.0273, -0.02709), SLRc = c(6.89086, 6.90707, 6.89528, 6.92749,
6.9287, 6.92991), mmslc = c(-0.19667, -0.18046, -0.19225, -0.16004,
-0.15883, -0.15762)), row.names = c(NA, 6L), class = "data.frame")
##tg2
structure(list(Year = 1915:1920, SLR = c(7011L, 6929L, 6987L,
6945L, 6959L, 6951L), mmsl = c(-0.066, -0.148, -0.09, -0.132,
-0.118, -0.126), m = c(7.011, 6.929, 6.987, 6.945, 6.959, 6.951
), GIA.correction = c(-0.02205, -0.02184, -0.02163, -0.02142,
-0.02121, -0.021), SLRc = c(6.98895, 6.90716, 6.96537, 6.92358,
6.93779, 6.93), mmslc = c(-0.09858, -0.18037, -0.12216, -0.16395,
-0.14974, -0.15753)), row.names = c(NA, 6L), class = "data.frame")
##full
structure(list(Year = 1900:1905, Lutregala = c(-0.103609677,
-0.118603251, -0.134550791, -0.105553735, -0.103983082, -0.121731984
), Wapengo = c(-0.095213147, -0.096005337, -0.115700625, -0.097696891,
-0.084444784, -0.109161066), Tarra = c(-0.106672829, -0.109537943,
-0.135256365, -0.101357772, -0.089716518, -0.104258351), Lutregalammsl = c(-0.292863465,
-0.307857039, -0.323804579, -0.294807523, -0.29323687, -0.310985772
), Wapengommsl = c(-0.257028279, -0.257820469, -0.277515756,
-0.259512023, -0.246259916, -0.270976198), Tarrammsl = c(-0.30925682,
-0.312121933, -0.337840355, -0.303941762, -0.292300508, -0.306842342
), LgGIAc = c(-0.01921, -0.01904, -0.01887, -0.0187, -0.01853,
-0.01836), WapGIAc = c(-0.02486, -0.02464, -0.02442, -0.0242,
-0.02398, -0.02376), TarGIAc = c(-0.02373, -0.02352, -0.02331,
-0.0231, -0.02289, -0.02268), Lutregalammslc = c(-0.312073465,
-0.326897039, -0.342674579, -0.313507523, -0.31176687, -0.329345772
), Wapmmslc = c(-0.281888279, -0.282460469, -0.301935756, -0.283712023,
-0.270239916, -0.294736198), Tarmmslc = c(-0.33298682, -0.335641933,
-0.361150355, -0.327041762, -0.315190508, -0.329522342)), row.names = c(NA,
6L), class = "data.frame")
##LGRSL
structure(list(depths = c(0.5, 1.5, 2.5, 3.5, 4.5, 5.5), RSL = c(0.047746907,
0.025564293, 0.021733558, 0.007855661, -0.004909879, 0.01747051
), RSLerror = c(0.058158556, 0.057902654, 0.057988654, 0.057957388,
0.057905405, 0.057226072), Age = c(2017.456716, 2013.594255,
2006.92838, 1999.675523, 1994.729181, 1990.518154), Ageerror = c(0.373138707,
0.77640096, 1.430582242, 1.627131115, 3.222393394, 3.239674718
), mmsl = c(0.01993169, -0.002250924, -0.006081659, -0.019959556,
-0.032725096, -0.010344707)), row.names = c(NA, 6L), class = "data.frame")
##LGRSLgp
structure(list(Age = 1892:1897, mean = c(-0.298147401, -0.304630597,
-0.31023294, -0.315506983, -0.321225142, -0.327190675), error = c(0.051858047,
0.04985084, 0.047760525, 0.045624121, 0.043505044, 0.041477551
), min = c(-0.246289354, -0.254779758, -0.262472416, -0.269882862,
-0.277720098, -0.285713124), max = c(-0.350005447, -0.354481437,
-0.357993465, -0.361131103, -0.364730186, -0.368668226), x = c(-0.02125,
-0.02108, -0.02091, -0.02074, -0.02057, -0.0204), meangia = c(-0.276897401,
-0.283550597, -0.28932294, -0.294766983, -0.300655142, -0.306790675
), rate = c(NA, -4.967327, -4.946326, -4.964493, -4.977451, -4.911859
), raterror = c(NA, 3.581013, 3.796417, 4.022157, 4.226762, 4.255126
), mmsl = c(-0.325962618, -0.332445814, -0.338048157, -0.3433222,
-0.349040359, -0.355005892)), row.names = c(NA, 6L), class = "data.frame")
Here is a way.
Override the guide legend with a list of vectors of values for each of the aesthetics involved, shape and linetype. Note the different ways to specify what is to be removed.
I have also simplified the code a bit.
library(ggplot2)
library(dplyr)
colrs <- c("Fort Denison 1" = "grey15",
"Fort Denison 2" = "grey50",
"Full budget" = "black")
legnd <- list(shape = c(1, 1, NA),
linetype = c("blank", "blank", "solid"))
bind_rows(
tg1 %>% mutate(col = "Fort Denison 1"),
tg2 %>% mutate(col = "Fort Denison 2")
) %>%
ggplot(aes(x = mmslc, y = Year, colour = col)) +
geom_point(pch = 1, size = 2) +
geom_lineh(data = full, aes(x = Lutregalammslc, col = "Full budget"))+
scale_colour_manual(values = colrs,
guide = guide_legend(override.aes = legnd)) +
coord_flip() +
theme_classic(base_size = 12)

How can I modify time on x-axis in ggplot?

I am creating a plot with r and I would like to change the time on the x-axis. There should be labels for every hour. I used: scale_x_datetime(breaks="1 hour", labels=date_format("%H:%M")), but unfortunately, it is not working. Does someone has an idea? Thanks for helping.
This is my code:
input2 <- "C:\\Users\\time_distance.csv"
time_distance <- read.csv(input2, sep=";")
library(scales)
time <- strptime(time_distance$time, format = "%H:%M:%S")
plot2 <-ggplot(time_distance, aes(x = time, y = distance, group = 1)) +
geom_point(stat = "identity") +
geom_smooth(method = lm, color = "red", se = FALSE) +
theme(legend.position = "none") +
theme_bw() +
labs(y = "Distance [m]", x = "time [hour]")+
scale_y_continuous(limits = c(0,1600), breaks = seq(100, 1500, 100))
print(plot2)
This is my data:
dput(time_distance)
structure(list(time = c("12:51:57", "12:55:16", "12:56:29", "13:25:05",
"13:36:54", "13:55:37", "14:11:20", "14:13:17", "15:14:26", "15:18:48",
"15:21:01", "15:22:29", "15:25:13", "15:28:16", "15:28:26", "15:39:58",
"15:46:49", "15:50:45", "15:59:51", "16:02:38", "16:24:05", "16:35:17",
"11:15:24", "13:32:40", "14:42:39", "15:24:08", "15:32:28", "16:43:48",
"16:48:42", "17:10:28", "17:27:55", "11:23:25", "12:19:21", "12:27:19",
"12:28:39", "12:47:18", "13:17:01", "14:06:26", "14:15:35", "14:18:06",
"14:26:26", "14:50:32", "15:25:26", "15:33:50", "15:56:02", "15:58:45",
"16:11:02", "16:35:42", "17:16:24", "17:28:30", "08:58:43", "09:55:52",
"10:14:17", "10:24:17", "10:36:42", "10:55:35", "14:18:29", "14:36:59",
"15:22:46", "15:51:35", "15:58:29", "16:36:36", "16:47:48", "13:33:52",
"14:31:38", "14:49:03", "16:13:31", "11:11:20", "11:46:00", "11:50:43",
"12:13:11", "13:07:31", "13:27:46", "14:37:37", "15:27:51", "10:07:52",
"10:18:18", "11:56:53", "12:18:37", "12:45:55", "13:14:20", "13:23:21",
"13:30:15", "13:31:34", "13:33:12", "13:48:06", "13:53:52", "14:30:18",
"14:44:24", "14:51:14", "15:03:19", "15:33:32", "15:49:00", "15:55:53",
"16:07:24", "16:11:43", "16:21:43", "16:35:50", "16:39:37", "16:48:41",
"10:02:45", "12:14:40", "13:10:27", "14:36:28", "14:51:12", "15:38:50",
"15:58:29", "10:53:33", "11:11:27", "11:32:26", "11:38:36", "12:56:03",
"13:45:09", "14:39:48", "14:51:57", "14:53:25", "15:08:02", "16:01:32",
"16:22:14", "16:46:01", "08:57:18", "09:07:51", "09:25:10", "09:34:32",
"10:15:35", "10:33:54", "11:07:55", "11:26:00", "11:40:21", "11:50:45",
"11:57:16", "12:55:00", "13:21:18", "14:47:07", "14:50:21", "14:56:56",
"15:06:39", "15:21:53", "15:36:26", "15:48:37", "15:54:50", "16:01:28",
"16:12:21", "16:21:53", "16:26:20", "16:30:52", "09:49:00", "10:13:53",
"10:27:21", "10:46:43", "12:24:04", "12:30:04", "12:54:33", "13:33:14",
"13:52:55", "14:12:14", "14:37:37", "14:42:58", "14:44:37", "14:51:39",
"15:08:57", "15:38:08", "15:49:06", "16:05:53", "17:01:34", "08:31:56",
"09:44:00", "10:19:35", "10:47:49", "11:18:16", "11:35:49", "12:32:43",
"12:43:45", "13:11:05", "13:24:34", "13:32:46", "13:42:01", "14:20:17",
"14:31:11", "14:36:30", "16:06:58", "08:30:07", "09:02:22", "10:03:07",
"10:29:09", "10:52:23", "11:47:59", "12:58:26", "13:47:26", "13:53:05",
"14:08:33", "14:16:46", "14:28:50", "15:16:42", "16:07:43", "08:27:08",
"08:52:03", "09:04:13", "09:14:04", "09:22:39", "09:32:25", "09:39:04",
"10:18:02", "13:06:58", "13:21:03", "13:37:28", "13:41:09", "14:17:06",
"14:36:17", "14:51:45"), distance = c(89.6472646, 162.833946,
204.1718123, 69.32061609, 145.5725233, 157.8104904, 142.7204165,
139.4100682, 156.290279, 281.1730457, 211.3723597, 146.9900352,
155.5278723, 121.630972, 115.0260845, 103.4678487, 535.2962882,
172.2392646, 187.1019506, 192.2072681, 163.1432699, 328.1146666,
161.378541, 276.9657775, 288.6843714, 232.236379, 286.5742551,
171.9799195, 131.3821584, 645.23548, 328.8999153, 83.1376454,
206.8425108, 160.1471859, 163.4999165, 71.84934976, 122.7265289,
156.6223912, 245.7737619, 76.4010552, 191.7314754, 241.2484589,
314.4240603, 168.1778327, 249.4432742, 171.5837494, 445.289732,
150.781544, 231.0174121, 233.6595053, 212.0908014, 274.9652469,
230.2231595, 463.3457859, 185.4275877, 413.7982665, 225.4934765,
171.8698762, 287.8326822, 457.6477022, 216.539991, 203.7116093,
261.3755307, 176.3162834, 113.2523456, 186.0197098, 110.2207489,
164.0611501, 111.7164405, 157.4453558, 158.5689564, 155.240531,
117.3045189, 117.6454036, 224.4921484, 326.2455013, 205.1963079,
166.1579876, 245.2281083, 168.9163027, 280.1466904, 331.9011914,
268.5518097, 296.9010562, 476.5082804, 391.8845907, 255.7662667,
483.9525726, 283.381313, 592.5282434, 158.4318925, 722.0857244,
1524.855308, 261.3729014, 495.6679407, 585.3051787, 485.0507777,
472.889667, 394.5888053, 328.1134478, 209.4992859, 161.8100828,
179.8945385, 438.95502, 538.0923178, 142.1052511, 150.0340155,
170.1366692, 123.6032668, 171.7141119, 98.76667809, 235.874407,
241.7648426, 224.3868202, 167.1152772, 67.44743255, 87.79127323,
150.6129032, 476.6532019, 186.2234702, 139.4749401, 102.0916653,
69.80207399, 77.6190789, 179.4630148, 109.0492763, 165.9927234,
228.9755043, 247.591346, 262.0171879, 155.9824185, 207.1022985,
121.5681699, 178.136665, 126.386831, 74.78485864, 71.12781299,
78.21953935, 317.1932926, 219.2749766, 247.6333865, 98.85128473,
235.0537481, 90.20871946, 124.2324844, 125.3904596, 248.5174138,
148.2374205, 299.5611988, 77.88228217, 194.0273272, 117.208969,
347.7014402, 306.3749268, 313.0498324, 313.7161044, 479.518518,
314.9932, 271.3977789, 274.4941856, 391.3815061, 418.8094285,
243.0563698, 599.4055807, 169.8079415, 70.72250265, 181.7665702,
172.3945082, 95.23804572, 147.9418742, 101.8629435, 127.5025047,
236.7513256, 113.5718566, 133.5204809, 215.6790291, 72.40720193,
80.22093147, 188.9783062, 75.71888165, 100.6934031, 293.32912,
269.7824729, 772.4577258, 468.3636793, 221.4304232, 282.6939389,
250.2391573, 111.5475549, 181.1174836, 221.0176637, 206.4821957,
127.4349516, 112.25765, 198.9440625, 144.8789547, 86.23383985,
48.90437015, 107.9818848, 186.2547336, 156.2620094, 112.5252141,
349.3143126, 342.4842646, 69.19741708, 206.0086208, 211.6554678,
119.8004909, 136.4785611, 111.8184516)), class = "data.frame",
row.names = c(NA, -210L))
The column time should be POSIXct to be able to use scale_x_datetime, convert it to the right type and it will work.
This should work:
library(scales)
time_distance$time <- as.POSIXct(time_distance$time, format = "%H:%M:%S")
ggplot(time_distance, aes(x=time, y=distance, group=1)) +
geom_point() +
geom_smooth(method=lm , color="red", se=FALSE) +theme(legend.position="none") +
theme_bw()+
labs(y = "Distance [m]", x = "time [hour]")+
scale_y_continuous(limits=c(0,1600), breaks=seq(100, 1500, 100)) +
scale_x_datetime(date_breaks="1 hour", labels=date_format("%H:%M"))

Incorporating an extra dimension in stat_density2d

I'm doing some single particle tracking experiments, and I've made a plot that looks like this
And an excerpt of the data like this
Tracks_subset <- structure(list(x = c(50.9870663039413, 51.1454673261645, 51.2438755862949,
51.2095459932038, 51.6660042216598, 50.9114140942979, 51.0024489561214,
50.8857311418507, 51.2794722111027, 51.2431331170315, 51.1686446878892,
51.6828347775235, 52.1450571292308, 52.0846161251043, 52.6398039669697,
52.8833174825244, 52.5705151101221, 47.7126603341761, 47.7043102797697,
47.7383984362003, 47.6860850023564, 47.6951829901912, 47.6699652212862,
47.7413324987855, 47.6843782373267, 47.644111020292, 47.6618622920263,
47.6541735611274, 47.6649621568611, 47.6523884564593, 47.6610279361442,
47.6512197115578, 47.6657086161767, 47.6192509459374, 47.6207586571131,
47.5774206679784, 47.6200920953012, 47.7306727739304, 47.6379509867329,
47.714775352656, 47.6550462636824, 47.6609930612728, 47.7000183950083,
47.6649651728503, 47.6539558351917, 47.6389944434634, 47.6717405874446,
47.6953802257671, 47.670156194978, 47.6659335190123, 47.6765976765644,
47.6840293686863, 47.6835120352331, 47.7206680944438, 47.6295515092937,
47.6296091556035, 47.6942899171821, 47.7250190805859, 47.5914020018512,
47.70039477981, 47.6173948003708, 47.6403690517817, 47.706224437305,
47.6278056300272, 47.7050590713087, 47.6202775363008, 47.616604053917,
47.6756516087319, 47.7266296774674, 47.6446057760854, 47.5705082443691,
47.6595393038973, 47.7454552888996, 47.6741550431176, 47.5860886147769,
47.5711827843158, 47.6032212783683, 47.6664108892973, 47.6658197925874,
48.3968244300988, 48.9904220754653, 48.8091985242839, 48.6060438195256,
47.9442329308765, 47.5918680853048, 61.7398341199382, 61.5105531711929,
61.3508575465536, 60.7780284493327, 60.9300399030291, 61.5425582885666,
61.6082571595723, 61.5298863337404, 61.7856612691648, 61.9071110539638,
62.2590343816915, 62.4185223478263, 62.9672715597194, 63.3275273995809,
63.8113387354958, 64.3231547762155, 63.7003651772996, 64.5725239608709
), y = c(25.4839360156051, 25.6047955465986, 25.4262289850266,
25.6172499551253, 25.2123769606517, 25.0385424557608, 24.966470254684,
25.0765695667183, 25.3864947576636, 25.5754234194841, 25.362590160186,
25.7772644867977, 25.5496302761478, 25.2288998273316, 25.0558763011072,
24.9078484752662, 24.976087151153, 25.3889343715977, 25.4338531136808,
25.4429033909954, 25.4318520315956, 25.3990616319309, 25.4511690945816,
25.3200107971065, 25.481502531704, 25.4813627020502, 25.4853491195569,
25.4847769218943, 25.4575428518825, 25.4557467653372, 25.4624512146039,
25.4595647154719, 25.4563293549661, 25.4804991266559, 25.3812415821813,
25.4473050286014, 25.4196869940826, 25.4442315703239, 25.479341197772,
25.4300738230213, 25.4693914326269, 25.4567345365435, 25.4674089428837,
25.4112535933299, 25.4637733103663, 25.3956145368333, 25.4990099732346,
25.4839175917237, 25.4745320245195, 25.4005315613532, 25.469591766703,
25.4539955232768, 25.4657553925353, 25.4245323404107, 25.4361711386943,
25.3891764540008, 25.466965662987, 25.1009694093232, 25.0358862503509,
25.1789725026332, 25.42714785297, 25.4795979566479, 25.468929968642,
25.4239887143419, 25.5281893742505, 25.4093001632827, 25.4384275123265,
25.4344555350342, 25.4429352491382, 25.5199514977758, 25.4910332653875,
25.4595502380103, 25.4968060944952, 25.4827240176748, 25.3698050293595,
25.4439838950347, 25.3741755234997, 25.5563892658156, 25.6960651973953,
25.7204681252841, 26.4352696253167, 27.1441226535699, 26.6615707288907,
26.3571435264103, 26.2462341038955, 25.4044026980314, 24.9429648374475,
25.316181029454, 25.5746195865509, 25.4159071371913, 25.0535276243763,
25.1404356519081, 24.9217292382466, 25.0681095303721, 25.6848864023155,
25.7992168557371, 25.2353282682059, 24.9773329424632, 24.4822347858453,
24.268105758617, 24.6415345738956, 24.0369860755695, 23.6865149091661
), steplength = c(0, 0.199243343862037, 0.203887720506984, 0.194081250972988,
0.610144455009775, 0.774354373270278, 0.116110930731278, 0.160452194371228,
0.501084477506786, 0.192391707241276, 0.225491734526058, 0.660565095527355,
0.515234739003464, 0.326375758563115, 0.581524445214716, 0.284975559445094,
0.320159087122816, 0, 0.0456882566850305, 0.035269107279719,
0.053468008239723, 0.0340291594490852, 0.0578888895422253, 0.14931773938219,
0.171240673444157, 0.0402674598162118, 0.0181933826629311, 0.00770999306101455,
0.0292931453981066, 0.0127013333423588, 0.0109357326776552, 0.0102241452834658,
0.0148457372554814, 0.0523688169390838, 0.0992689947975958, 0.079009874415496,
0.0508291898464581, 0.113271897260304, 0.0991464359452704, 0.091264765459093,
0.071508310668843, 0.0139843276580312, 0.0404588633537657, 0.066197822223741,
0.0536612168447871, 0.0697815280139674, 0.108457024734435, 0.0280466126241494,
0.0269135765181205, 0.0741208441744882, 0.06987872508325, 0.0172763670024788,
0.0117712428774199, 0.055496961741496, 0.0918569197986897, 0.0469947200495333,
0.101167000316718, 0.367284003435199, 0.148624834100549, 0.179869678487995,
0.261686837845756, 0.0572610653386249, 0.0667138499153149, 0.0903837688578274,
0.129714577882537, 0.146022440613998, 0.029358081257867, 0.0591809963859102,
0.0516785162644559, 0.112514101125709, 0.0795406083136827, 0.0944336305219004,
0.0936459039218517, 0.0726775751944748, 0.143200536740801, 0.0756616672734531,
0.0768093343124944, 0.192859469091138, 0.139677182309732, 0.731411842228994,
0.929139036438272, 0.731651960406345, 0.523572529910693, 0.728470709050467,
0.369407477451884, 0, 0.515261732169024, 0.405947063673079, 0.628429520644252,
0.219766065707624, 0.711686506777716, 0.108946532303133, 0.232324087684107,
0.294699860051154, 0.628620521453137, 0.37002902748092, 0.586009172701016,
0.606372233581375, 0.61229605162923, 0.529079057477617, 0.633565102904463,
0.867954936241549, 0.939942010041431)), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -103L), .internal.selfref = <pointer: 0x0>, .Names = c("x",
"y", "steplength"))
And the code for the plot is
ggplot(data = Tracks_subset, aes(x = x,
y = y)) +
# stat_bin2d(bins = 10, aes(fill = ..density..)) + # blocky heat map
stat_density2d(aes(fill = ..level..,
alpha = ..level..),
bins = 20,
geom = 'polygon') +
xlim(-20,max(Tracks_subset$x)+20) + # Adds padding for polygons to have proper shape
ylim(-20,max(Tracks_subset$y)+20) +
coord_cartesian(xlim = c(0,max(Tracks_subset$x)), # Clips extra padding visually only
ylim = c(0,max(Tracks_subset$y)),
expand = FALSE) +
scale_fill_continuous(low = "white",
high = "red") +
theme_classic(base_size = 12,
base_family = "Droid Sans") +
theme(axis.text = element_text(color = "black"),
strip.text.y = element_blank(),
strip.background = element_rect(color = "white"),
legend.text = element_text(size = 12, family = "Droid Sans"),
legend.title = element_blank(),
legend.position = "none") +
facet_wrap(condition + lipase ~ video,
scales = "free")
Now my question is, how can I somehow incorporate the steplength parameter as either an extra dimension (e.g. instead of ..level.. I could somehow use steplength) or weight in the stat_density2d? I know it's possible for the stat_bin2d, but this gives me a very blocky appearance that I don't really like.

Resources