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()
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 the following data frame:
df.test <- data.frame(
id = c("EIF3H", "USP9X", "USP44", "USP51", "USP15",
"USP48", "USP47", "USP43", "USPL1", "UCHL5", "USP50", "USP7",
"UCHL1", "USP11", "USP26", "PAN2", "VCPIP1", "USP46", "USP29",
"USP22", "USP49", "ZRANB1", "OTUD4", "OTUD7B", "USP54", "PSMD14",
"USP20", "USP6", "OTUD3", "USP39", "UCHL3", "USP19", "USP21",
"USP30", "TNFAIP3", "USP17L2", "USP32", "JOSD2", "PSMD7", "ATXN3L",
"SENP2", "STAMBPL1", "USP37", "USP35", "USP3", "ALG13", "USP45",
"Control", "USP9Y", "ATXN3", "OTUD6A", "USP42", "USP12", "MPND",
"USP40", "OTUD1", "USP31", "USP8", "USP13", "USP53", "USP34",
"USP17L5", "MYSM1", "USP36", "OTUD7A", "USP10", "USP2", "USP18",
"OTUB1", "EIF3F", "USP1", "USP14", "COPS5", "USP24", "USP4",
"CYLD", "COPS6", "STAMBP", "USP5", "OTUD6B", "BAP1", "USP25",
"YOD1", "USP28", "USP38", "USP41", "JOSD1", "UCK2", "USP16",
"USP27X", "BRCC3", "USP33", "OTUD5", "OTUB2"),
log.score = c(4.22265293851218, 3.03983376346562,
2.4139305569695, 2.32586482009754, 2.30391458369018, 2.19017103893211,
2.10803347738743, 2.10011933499842, 1.82596928196197, 1.79890343496053,
1.78330640083025, 1.58384231036782, 1.4480988629484, 1.4331502122056,
1.41965675282741, 1.37552194849409, 1.37548070593268, 1.3126672736385,
1.27123241483349, 1.25213781606166, 1.1643918571801, 1.14738583497561,
1.0423927129399, 1.03157776352028, 1.0279685056071, 0.953426802337995,
0.94104282122269, 0.929925173732472, 0.886424283199432, 0.886123467368948,
0.815961921373111, 0.811437095842094, 0.767054687254773,
0.754314635766764, 0.750654863646671, 0.728646377897516,
0.707899061519581, 0.703532261199885, 0.692546751828376,
0.684554481775416, 0.652104306506768, 0.642046105413661,
0.630116510664521, 0.62908000782908, 0.619354680809075, 0.614876544107784,
0.61293067306798, 0.606898831140113, 0.603504247802433, 0.578642901486857,
0.576246380387172, 0.549612309171809, 0.53101794103743, 0.513442014568548,
0.506304999011214, 0.492144128304169, 0.462596515841992,
0.454185884038717, 0.450163300207299, 0.434529992991809,
0.429725658566606, 0.42864060724616, 0.419896514762075, 0.409715596281838,
0.365946146577929, 0.363963683646553, 0.357614629472314,
0.352851847129221, 0.343470593766502, 0.313051079788499,
0.304614649499993, 0.291604597354374, 0.287030586811975,
0.272263598289704, 0.27175988000523, 0.265200170411153, 0.264528852761016,
0.244704590019742, 0.179680291853473, 0.154102353851514,
0.147800680553723, 0.127575655021633, 0.126051956011554,
0.1207205737776, 0.118712371231544, 0.11046860245595, 0.0939775902962627,
0.0673791277640148, 0.066320409857141, 0.0582650179118847,
0.0548860857591892, 0.0374554663486737, 0.0147532091971383,
0.0134163514896924),
neg.rank = 1:94)
From this data frame I made this plot:
library(ggplot2)
x <- "neg.rank"
p <- ggplot(df.test, aes_string(x = x, y = df.test$log.score)) +
geom_point()
I want to add labels to the top10 ids and I tried the following:
library(ggrepel)
library(dplyr)
p + geom_label_repel(data = df.test[df.test[[x]] %in% 1:10, ], aes_string(x = x, y = df$log.score, label = df.test$id))
But this gives me a More than one expression parsed error:
More than one expression parsed
Backtrace:
█
1. ├─ggrepel::geom_label_repel(...)
2. │ └─ggplot2::layer(...)
3. └─ggplot2::aes_string(x = x, y = df$log.score, label = df.test$id)
4. └─base::lapply(...)
5. └─ggplot2:::FUN(X[[i]], ...)
6. └─rlang::parse_expr(x)
I have no clue what is wrong with the code.
It is not working as you are inserting the vectors directly into your aes_string.
If you want yours to be working you need to be strict with your aes_string and really should only use strings:
p +
geom_label_repel(
data = df.test[df.test[[x]] %in% 1:10, ],
aes_string(x = x, y = "log.score", label = "id"),
)
I also added a "cleaner" solution. I changed your subsetting logic to use dplyr, as you are already loading the package anyway and changed all your aes_string() to aes().
library(ggplot2)
library(ggrepel)
library(dplyr)
ggplot(df.test, aes(x = neg.rank, y = log.score)) +
geom_point() +
geom_label_repel(
data = df.test %>% slice_min(neg.rank, n = 10),
aes(label = id),
max.overlaps = 10,
xlim = c(10, NA),
ylim = c(3, NA),
direction = "x"
)
Cheers
Hannes
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"))
So I would like to do a kernel density estimation and output both a contour and perspective plot. I am extremely close to figuring out the contour plot but I have hit an error when I run the code below. Additionally, the code I find online does not seem to work for my dataset in regard to a perspective plot for this same data. I am doing this with a bandwidth of .5,.5 .
countourist<- ggplot(df, aes(x = X, y = Y)) +
geom_point() +
xlim(-5, 5) +
ylim(-5, 5)
countourist + stat_density_2d(aes(fill = stat(level)),
countour = TRUE, h = .5,.5,
geom = "polygon")
Sample Data:
structure(list(X = c(-0.0962590773067708, -1.18915207404317,
0.706207022991542, 0.354207143877577, -0.81518898552579, 0.275541521081109,
0.497855536939137, 0.645414156351502, -0.0122766542233017, 0.40578545233698,
0.189360054285873, 0.253754051773016, -0.88127350804621, 0.485445084008632,
0.276018362443043, 0.0145802591922327, -0.181071537886075, 0.121463173714344,
-0.152709201805671, -0.338877208521439, -1.46948728986613, -2.02398725046067,
-0.624336281672069, -1.62555577656191, -0.67130590447829, -0.0335257471517457,
0.198179809536955, -0.63029273337944, -0.694460643800555, -0.411059118662514,
0.221464974231996, 0.430316124954996, -1.6217665981342, -0.881526856414648,
-1.3192875841495, 0.404600053659747, 1.12817344490934, 0.024248421312024,
-0.399749792485403, 0.0857939068657727, -0.217844669409035, 0.101279872558566,
0.291323502766035, -1.42132579845577, 1.2753388274467, -1.33076195534297,
-1.46502902788164, -0.73855874296236, -1.0322620171297, 0.113170727116302
), Y = c(0.0632600046556984, -1.78280440755007, 2.82492518636035,
1.68999775891935, -1.40182480306009, -0.0162214629216463, 1.00914182163537,
0.258321598714745, 0.249412178294172, 0.505938493454907, 0.88745904296611,
0.545487039522648, 0.205393499236483, 1.43415169211384, -0.0293576589272771,
0.373169713548136, 0.462176118409707, -1.11661047785449, 0.0361289986559508,
0.219314070361257, -1.72970101801671, -1.13555829290375, -0.257332488904325,
-1.00336683254877, -2.24667782853137, 0.718672747346788, 0.381256786447137,
-0.0897024758439116, 0.533411392798842, 0.712575478035058, 1.6809613554424,
0.504565244422453, -0.0852144876427054, 0.229922323383187, 0.0269379632874171,
0.644533564497338, 1.49725854460476, 0.382307653466023, -0.41280923134344,
-0.666316793613442, 0.410058708890068, -0.354068636380733, 1.55419054704138,
-1.0573192227978, 1.48806834104077, -1.57278080268752, -0.863516432481153,
-0.908568905792217, -1.30139200592853, 0.896154050079126)), row.names = c(NA,
-50L), class = c("tbl_df", "tbl", "data.frame"))
Let's say I have a saved plot named my_plot, produced with ggplot. Also, let's say that the column in my_plot[[1]] data frame used for horizontal axis is named my_dates
Now, I want to add some vertical lines to the plot, which, of course, can be done by something like that:
my_plot +
geom_vline(aes(xintercept = my_dates[c(3, 8)]))
Since I perform this task quite on a regular basis, I want to write a function for that -- something like that:
ggplot.add_lines <- function(given_plot, given_points) {
finale <- given_plot +
geom_vline(aes(xintercept = given_plot[[1]]$my_dates[given_points]))
return(finale)
}
Which, as it's probably obvious to everyone, doesn't work:
> ggplot.add_lines(my_plot, c(3, 5))
Error in eval(expr, envir, enclos) : object 'given_plot' not found
So, my question would be what am I doing wrong, and how can it be fixed? Below is some data for a reproducible example:
> dput(my_plot)
structure(list(data = structure(list(my_dates = c(1, 2, 3, 4,
5, 6, 7, 8, 9, 10), my_points = c(-2.20176409422924, -1.12872396340683,
-0.259703895194354, 0.634233385649338, -0.678983982973015, -1.83157126614836,
1.33360095418957, -0.120455389285709, -0.969431974863616, -1.20451262626184
)), .Names = c("my_dates", "my_points"), row.names = c(NA, -10L
), class = "data.frame"), layers = list(<environment>), scales = <S4 object of class structure("Scales", package = "ggplot2")>,
mapping = structure(list(x = my_dates, y = my_points), .Names = c("x",
"y"), class = "uneval"), theme = list(), coordinates = structure(list(
limits = structure(list(x = NULL, y = NULL), .Names = c("x",
"y"))), .Names = "limits", class = c("cartesian", "coord"
)), facet = structure(list(shrink = TRUE), .Names = "shrink", class = c("null",
"facet")), plot_env = <environment>, labels = structure(list(
x = "my_dates", y = "my_points"), .Names = c("x", "y"
))), .Names = c("data", "layers", "scales", "mapping", "theme",
"coordinates", "facet", "plot_env", "labels"), class = c("gg",
"ggplot"))
According to this post, below is my solution to this problem. The environment issue in the **ply and ggplot is annoying.
ggplot.add_lines <- function(given_plot, given_points) {
finale <- eval(substitute( expr = {given_plot +
geom_vline(aes(xintercept = my_dates[given_points]))}, env = list(given_points = given_points)))
return(finale)
}
The following code runs well on my machine. (I cannot make your reproducible work on my machine...)
df <- data.frame(my_dates = 1:10, val = 1:10)
my_plot <- ggplot(df, aes(x = my_dates, y = val)) + geom_line()
my_plot <- ggplot.add_lines(my_plot, c(3, 5))
print(my_plot)
Update: The above solution fails when more than two points are used.
It seems that we can easily solve this problem by not including the aes (subsetting together with aescauses problems):
ggplot.add_lines <- function(given_plot, given_points) {
finale <- given_plot + geom_vline(xintercept = given_plot[[1]]$my_dates[given_points])
return(finale)
}
I would take the following approach: extract the data.frame of interest, and pass it to the new layer,
df <- data.frame(my_dates = 1:10, val = rnorm(10))
my_plot <- ggplot(df, aes(x = my_dates, y = val)) + geom_line()
add_lines <- function(p, given_points=c(3,5), ...){
d <- p[["data"]][given_points,]
p + geom_vline(data = d, aes_string(xintercept="my_dates"), ...)
}
add_lines(my_plot, c(3,5), lty=2)