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"))
Related
I have the follow lines of code:
ggplot() +
geom_line(data=TS_SimHeads_HOBS_final, aes(x=as.Date(Date), y=BH2672), color='red') +
geom_point(data=Hydro_dates_wellData_2014_2018, aes(x=as.Date(Date), y=BH2672), color='red') +
geom_line(data=TS_SimHeads_HOBS_final, aes(x=as.Date(Date), y=BH3025), color='green') +
geom_point(data=Hydro_dates_wellData_2014_2018, aes(x=as.Date(Date), y=BH3025), color='green') +
xlab("Date") + ylab("Head")
#theme_bw()
which generate the following plot:
What I am trying to do, unsuccessfully, is to include legends only for the lines (points are the experimental data and lines the simulated ones). Some data for reproduction purposes:
Date BH2672 BH278 BH2978 BH2987 BH3025 BH312 BH3963 BH3962 BH3957
2014-02-19 31.28400 78.86755 5.671027 39.48419 53.60201 44.29516 69.23685 61.70843 56.13871
2014-02-20 30.76656 78.87344 5.656940 39.49012 53.56489 44.50679 69.50910 61.70638 56.09621
2014-02-21 30.43226 78.88097 5.642136 39.49902 53.56041 44.65761 69.65709 61.70126 56.04346
2014-02-22 30.16532 78.88979 5.643818 39.51101 53.56065 44.78333 69.75621 61.69643 55.99459
2014-02-23 29.93577 78.89954 5.650873 39.52544 53.55970 44.89429 69.82983 61.69332 55.95241
2014-02-24 29.73162 78.90991 5.658991 39.54147 53.55682 44.99520 69.88845 61.69236 55.91639
As is quite often the case you first have to convert both of your datasets to long or tidy format using e.g. tidyr::pivot_longer which will result in a new column with the variable names as categories which could then be mapped on the color aes. Doing so will automatically create a legend and also allows to simplify your code. And if you want only the lines to appear in the legend then you could add show.legend=FALSE to geom_point. Finally you can set your desired colors via scale_color_manual.
As you provided only one dataset I used this for both datasets which however shouldn't matter. Also, to make my life a bit easier I have put the datasets in an named list:
library(dplyr, warn = FALSE)
library(tidyr)
library(ggplot2)
data_list <- list(data = Hydro_dates_wellData_2014_2018, sim = TS_SimHeads_HOBS_final) %>%
lapply(function(x) {
x %>%
select(Date, BH2672, BH3025) %>%
mutate(Date = as.Date(Date)) %>%
tidyr::pivot_longer(-Date)
})
ggplot() +
geom_line(data=data_list$sim, aes(x=Date, y=value, color = name)) +
geom_point(data=data_list$data, aes(x=Date, y=value, color = name), show.legend = FALSE) +
scale_color_manual(values = c(BH2672 = "red", BH3025 = "green")) +
labs(x = "Date", y = "Head")
DATA
TS_SimHeads_HOBS_final <- structure(list(Date = c(
"2014-02-19", "2014-02-20", "2014-02-21",
"2014-02-22", "2014-02-23", "2014-02-24"
), BH2672 = c(
31.284,
30.76656, 30.43226, 30.16532, 29.93577, 29.73162
), BH278 = c(
78.86755,
78.87344, 78.88097, 78.88979, 78.89954, 78.90991
), BH2978 = c(
5.671027,
5.65694, 5.642136, 5.643818, 5.650873, 5.658991
), BH2987 = c(
39.48419,
39.49012, 39.49902, 39.51101, 39.52544, 39.54147
), BH3025 = c(
53.60201,
53.56489, 53.56041, 53.56065, 53.5597, 53.55682
), BH312 = c(
44.29516,
44.50679, 44.65761, 44.78333, 44.89429, 44.9952
), BH3963 = c(
69.23685,
69.5091, 69.65709, 69.75621, 69.82983, 69.88845
), BH3962 = c(
61.70843,
61.70638, 61.70126, 61.69643, 61.69332, 61.69236
), BH3957 = c(
56.13871,
56.09621, 56.04346, 55.99459, 55.95241, 55.91639
)), class = "data.frame", row.names = c(
NA,
-6L
))
Hydro_dates_wellData_2014_2018 <- TS_SimHeads_HOBS_final
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 = "")
My data frame is as follows,
Date,Precipitation,Observed,Simulated
1/1/1988,21.90,3.06,15.27
1/2/1988,12.34,6.70,17.46
1/3/1988,19.76,18.32,32.45
1/4/1988,5.54,98.20,46.67
1/5/1988,6.50,91.92,37.43
1/6/1988,11.04,38.12,20.94
1/7/1988,17.90,44.14,26.64
1/8/1988,23.76,41.07,118.2
1/9/1988,59.74,169.06,225.4
1/10/1988,51.58,371.91,325.3
1/11/1988,18.08,447.05,387.1
1/12/1988,2.92,295.40,329.6
1/13/1988,2.72,133.90,218.7
1/14/1988,0.00,95.82,128.6
1/15/1988,7.38,70.32,70.17
1/16/1988,3.50,36.21,38.42
1/17/1988,4.58,9.43,21.97
1/18/1988,7.84,35.74,12.7
1/19/1988,16.86,24.32,12.96
1/20/1988,5.00,33.90,18.56
1/21/1988,0.96,27.06,20.9
1/22/1988,15.54,23.20,42.36
1/23/1988,11.36,16.41,54.19
1/24/1988,1.06,23.94,48.3
1/25/1988,6.42,17.35,42.32
1/26/1988,0.00,14.91,33.91
1/27/1988,2.44,10.13,25.46
1/28/1988,2.00,14.33,16.39
1/29/1988,2.36,10.62,6.423
1/30/1988,4.20,18.65,6.175
1/31/1988,15.80,12.67,38.42
2/1/1988,0.46,15.50,52.92
2/2/1988,2.50,14.91,35.03
2/3/1988,1.24,11.36,22.25
2/4/1988,0.04,16.72,15.55
2/5/1988,3.02,22.83,11.84
2/6/1988,0.00,10.86,10.33
2/7/1988,2.72,5.43,9.352
2/8/1988,0.36,3.32,9.41
2/9/1988,4.92,2.25,8.246
2/10/1988,1.94,21.38,8.059
2/11/1988,1.68,66.37,8.936
2/12/1988,0.00,66.37,8.247
2/13/1988,14.04,14.91,8.071
2/14/1988,2.08,9.90,9.941
2/15/1988,5.64,7.29,10.41
2/16/1988,0.00,21.38,10.08
2/17/1988,0.00,15.21,9.821
2/18/1988,1.44,13.49,10.43
2/19/1988,5.66,8.32,10.66
2/20/1988,0.32,4.77,8.877
2/21/1988,0.96,3.32,7.622
2/22/1988,1.36,2.46,6.069
2/23/1988,0.00,1.94,6.852
2/24/1988,0.36,1.48,7.538
2/25/1988,1.96,1.23,7.785
2/26/1988,4.08,1.08,9.163
2/27/1988,0.00,0.94,10.55
2/28/1988,0.56,0.81,10.5
2/29/1988,0.00,0.81,10.34
3/1/1988,0.00,0.69,10.77
3/2/1988,0.08,0.58,11.25
3/3/1988,0.82,0.53,10.86
3/4/1988,7.48,0.49,7.274
3/5/1988,6.26,0.40,5.618
3/6/1988,17.26,0.49,6.707
3/7/1988,3.42,0.35,8.519
3/8/1988,0.64,0.28,9.339
3/9/1988,3.64,0.32,7.926
3/10/1988,2.00,0.64,6.804
3/11/1988,19.60,0.75,4.982
3/12/1988,0.72,0.69,1.149
3/13/1988,12.52,0.64,1.99
3/14/1988,12.58,0.69,4.057
3/15/1988,22.34,0.81,13.46
3/16/1988,23.44,18.65,20.58
3/17/1988,35.20,65.08,41.81
3/18/1988,10.24,78.59,54.7
3/19/1988,47.32,146.45,88.96
3/20/1988,10.50,172.28,111.9
3/21/1988,0.12,215.85,88.96
3/22/1988,0.00,86.61,62.25
3/23/1988,5.88,49.53,42.3
3/24/1988,4.34,32.56,27.56
3/25/1988,0.50,121.94,18.82
3/26/1988,2.00,88.11,12.78
3/27/1988,14.30,35.28,7.07
3/28/1988,4.76,55.83,6.714
3/29/1988,2.60,17.67,6.184
3/30/1988,2.80,52.92,6.394
3/31/1988,2.80,63.80,7.282
4/1/1988,4.12,34.36,6.681
4/2/1988,23.88,14.62,18.06
4/3/1988,40.14,21.74,63.34
4/4/1988,20.74,31.24,128
4/5/1988,14.72,196.87,148.2
4/6/1988,3.08,121.94,115.5
4/7/1988,8.58,52.92,76.39
4/8/1988,4.32,99.81,48.48
4/9/1988,14.54,31.68,29.99
4/10/1988,3.62,53.49,20.66
4/11/1988,2.20,112.29,17.43
4/12/1988,2.06,58.83,14.83
4/13/1988,1.84,33.00,14.34
4/14/1988,0.00,50.08,13.21
4/15/1988,4.32,25.87,11.88
4/16/1988,6.84,21.03,11.32
4/17/1988,1.74,130.16,10.49
4/18/1988,19.20,55.83,10.87
4/19/1988,8.32,42.08,13.14
4/20/1988,4.30,21.38,14.37
4/21/1988,7.96,116.62,14.34
4/22/1988,11.52,60.67,14.35
4/23/1988,33.82,59.44,107.3
4/24/1988,5.66,198.03,152.3
4/25/1988,18.32,247.39,131.8
4/26/1988,17.80,130.16,121
4/27/1988,2.68,219.52,85.55
4/28/1988,0.00,148.43,52.51
4/29/1988,5.42,98.20,33.55
4/30/1988,1.12,54.65,24.19
5/1/1988,2.90,30.81,19.78
5/2/1988,0.00,16.72,17.36
5/3/1988,5.28,10.62,15.42
5/4/1988,0.00,7.69,10.36
5/5/1988,0.00,9.43,8.719
5/6/1988,0.00,5.60,8.94
5/7/1988,0.16,4.46,7.924
5/8/1988,0.00,3.72,6.791
5/9/1988,0.32,3.19,4.776
5/10/1988,3.24,3.32,6.717
5/11/1988,0.08,6.89,8.701
5/12/1988,0.00,4.61,9.747
5/13/1988,0.00,4.01,8.976
5/14/1988,0.00,3.19,8.588
5/15/1988,0.00,2.04,10.46
5/16/1988,0.00,1.75,12.28
5/17/1988,0.00,1.84,12.8
5/18/1988,3.26,1.56,12.48
5/19/1988,0.00,1.39,11.55
5/20/1988,2.84,1.39,11.39
5/21/1988,14.68,1.48,11.79
5/22/1988,8.00,1.39,12.92
5/23/1988,0.24,1.31,13.17
5/24/1988,0.00,1.08,12.73
5/25/1988,0.12,1.23,12.26
5/26/1988,3.12,1.01,12.14
5/27/1988,0.00,0.94,12.18
5/28/1988,0.00,1.23,12.56
5/29/1988,0.48,1.16,12.86
5/30/1988,0.00,0.94,11.86
5/31/1988,0.00,0.94,11.16
6/1/1988,2.72,0.94,11.3
6/2/1988,0.00,0.88,12.22
6/3/1988,0.92,0.81,13.16
6/4/1988,0.00,0.81,13.31
6/5/1988,0.00,0.69,13.42
6/6/1988,0.00,0.69,13.69
6/7/1988,0.00,0.64,13.95
6/8/1988,0.00,0.64,14.15
6/9/1988,0.00,0.58,14.18
6/10/1988,0.00,0.58,13.17
6/11/1988,0.00,0.58,12.07
6/12/1988,0.00,0.53,11.68
6/13/1988,0.00,0.49,10.58
6/14/1988,0.00,0.40,9.865
6/15/1988,0.00,0.40,9.251
6/16/1988,0.00,0.32,9.095
6/17/1988,0.00,0.32,9.796
6/18/1988,0.00,0.28,11.09
6/19/1988,0.00,0.25,12.24
6/20/1988,0.00,0.25,12.51
6/21/1988,0.00,0.25,12.83
6/22/1988,0.00,0.25,12.24
6/23/1988,0.00,0.25,11.73
6/24/1988,0.00,0.18,11.34
6/25/1988,0.00,0.18,11.88
6/26/1988,0.00,0.18,12.57
6/27/1988,0.00,0.16,13.2
6/28/1988,5.50,0.16,12.72
6/29/1988,0.00,0.13,11.74
6/30/1988,0.00,0.13,11.08
7/1/1988,0.00,0.32,11.06
7/2/1988,0.00,0.28,11.23
7/3/1988,0.00,0.28,11.97
7/4/1988,0.00,0.25,12.37
7/5/1988,0.00,0.25,13
7/6/1988,0.00,0.21,13.21
7/7/1988,0.00,0.18,13.29
7/8/1988,0.00,0.13,13.41
7/9/1988,0.00,0.13,13.49
7/10/1988,0.00,0.13,13.33
7/11/1988,0.00,0.13,13.13
7/12/1988,0.00,0.13,11.59
7/13/1988,0.00,0.11,10.34
7/14/1988,0.00,0.11,9.258
7/15/1988,0.00,0.09,9.584
7/16/1988,0.00,0.09,10.67
7/17/1988,0.00,0.09,11.51
7/18/1988,0.00,0.09,11.22
7/19/1988,0.00,0.13,10.27
7/20/1988,0.00,0.18,6.438
7/21/1988,0.00,0.28,2.296
7/22/1988,0.00,0.25,3.29
7/23/1988,0.00,0.32,5.341
7/24/1988,0.00,0.40,7.088
7/25/1988,0.00,0.35,7.95
7/26/1988,0.00,0.32,9.277
7/27/1988,0.00,0.32,10.6
7/28/1988,0.00,0.25,10.83
7/29/1988,0.00,0.25,10.31
7/30/1988,0.00,0.25,8.075
7/31/1988,0.00,0.18,7.727
8/1/1988,0.00,0.21,7.823
8/2/1988,0.00,0.21,8.826
8/3/1988,0.00,0.18,9.881
8/4/1988,0.00,0.18,9.957
8/5/1988,0.00,0.18,10.3
8/6/1988,0.00,0.18,9.91
8/7/1988,2.00,0.18,10.55
8/8/1988,2.64,0.18,10.41
8/9/1988,2.60,0.18,10.79
8/10/1988,0.00,0.18,9.982
8/11/1988,0.00,0.58,9.223
8/12/1988,0.00,0.64,7.946
8/13/1988,0.00,0.40,2.514
8/14/1988,0.00,0.32,4.496
8/15/1988,0.00,0.69,6.865
8/16/1988,0.00,0.81,9.129
8/17/1988,0.00,0.58,10.58
8/18/1988,0.00,0.32,11.36
8/19/1988,0.00,0.53,11.24
8/20/1988,0.00,0.32,11.25
8/21/1988,3.00,0.32,10.33
8/22/1988,0.00,0.25,9.991
8/23/1988,0.00,0.25,10.26
8/24/1988,0.00,0.21,9.69
8/25/1988,0.00,0.18,10.23
8/26/1988,1.20,0.18,9.806
8/27/1988,0.40,0.18,10.48
8/28/1988,18.32,0.16,11.04
8/29/1988,0.00,0.13,10.73
8/30/1988,0.00,0.13,10.2
8/31/1988,0.00,0.44,8.738
9/1/1988,0.00,0.58,7.971
9/2/1988,0.00,0.49,6.974
9/3/1988,0.00,0.32,7.245
9/4/1988,0.00,0.25,6.757
9/5/1988,0.00,0.18,5.911
9/6/1988,0.00,0.16,7.438
9/7/1988,0.00,0.13,7.892
9/8/1988,1.74,0.13,8.176
9/9/1988,0.00,0.09,7.239
9/10/1988,0.32,0.09,8.05
9/11/1988,0.00,0.09,8.973
9/12/1988,0.00,0.06,9.029
9/13/1988,0.00,0.04,7.839
9/14/1988,0.00,0.03,8.298
9/15/1988,0.00,0.03,9.274
9/16/1988,0.00,0.03,9.598
9/17/1988,0.20,0.01,9.834
9/18/1988,0.00,0.01,8.965
9/19/1988,0.00,0.01,7.586
9/20/1988,0.00,0.00,6.39
9/21/1988,0.00,0.00,6.086
9/22/1988,0.00,0.01,5.242
9/23/1988,0.00,0.01,5.468
9/24/1988,0.00,0.00,7.112
9/25/1988,0.00,0.00,8.564
9/26/1988,0.00,0.01,8.098
9/27/1988,0.00,0.01,7.438
9/28/1988,0.00,0.01,5.027
9/29/1988,0.00,0.01,3.847
9/30/1988,0.00,0.00,3.736
10/1/1988,19.98,0.00,4.882
10/2/1988,1.44,0.00,5.67
10/3/1988,4.76,0.00,5.89
10/4/1988,0.00,0.00,5.984
10/5/1988,0.00,0.01,4.795
10/6/1988,0.46,0.09,1.18
10/7/1988,0.16,0.16,1.71
10/8/1988,2.92,0.09,2.805
10/9/1988,0.00,0.06,3.646
10/10/1988,0.00,0.13,3.978
10/11/1988,3.02,0.11,3.825
10/12/1988,12.22,0.09,4.28
10/13/1988,5.72,0.13,5.557
10/14/1988,0.00,0.13,5.941
10/15/1988,0.00,0.09,6.157
10/16/1988,0.00,0.09,6.48
10/17/1988,2.12,0.16,7.368
10/18/1988,0.00,0.40,7.508
10/19/1988,0.00,0.28,7.449
10/20/1988,0.00,0.21,6.893
10/21/1988,1.74,0.18,7.525
10/22/1988,0.00,0.09,8.059
10/23/1988,0.00,0.09,6.449
10/24/1988,5.74,0.09,2.135
10/25/1988,0.08,0.06,2.628
10/26/1988,8.02,0.04,3.698
10/27/1988,1.08,0.03,3.943
10/28/1988,0.00,0.02,3.831
10/29/1988,2.66,0.01,4.499
10/30/1988,1.66,0.01,5.826
10/31/1988,2.88,0.09,5.149
11/1/1988,4.52,0.03,4.765
11/2/1988,2.92,0.01,4.237
11/3/1988,1.66,0.00,5.34
11/4/1988,1.40,0.00,5.443
11/5/1988,2.44,0.00,2.925
11/6/1988,2.76,0.00,2.2
11/7/1988,4.86,0.00,0.872
11/8/1988,6.00,0.03,1.637
11/9/1988,10.34,0.06,0
11/10/1988,4.36,0.06,0.4189
11/11/1988,2.00,0.81,0.7399
11/12/1988,0.58,4.46,0.0379
11/13/1988,3.72,3.59,0
11/14/1988,0.00,2.04,0.7838
11/15/1988,0.00,1.39,2.604
11/16/1988,0.00,1.94,3.007
11/17/1988,2.56,1.39,3.594
11/18/1988,6.48,0.69,3.388
11/19/1988,0.48,0.69,3.61
11/20/1988,6.92,0.40,4.088
11/21/1988,9.48,0.58,4.012
11/22/1988,2.50,0.49,2.636
11/23/1988,1.92,1.94,4.444
11/24/1988,3.78,3.45,5.406
11/25/1988,0.04,1.56,6.103
11/26/1988,1.52,1.56,6.55
11/27/1988,2.04,1.39,6.061
11/28/1988,0.00,2.46,6.712
11/29/1988,2.32,1.56,7.358
11/30/1988,0.00,1.08,7.037
12/1/1988,0.00,0.88,7.034
12/2/1988,1.04,0.64,5.991
12/3/1988,9.80,0.49,6.684
12/4/1988,0.00,0.40,7.438
12/5/1988,1.26,0.32,7.088
12/6/1988,1.00,0.28,6.931
12/7/1988,2.70,0.25,5.799
12/8/1988,0.76,0.21,3.556
12/9/1988,3.12,0.09,2.857
12/10/1988,0.04,0.09,2.951
12/11/1988,4.00,0.13,3.475
12/12/1988,0.40,0.35,4.755
12/13/1988,0.00,0.35,6.497
12/14/1988,2.36,0.64,6.792
12/15/1988,0.00,0.69,5.049
12/16/1988,1.88,0.49,3.227
12/17/1988,0.64,0.40,3.096
12/18/1988,1.56,0.28,3.878
12/19/1988,2.32,0.25,5.608
12/20/1988,3.18,0.25,6.537
12/21/1988,3.84,0.32,7.767
12/22/1988,1.86,0.32,8.419
12/23/1988,3.28,0.28,8.728
12/24/1988,14.22,1.48,8.467
12/25/1988,4.64,1.48,5.57
12/26/1988,3.20,1.39,3.291
12/27/1988,0.00,23.57,0.897
12/28/1988,4.92,22.46,1.262
12/29/1988,2.02,5.26,1.976
12/30/1988,0.00,43.62,2.227
12/31/1988,0.00,55.83,3.271
I would like top produce a plot like the following using ggplot2
I did it with excel but I would like to plot it using ggplot2 so as I can customize most of stuffs.
The entire dataset for the plot is available in the following link.
https://drive.google.com/file/d/10VXqgHyDOp7V4Ui5qQJD-f2V-7OcRA-e/view?usp=sharing
Help please.
There's some solutions in the following link:
https://rpubs.com/cxiao/hydrograph-ggplot2-plot
I personally use mine
library(ggplot2)
library(data.table)
library(lubridate)
library(tidyverse)
# store the data in csv
data <- fread('data.csv')
data <- melt(data, measure.vars = c('Observed', 'Simulated'), value.name = 'Streamflow') %>%
.[,Date := fast_strptime(Date, '%m/%d/%Y') %>% as_date()]
summary(data)
# Calculate the range needed to avoid having your hyetograph and hydrograph overlap
maxRange <- 600 # set how wide of the first axis (streamflow)
coeff <- 0.4 # set the shrink coeffcient of Precipitation
# Plot the data
ggplot(data = data, aes(x = Date)) +
# Use geom_tile to create the inverted hyetograph
# y = the center point of each bar
# maxRange - Precipitation/coeff/2
geom_tile(aes(y = maxRange - Precipitation/coeff/2,
height = Precipitation/coeff,
fill = 'PColor')
)+
# Plot your discharge data
geom_line(aes(y = Streamflow,
color = variable),
alpha = 0.8,
size = 0.7) +
# Create a second axis with sec_axis() and format the labels to display the original precipitation units.
scale_y_continuous(name = "Streamflow ()",
limit = c(0, maxRange),
expand = c(0, 0),
sec.axis = sec_axis(trans = ~(.-maxRange)*coeff,
name = "Precipitation (mm/d)"))+
scale_fill_manual(values = c('PColor' = "#386cb0"),
labels = c('PColor' = 'Precipitation'),
name = NULL
)+
scale_color_manual(values = c('black', '#e41a1c'),
name = NULL)+
theme_bw()+
guides(color = guide_legend(nrow = 1)) +
theme(
# legend.position = c(0.75, 0.5),
legend.position = 'top',
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
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)
I've run a spline through some points from a regression and I would like to plot them with ggplot2 where the x scale are years, but I'm not sure how to do this.
How would I override the x-scale to go from 1920-1950?
Data:
df <- structure(list(x = 1:200, y = c(0.00122973667762024, 6.62098801946071e-05,
-0.000959979058174531, -0.00185343528846307, -0.00261876396164689,
-0.0032605702287019, -0.00378345924060399, -0.00419203614832906,
-0.00449090610285299, -0.00468467425515169, -0.00477794575620104,
-0.00477532575697695, -0.0046814194084553, -0.00450083186161199,
-0.00423816826742291, -0.00389803377686397, -0.00348503354091104,
-0.00300377271054004, -0.00245885643672684, -0.00185488987044735,
-0.00119647878632586, -0.000489056540407894, 0.000259484465636334,
0.00104079719045607, 0.00184653459270055, 0.00266834963101903,
0.00349789526406075, 0.00432682445047494, 0.00514679014891087,
0.00594944531801776, 0.00672644291644486, 0.00746943590284142,
0.00817007723585667, 0.00882001987413988, 0.00941091677634026,
0.00993442090110708, 0.0103821852070896, 0.010745862652937, 0.0110171061972986,
0.0111875687988235, 0.011248928362097, 0.0111981513301005, 0.0110440035734643,
0.0107968475027119, 0.010467045528367, 0.010064960060953, 0.00960095351099359,
0.00908538828901222, 0.00852862680553249, 0.00794103147107794,
0.00733296469617213, 0.00671478889133861, 0.00609686646710094,
0.00548955983398266, 0.00490323140250733, 0.00434824358319851,
0.00383495878657975, 0.00337373942317461, 0.00297494790350662,
0.00264894663809936, 0.00240601257949406, 0.00224981076448868,
0.00217279857375538, 0.00216634775507979, 0.00222183005624753,
0.00233061722504423, 0.00248408100925552, 0.00267359315666704,
0.00289052541506439, 0.00312624953223322, 0.00337213725595915,
0.00361956033402782, 0.00385989051422484, 0.00408449954433585,
0.00428475917214646, 0.00445204114544233, 0.00457771721200906,
0.00465315911963229, 0.00466973861609765, 0.00461882744919076,
0.00449196986691963, 0.0042874160634374, 0.00401212749412751,
0.00367364780262395, 0.0032795206325607, 0.00283728962757174,
0.00235449843129108, 0.00183869068735268, 0.00129741003939055,
0.000738200131038661, 0.000168604605931003, -0.000403832892298435,
-0.000971568720015669, -0.00152705923358671, -0.00206276078937758,
-0.00257112974375428, -0.00304462245308283, -0.00347569527372924,
-0.00385680456205953, -0.00418040667443971, -0.00443922939659004,
-0.00463224338937841, -0.00476466218882034, -0.00484197076028562,
-0.00486965406914401, -0.00485319708076528, -0.00479808476051921,
-0.00470980207377557, -0.00459383398590413, -0.00445566546227465,
-0.00430078146825693, -0.00413466696922072, -0.00396280693053579,
-0.00379068631757193, -0.00362379009569889, -0.00346760323028646,
-0.00332761068670441, -0.0032092974303225, -0.00311814842651051,
-0.00305964864063822, -0.00303897089201381, -0.00305661736998701,
-0.00310949480371679, -0.00319441743464015, -0.00330819950419407,
-0.00344765525381556, -0.00360959892494162, -0.00379084475900925,
-0.00398820699745545, -0.00419849988171722, -0.00441853765323156,
-0.00464513455343546, -0.00487510482376593, -0.00510526270565997,
-0.00533242244055458, -0.00555339826988675, -0.00576500443509349,
-0.00596405517761179, -0.00614736473887866, -0.00631174736033109,
-0.00645423379806727, -0.00657409002222406, -0.00667190065928587,
-0.00674826737916529, -0.00680379185177487, -0.00683907574702718,
-0.0068547207348348, -0.0068513284851103, -0.00682950066776623,
-0.00678983895271517, -0.00673294500986969, -0.00665942050914235,
-0.00656986712044573, -0.00646488651369238, -0.00634508035879489,
-0.00621105032566582, -0.00606339808421773, -0.0059027253043632,
-0.00572963365601479, -0.00554472480908507, -0.0053486844807736,
-0.00514281955026036, -0.00492871530336347, -0.00470795833913992,
-0.0044821352566467, -0.0042528326549408, -0.0040216371330792,
-0.0037901352901189, -0.00355991372511687, -0.00333255903713012,
-0.00310965782521562, -0.00289279668843038, -0.00268356222583137,
-0.00248354103647558, -0.00229431971942, -0.00211748487372163,
-0.00195462309843743, -0.00180732099262442, -0.00167716515533957,
-0.00156574218563988, -0.00147461474880411, -0.00140521605759051,
-0.00135893575805815, -0.00133716346343512, -0.00134128878694955,
-0.00137270134182952, -0.00143279074130315, -0.00152294659859854,
-0.0016445585269438, -0.00179901613956701, -0.00198770904969631,
-0.00221202687055977, -0.00247335921538553, -0.00277309569740165,
-0.00311262592983628, -0.00349333952591749, -0.00391662609887341,
-0.00438387526193212, -0.00489647662832175, -0.00545581981127037
)), .Names = c("x", "y"), row.names = c(NA, -200L), class = "data.frame")
I've tried this, but no luck:
ggplot(df, aes(x = x, y = y)) + geom_line() + scale_x_discrete(breaks = 1:31, labels = seq(1920,1950))
You can use tidyr::seq_range() for this
library(ggplot2)
library(tidyr)
df$x <- seq_range(1920:1950,dim(df)[1])
ggplot(df, aes(x = x, y = y) ) + geom_line()