Combine two faceted plots on one plot - r

Sorry if this is a duplicate question but I cannot seem to find the answer to my question anywhere. I have two plots and I would like to overlay plot two on plot one so that they form one plot. Is this possible? I will attach how both plots look separately. They are both facetted by the same variable which is by location and are on the same x and y-axis scale so theoretically should be possible.
Thank you.
## Plot one
Proxy<-read.csv("ALLRSL.csv",header=T)
p1<-ggplot()+
geom_ribbon(data=Proxy,aes(x=YEAR,ymin=LOWER,ymax=UPPER,fill=SITE),alpha=.5)+
geom_line(data=Proxy,aes(x=YEAR,y=RSL,col=SITE))+
facet_wrap(~ SITE,ncol= 1)+
scale_fill_manual(values=c("#4E193D","#342955","#4E617E","#97B4CB"))+
scale_color_manual(values=c("#4E193D","#342955","#4E617E","#97B4CB"))+
theme_classic()+
xlim(1900, 2020)+
theme(panel.grid.major.x = element_blank())+
theme(panel.grid.minor.x = element_blank())+
theme(panel.grid.minor.y = element_blank())+
theme(panel.grid.major.y = element_blank())+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank())+
theme(
strip.background = element_blank(),
strip.text.x = element_blank()
)+
theme(legend.position="none")
p1
plot two
tgsm<-read.csv("tgsm.csv",header=T)
tgsm<-na.omit(tgsm)
tglonger<-pivot_longer(tgsm, cols=c(-Year),names_to="Site", values_to = "value")
p2<-ggplot()+
geom_point(data=tglonger,aes(x=Year,y=value,col=Site),alpha=.7,size=1)+
facet_wrap(~Site,ncol=1)+
theme_classic()+
xlim(1900,2020)+
scale_color_manual(values=c("#4E193D","#342955","#4E617E","#97B4CB"))+
theme(panel.grid.major.x = element_blank())+
theme(panel.grid.minor.x = element_blank())+
theme(panel.grid.minor.y = element_blank())+
theme(panel.grid.major.y = element_blank())+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank())+
theme(
strip.background = element_blank(),
strip.text.x = element_blank()
)+
theme(legend.position="none")
p2
Data
Proxy <- structure(list(RSL = c(-0.305251214, -0.306414006, -0.307194187,
-0.308202139, -0.309150572, -0.309679123), UPPER = c(-0.182716456,
-0.186724068, -0.189331305, -0.193118273, -0.197069799, -0.20118809
), LOWER = c(-0.416725663, -0.413606073, -0.411131729, -0.408930899,
-0.406531588, -0.404478981), YEAR = 1820:1825, SITE = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = c("Little Swanport", "Lutregala",
"Tarra", "Wapengo"), class = "factor")), row.names = c(NA, 6L
), class = "data.frame")
tgsm <- structure(list(Year = 1993:1998, Lg2002 = c(-0.001164223, -0.002229453,
-0.002734792, -0.002977787, -0.002786098, -0.002026994), Wap2002 = c(-0.002531348,
-0.002051993, -0.001468704, -0.001182162, -0.001027132, -0.00020881
), Tar2002 = c(-0.029020612, -0.024330561, -0.019927593, -0.015682528,
-0.012907219, -0.009784772), LSP2002 = c(-0.034514531, -0.030171621,
-0.026095138, -0.021952898, -0.018480702, -0.014531318)), na.action = structure(c(`1` = 1L,
`2` = 2L, `3` = 3L, `4` = 4L, `5` = 5L, `6` = 6L, `7` = 7L, `8` = 8L,
`9` = 9L, `10` = 10L, `11` = 11L, `12` = 12L, `13` = 13L, `14` = 14L,
`15` = 15L, `16` = 16L, `17` = 17L, `18` = 18L, `19` = 19L, `20` = 20L,
`21` = 21L, `22` = 22L, `23` = 23L, `24` = 24L, `25` = 25L, `26` = 26L,
`27` = 27L, `28` = 28L, `29` = 29L, `30` = 30L, `31` = 31L, `32` = 32L,
`33` = 33L, `34` = 34L, `35` = 35L, `36` = 36L, `37` = 37L, `38` = 38L,
`39` = 39L, `40` = 40L, `41` = 41L, `42` = 42L, `43` = 43L, `44` = 44L,
`45` = 45L, `46` = 46L, `47` = 47L, `48` = 48L, `49` = 49L, `50` = 50L,
`51` = 51L, `52` = 52L, `53` = 53L, `54` = 54L, `55` = 55L, `56` = 56L,
`57` = 57L, `58` = 58L, `59` = 59L, `60` = 60L, `61` = 61L, `62` = 62L,
`63` = 63L, `64` = 64L, `65` = 65L, `66` = 66L, `67` = 67L, `68` = 68L,
`69` = 69L, `70` = 70L, `71` = 71L, `72` = 72L, `73` = 73L, `74` = 74L,
`75` = 75L, `76` = 76L, `77` = 77L, `78` = 78L, `79` = 79L, `80` = 80L,
`81` = 81L, `82` = 82L, `83` = 83L, `84` = 84L, `85` = 85L, `86` = 86L,
`87` = 87L, `88` = 88L, `89` = 89L, `90` = 90L, `91` = 91L, `92` = 92L,
`93` = 93L, `94` = 94L, `95` = 95L, `96` = 96L, `97` = 97L, `98` = 98L,
`99` = 99L, `100` = 100L, `101` = 101L, `102` = 102L, `103` = 103L,
`104` = 104L, `105` = 105L, `106` = 106L, `107` = 107L, `108` = 108L,
`109` = 109L, `110` = 110L, `111` = 111L, `112` = 112L, `113` = 113L,
`114` = 114L, `115` = 115L, `116` = 116L, `117` = 117L, `118` = 118L,
`119` = 119L, `120` = 120L, `121` = 121L, `122` = 122L, `123` = 123L,
`124` = 124L, `125` = 125L, `126` = 126L, `127` = 127L, `128` = 128L,
`129` = 129L, `130` = 130L, `131` = 131L, `132` = 132L, `133` = 133L,
`134` = 134L, `135` = 135L, `136` = 136L, `137` = 137L, `138` = 138L,
`139` = 139L, `140` = 140L, `141` = 141L, `142` = 142L, `143` = 143L,
`144` = 144L, `145` = 145L, `146` = 146L, `147` = 147L, `148` = 148L,
`149` = 149L, `150` = 150L, `151` = 151L, `152` = 152L, `153` = 153L,
`154` = 154L, `155` = 155L, `156` = 156L, `157` = 157L, `183` = 183L
), class = "omit"), row.names = 158:163, class = "data.frame")

See plot one how you can do that with patchwork.
However. Conceptually, I am guessing you want to add a sort of prediction to some historic values or so. I personally would put everything in one data frame and plot this. If there is a too large gap between the two time points, you can facet by timepoints (as in my suggestion).
The plots look a bit different than your plot because you only provided data for one Site in Proxy (so I filtered the other for what I thought is the equivalent, it will work nonetheless, because the faceting remains) - and I removed all those theme elements that are not relevant to the problem.
Plot one - combining plots.
library(tidyverse)
library(patchwork)
tgsm<-na.omit(tgsm)
tglonger <-
pivot_longer(tgsm, cols=c(-Year), names_to="SITE", values_to = "RSL") %>%
filter(SITE == "LSP2002") %>%
rename(YEAR = Year)
p1 <- ggplot() +
geom_ribbon(data = Proxy, aes(x = YEAR, ymin = LOWER, ymax = UPPER, fill = SITE), alpha = .5) +
geom_line(data = Proxy, aes(x = YEAR, y = RSL, col = SITE)) +
facet_wrap(~SITE) +
coord_cartesian(xlim = c(1800, 1830), ylim = c(-1, 0)) +
theme_classic() +
theme(
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
strip.background = element_blank(),
strip.text.x = element_blank(),
legend.position = "none"
)
p2 <- ggplot() +
geom_point(data = tglonger, aes(x = YEAR, y = RSL, col = SITE), alpha = .7, size = 1) +
facet_wrap(~SITE) +
coord_cartesian(xlim = c(1990, 2000), ylim = c(-1, 0)) +
theme_classic() +
## only one call to theme!!
theme(
## this is where the theme call is different to above
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.line.y = element_blank(),
strip.background = element_blank(),
strip.text.x = element_blank(),
legend.position = "none",
)
p1 + p2
Suggestion for an alternative visualisation
df_new <-
bind_rows(time1 = Proxy, time2 = tglonger, .id = "timevar") %>%
mutate(SITE = "LSP2002")
ggplot(df_new)+
geom_point(aes(x=YEAR,y=RSL))+
facet_grid(SITE~timevar, scales = "free_x")+
theme(legend.position="none") +
theme(panel.spacing = unit(.5, "lines"))
You can also use this data frame in order to create a list of plots, and then stitch it together with patchwork. This approach doesn't allow to change individual plots though.
ls_p <-
df_new %>%
split(., .$timevar) %>%
map(~{ggplot(.x)+
geom_point(aes(x=YEAR,y=RSL))+
coord_cartesian(ylim = c(-0.4,0))+
facet_grid(~SITE, scales = "free_x")+
theme(legend.position="none") +
theme(panel.spacing = unit(.5, "lines"))})
library(patchwork)
wrap_plots(ls_p)

Related

Event Analysis around Earnings Announcement Dates with multiple firms and multiple dates

I am trying to compute Cumulative Abnormal Returns per firm around the EPS announcement dates by using the EvReturn function from the ererpackage. I have a dataframe (df) with column Date (time-series), 5 columns with daily firms returns, and last column with the market index returns. The second dataframe (Dates_clean), consists of 2 columns, one referring to the firms and the second with the announcement dates. This dataframe has been tidied and cleaned from NAs and it is possible that a firm is not present due to missing observations regarding EPS dates.
df looks as follow (subset only for the last 2 years, daily observations of returns):
df <-- structure(list(Date = c("2021-12-31", "2022-01-03", "2022-01-04",
"2022-01-05", "2022-01-06", "2022-01-07", "2022-01-10", "2022-01-11",
"2022-01-12", "2022-01-13", "2022-01-14", "2022-01-17", "2022-01-18",
"2022-01-19", "2022-01-20", "2022-01-21", "2022-01-24", "2022-01-25",
"2022-01-26", "2022-01-27", "2022-01-28", "2022-01-31", "2022-02-01",
"2022-02-02", "2022-02-03", "2022-02-04", "2022-02-07", "2022-02-08",
"2022-02-09", "2022-02-10", "2022-02-11", "2022-02-14", "2022-02-15",
"2022-02-16", "2022-02-17", "2022-02-18", "2022-02-21", "2022-02-22",
"2022-02-23", "2022-02-24", "2022-02-25", "2022-02-28", "2022-03-01",
"2022-03-02", "2022-03-03", "2022-03-04", "2022-03-07", "2022-03-08",
"2022-03-09", "2022-03-10", "2022-03-11", "2022-03-14", "2022-03-15",
"2022-03-16", "2022-03-17", "2022-03-18", "2022-03-21", "2022-03-22",
"2022-03-23", "2022-03-24", "2022-03-25", "2022-03-28", "2022-03-29",
"2022-03-30", "2022-03-31", "2022-04-01", "2022-04-04", "2022-04-05",
"2022-04-06", "2022-04-07", "2022-04-08", "2022-04-11", "2022-04-12",
"2022-04-13", "2022-04-14", "2022-04-15", "2022-04-18", "2022-04-19",
"2022-04-20", "2022-04-21", "2022-04-22", "2022-04-25", "2022-04-26",
"2022-04-27", "2022-04-28", "2022-04-29"), NESN = c(NA, 0.0128217636632681,
0.0055390098819148, -0.0303446403908639, -0.00353202700477517,
-0.0148639604729367, -0.0242468464841654, 0.000833755941585634,
0.000566825263230264, -0.00107292453477992, -0.00413304805849857,
0.00944796286389749, 0.00606019163539706, -0.0178671560381302,
0.0102682549157, 0.00238042231089342, -0.0264129023440169, -0.00359737479380895,
-0.00057237456191328, 0.00371815995136426, -0.00208920373247656,
0.00360658333406638, 0.00460158995889248, 0.0124240943672786,
-0.0192088934850052, -0.0116772543954452, 0.00161803666364402,
-0.00947063835830442, 0.00910418738855001, -0.0111621216023017,
0.0149520456912964, -0.00898947983118181, -0.000580331235212728,
-0.00085760228693943, 0.00337970744966198, 0.00873269055978332,
0.0104415116340701, -0.00548153587914391, 0.00615346747892431,
-0.0248914439483823, 0.0173285069170668, 0.0237445174472881,
0.0114592474061839, -0.00044228969975324, -0.0129341888051192,
-0.0039655856135451, -0.0259827935397877, -0.0295994170754248,
0.0307311936266654, -0.013299455406402, -0.0056093783764315,
0.0110647313950434, 0.00334040819250792, 0.00425756006997746,
0.0172069005359381, 0.0251378343192163, -0.0021649080340248,
0.00189626896268957, -0.0135983085238801, 0.0114262007450368,
-0.00381128173575684, 0.0058674672957324, -0.00307865494333059,
0.00175366558880397, 0.00248499184478623, 0.00721501950695935,
0.0140475807883897, 0.0150789811679537, -0.0124887026538493,
0.00613195773358854, 0.0132145840052262, -0.0015996735360132,
-0.0066051386016398, 0.00478929567729036, 0.000712513206063692,
0, -0.00234061707177347, -0.0254790654941593, -0.002887253994175,
0.0031573171246464, 0.0188337863652812, 0.0128193479643504, -0.0131137915005016,
0.0142879572931387, 0.00363369724621165, -0.00110402156089173
), MC = c(NA, 0.00550261575073563, 0.0177834448996657, 0.0188176125853796,
-0.0407649022737596, -0.0155420098162522, -0.0183010694192803,
0.0103886467592562, -0.00183070448319778, -0.0263867607751148,
-0.021159281495444, 0.0162863512416866, -0.0195218616791354,
0.036701342749085, 0.00888605545163479, -0.00951769122788382,
-0.049340634153576, 0.00633645237423841, 0.0248878672659074,
0.0152138699844748, 0.0322766524097042, 0.00865438181820477,
0.0132850798110877, -0.00396012072659391, -0.0241332685589187,
-0.00786778247404551, 0.0141615838200913, -0.0139641131076824,
0.014870269120445, -0.0145131154740279, -0.0300199660152931,
-0.0218975214448938, 0.0358204424148594, -0.00922132830585842,
-0.00509031265552273, 0.00321590363749413, -0.0205452396116511,
-0.00446266058653078, -0.00508069260599331, -0.0449083741501392,
0.0482780235448934, -0.0100514536376457, -0.0516743602589363,
0.0194952092513918, -0.0322887147335422, -0.0641394765595972,
-0.0173068505872533, -0.0311734655958205, 0.0954377559421478,
-0.0243947811500892, 0.0119067888938236, 0.00638796543754672,
-0.0145319835423444, 0.065932734214486, 0.00143122890265102,
0.0152422943022248, -0.0096967499833438, 0.0168987761680872,
-0.0304396645441839, 0.000640877885772717, 0.00208067872578832,
0.0154956918516587, 0.0567879413578825, -0.0168206261325913,
-0.0168057506728613, 0.000308592594485901, 0.0220126129867861,
-0.00451860424097195, -0.0373729875542405, -0.0191766740018862,
0.0123403885706372, -0.0186802220420382, 0.0177444777777833,
0.005072438614252, 0.00930500067891504, 0, -3.12499902288543e-07,
-0.00921874999999994, 0.0129321873521526, 0.0172808575677141,
-0.0211199942209961, -0.0375232958098812, -0.0134827788364557,
0.0153135163323701, 0.000972753811222749, 0.00372650801247842
), ASML = c(NA, -0.00707457495736874, -0.0290725339785473, -0.0152647901460136,
-0.0184823371590401, 0.00971814435287954, -0.0640693722781605,
0.00755262856381633, 0.0307819825808584, 0.0255294675755942,
-0.0310802702293709, 0.0217999099657287, -0.0225541062341073,
-0.0236980117549149, 0.0220375314655912, -0.0167179739743669,
-0.070396438711375, -0.0162393217912211, 0.0253696004759689,
-0.0127093712930013, -0.0283213085062449, 0.0496373256889857,
0.0104341989959946, -0.00316405782212825, -0.0412701616473979,
-0.0167302195887068, 0.014887800465154, -0.0106529882383843,
0.0393645189761693, -0.00798216711956512, -0.024996058038682,
-0.017207904689711, 0.0235835268894051, 0.00174602988969963,
0.00749259452866369, -0.0134901418194397, -0.0343618513323983,
0.0248730936819173, -0.000354295774261049, -0.0108096737888227,
0.0553563040643841, 0.0166350336725456, -0.0362331061055623,
0.010395360497353, -0.0169754801097395, -0.069247700897907, 0.0131186232688818,
-0.0436560986000191, 0.077563666368923, -0.0210021464720349,
-0.00476696090336659, -0.0152907148120854, 0.011037414017806,
0.0712443030593835, 0.0100189998239766, 0.0430985046864196, 0.00459075408780252,
0.0236664062345713, -0.0212058320781517, 0.00293240036892017,
0.00730826701315568, 0.00403160465895103, 0.0165410283377181,
-0.0170617639615278, -0.0196080007058823, -0.00540967213114751,
0.0176357313934843, -0.0294781484344746, -0.0457273107021545,
-0.00769569645056023, 0.00334966691029259, -0.029861744320177,
0.0146653992395438, 0.010171668543006, -0.0130715444394179, 0,
-1.7898690185536e-07, 0.00519062108466084, 0.0530628561253561,
-0.0125130178368247, -0.0219178119722282, -0.0411412887152115,
-0.0334124520723025, -0.00132262939176431, 0.0378286505877248,
-0.00127556088046676), ROG = c(NA, -0.0016563124362825, -0.0167576047110743,
0.00558547220350492, -0.00931186868686873, 0.00929598170989054,
-0.0189468728798304, 0.0102378674073382, -0.0211679240011176,
-0.0090002397780341, 0.00835899311866473, 0.00677858618059668,
-0.004236109928897, 0.00246186811339411, -3.6951028517751e-05,
-0.0085303180179872, -0.0453608483871986, 0.00297013943738533,
0.00575500793484429, 0.0165975968466054, 0.00233697546175771,
-0.000929104641140599, 0.0127388162493018, -0.00699098766109441,
-0.034148341582287, -0.0182875241992058, 0.0008188551362458,
0.0038028755990831, 0.0290255912422577, -0.00518866385230854,
-0.014558705998831, -0.0111877742472386, 0.0114612566678807,
0.00773075782606858, -0.00964711322222001, -0.00537533349502783,
-0.00277687179752917, 0.0100685911308367, 0.00239370555303364,
-0.00492692631648861, 0.0156943530061375, 0.0141076879880042,
0.0095079601184076, -0.00810543484286608, -0.0153314409260049,
-0.00265576875393958, 0.011543968321482, 0.00650858137799504,
0.0172871323248192, -0.013768790740987, 0.00885575961597662,
0.0135187375923218, -5.44893487662845e-05, -0.00846638540982525,
0.00148964049045319, 0.0260458184590857, 0.0049176082464939,
-0.000655460348850556, -0.000953002940294323, 0.0201022930361616,
0.00785770864045432, -0.00585620372821327, -0.0105159715950733,
-0.00101256130850647, -0.00614261872165789, 0.0145685186996185,
0.0366446438948085, 0.00785569119190255, 0.0111315542528179,
0.0105840795630807, 0.0110330061523778, 0.00756508782135668,
-0.0247558224043232, 0.000999685813030116, -0.00142410234860157,
0, -0.00234052114911543, -0.0406737559105963, -0.0119804035990065,
-0.0253254619925773, 0.00693062609568162, -0.00655543146327209,
-0.0173983359583453, 0.0239373293877456, -0.00889126611629498,
-2.8255212379813e-06), DJSTOXX = c(NA, 0.00448956330284256, 0.00822265035062086,
0.000665968984872922, -0.0125074340644002, -0.00391672453668146,
-0.014833902655213, 0.00842939391029529, 0.00646896140565878,
-0.000302344092258267, -0.0100667827031564, 0.00696648315220894,
-0.00975618817245349, 0.00231978423713897, 0.00508630710398661,
-0.0184133486433087, -0.0381245455216094, 0.00708217470094685,
0.0167954411441527, 0.00646040726886765, -0.0101525109126204,
0.00715709523318564, 0.0127472893082297, 0.0045192172025077,
-0.0175615871465199, -0.0138360753686276, 0.0067878981966738,
0.000120356858084092, 0.0171767249252694, -0.00207254104941135,
-0.00589392210453665, -0.0183275230158848, 0.0143092365731592,
0.000447004322467581, -0.0068709568459443, -0.00805937763694919,
-0.0130249474835507, 0.000692602999960323, -0.00277069175010591,
-0.0328295068963999, 0.0331852560597776, -0.000926074963563228,
-0.0237052175314, 0.00896542681851686, -0.020101628384252, -0.0356274007682458,
-0.0110105316066746, -0.00508229969266472, 0.0468249263880254,
-0.0168766270684341, 0.00950095407560014, 0.0120160585194342,
-0.00282797911787214, 0.0306352270637984, 0.00455346192440631,
0.0091122594851849, 0.000435550608561153, 0.00846535252146574,
-0.0100600682445028, -0.00211439307007666, 0.00104840080252311,
0.00137141546833175, 0.0174494900565429, -0.00411821862698636,
-0.00940915708728995, 0.00543807309261601, 0.00839116898191072,
0.00191698129536877, -0.015334581522056, -0.0020812772770139,
0.0130697264521857, -0.00588544107183153, -0.0035024167766855,
0.000289061644585642, 0.00664430735011456, 0, 0, -0.00769436536709167,
0.00837864552170919, 0.00318842343654246, -0.0178911495356058,
-0.0180825084765205, -0.00902690097368308, 0.00728639570524336,
0.00620287637010186, 0.00743961741666777)), class = "data.frame", row.names = c(NA,
-86L))
Whereas, Dates_clean is:
Dates_clean <-- structure(list(stock = c("NESN", "NESN", "MC", "ASML", "ASML",
"ROG", "ROG"), EPS = structure(c(19103, 19040, 19019, 19102,
19011, 19107, 19026), class = "Date")), row.names = c("33", "130",
"260", "293", "390", "423", "520"), class = "data.frame", na.action = structure(c(`NA` = 1L,
NA.1 = 2L, NA.2 = 3L, NA.3 = 4L, NA.4 = 5L, NA.5 = 6L, NA.6 = 7L,
NA.7 = 8L, NA.8 = 9L, NA.9 = 10L, NA.10 = 11L, NA.11 = 12L, NA.12 = 13L,
NA.13 = 14L, NA.14 = 15L, NA.15 = 16L, NA.16 = 17L, NA.17 = 18L,
NA.18 = 19L, NA.19 = 20L, NA.20 = 21L, NA.21 = 22L, NA.22 = 23L,
NA.23 = 24L, NA.24 = 25L, NA.25 = 27L, NA.26 = 28L, NA.27 = 29L,
NA.28 = 30L, NA.29 = 31L, NA.30 = 32L, NA.31 = 33L, NA.32 = 34L,
NA.33 = 35L, NA.34 = 36L, NA.35 = 37L, NA.36 = 38L, NA.37 = 39L,
NA.38 = 40L, NA.39 = 41L, NA.40 = 42L, NA.41 = 43L, NA.42 = 44L,
NA.43 = 45L, NA.44 = 46L, NA.45 = 47L, NA.46 = 48L, NA.47 = 49L,
NA.48 = 50L, NA.49 = 51L, NA.50 = 52L, NA.51 = 53L, NA.52 = 54L,
NA.53 = 55L, NA.54 = 57L, NA.55 = 58L, NA.56 = 59L, NA.57 = 60L,
NA.58 = 61L, NA.59 = 62L, NA.60 = 63L, NA.61 = 64L, NA.62 = 65L,
NA.63 = 66L, NA.64 = 67L, NA.65 = 68L, NA.66 = 69L, NA.67 = 70L,
NA.68 = 71L, NA.69 = 72L, NA.70 = 73L, NA.71 = 74L, NA.72 = 75L,
NA.73 = 76L, NA.74 = 77L, NA.75 = 78L, NA.76 = 79L, NA.77 = 80L,
NA.78 = 81L, NA.79 = 82L, NA.80 = 83L, NA.81 = 84L, NA.82 = 85L,
NA.83 = 86L, NA.84 = 87L, NA.85 = 88L, NA.86 = 89L, NA.87 = 90L,
NA.88 = 91L, NA.89 = 92L, NA.90 = 93L, NA.91 = 94L, NA.92 = 95L,
NA.93 = 96L, NA.94 = 97L, NA.95 = 98L, NA.96 = 99L, NA.97 = 100L,
NA.98 = 101L, NA.99 = 102L, NA.100 = 103L, NA.101 = 104L, NA.102 = 106L,
NA.103 = 107L, NA.104 = 108L, NA.105 = 109L, NA.106 = 110L, NA.107 = 111L,
NA.108 = 112L, NA.109 = 113L, NA.110 = 114L, NA.111 = 115L, NA.112 = 116L,
NA.113 = 117L, NA.114 = 118L, NA.115 = 119L, NA.116 = 120L, NA.117 = 121L,
NA.118 = 122L, NA.119 = 124L, NA.120 = 125L, NA.121 = 126L, NA.122 = 127L,
NA.123 = 128L, NA.124 = 129L, NA.125 = 130L, NA.126 = 131L, NA.127 = 132L,
NA.128 = 133L, NA.129 = 134L, NA.130 = 135L, NA.131 = 136L, NA.132 = 137L,
NA.133 = 138L, NA.134 = 139L, NA.135 = 140L, NA.136 = 141L, NA.137 = 142L,
NA.138 = 143L, NA.139 = 144L, NA.140 = 145L, NA.141 = 146L, NA.142 = 147L,
NA.143 = 148L, NA.144 = 149L, NA.145 = 150L, NA.146 = 151L, NA.147 = 152L,
NA.148 = 153L, NA.149 = 155L, NA.150 = 156L, NA.151 = 157L, NA.152 = 158L,
NA.153 = 159L, NA.154 = 160L, NA.155 = 161L, NA.156 = 162L, NA.157 = 163L,
NA.158 = 164L, NA.159 = 165L, NA.160 = 166L, NA.161 = 167L, NA.162 = 168L,
NA.163 = 169L, NA.164 = 170L, NA.165 = 171L, NA.166 = 172L, NA.167 = 173L,
NA.168 = 174L, NA.169 = 175L, NA.170 = 177L, NA.171 = 178L, NA.172 = 179L,
NA.173 = 180L, NA.174 = 181L, NA.175 = 182L, NA.176 = 183L, NA.177 = 184L,
NA.178 = 185L, NA.179 = 186L, NA.180 = 187L, NA.181 = 188L, NA.182 = 189L,
NA.183 = 190L, NA.184 = 191L, NA.185 = 192L, NA.186 = 193L, NA.187 = 194L,
NA.188 = 195L, NA.189 = 196L, NA.190 = 197L, NA.191 = 198L, NA.192 = 199L,
NA.193 = 200L, NA.194 = 201L, NA.195 = 202L, NA.196 = 203L, NA.197 = 204L,
NA.198 = 205L), class = "omit"))
Those are subsets of my original dataframes, encompassing more than 20 years of daily observations and 250 firms.
My goal is to compute automatically cumulative abnormal returns for each firm around each earnings announcement date. I was able to compute the simple 1 firm or multiple firms with 1 event date in common for all. I am struggling in telling R how to match each date and firm with the dataframe containing returns and then compute the cumulative abnormal returns.
Until now I have tried this code:
hh2 <- list()
for(i in Dates_clean[2]){
firms <- colnames(df)[2:6]
hh2[[i]] <- evReturn(y = df, firm = firms, event.date = i, y.date = "Date", index = "DJSTOXX", event.win = 3, est.win = 100, digits = 4)}
Error in xj[i] : only 0's may be mixed with negative subscripts
Any help is highly appreciated.

Plot multiple regression lines on one plot in ggplot2

Sorry if this is a repeat question but I haven't managed to find an answer yet since my data frame has to be split. I am trying to plot two regression lines on one plot, with a regression line for data in period 1 (1815-1899)and a regression line for data in period 2 (1900-2013). I have used dplyr to split the data to run the two separate regressions but can't work out how to get them on the same graph as you seem to need the data frame in the ggplot() command for it to plot the line. Can anyone help?
Thanks.
library(tidyverse)
brest<-read.csv("brest.csv",header=TRUE) ## read in csv
brest<- na.omit(brest) ## get rid of NAs
brestp1<- select(filter(brest, period == 1),c(year,slr,period)) ## Divide into periods
brestp2<- select(filter(brest, period == 2),c(year,slr,period))
fit1 <- lm(slr ~ year, data = brestp1) ## Run lms
summary(fit1)
fit2<- lm(slr ~ year, data = brestp2)
summary(fit2)
## plot graph
ggplot(brestp1, aes(x = year, y = slr)) + ### Need not only brestp1 but also brestp2
geom_point() +
stat_smooth(method = "lm",se=FALSE)+
theme_classic()
## Data
## Brest period 1
structure(list(year = 1815:1820, slr = c(6926L, 6959L, 6945L,
6965L, 6941L, 6909L), period = c(1L, 1L, 1L, 1L, 1L, 1L)), na.action = structure(c(`30` = 30L,
`31` = 31L, `32` = 32L, `33` = 33L, `34` = 34L, `35` = 35L, `36` = 36L,
`37` = 37L, `38` = 38L, `39` = 39L, `51` = 51L, `52` = 52L, `53` = 53L,
`54` = 54L, `138` = 138L, `139` = 139L, `140` = 140L, `141` = 141L,
`142` = 142L, `143` = 143L, `144` = 144L, `145` = 145L, `146` = 146L
), class = "omit"), row.names = c(NA, 6L), class = "data.frame")
##Brest period 2
structure(list(year = 1900:1905, slr = c(6936L, 6916L, 6923L,
6976L, 6931L, 6913L), period = c(2L, 2L, 2L, 2L, 2L, 2L)), na.action = structure(c(`30` = 30L,
`31` = 31L, `32` = 32L, `33` = 33L, `34` = 34L, `35` = 35L, `36` = 36L,
`37` = 37L, `38` = 38L, `39` = 39L, `51` = 51L, `52` = 52L, `53` = 53L,
`54` = 54L, `138` = 138L, `139` = 139L, `140` = 140L, `141` = 141L,
`142` = 142L, `143` = 143L, `144` = 144L, `145` = 145L, `146` = 146L
), class = "omit"), row.names = c(NA, 6L), class = "data.frame")
Use geom_smooth with separate data:
ggplot() +
geom_smooth(aes(x = year, y = slr), data = brest1,
method = "lm", se = FALSE, color = "red") +
geom_smooth(aes(x = year, y = slr), data = brest2,
method = "lm", se = FALSE, color = "blue") +
geom_point(aes(x = year, y = slr), data = brest1, color = "red") +
geom_point(aes(x = year, y = slr), data = brest2, color = "blue")

Combine scatterplot and barplot, then lapply

I am trying to add a scatterplot and a barplot within the same plot area with ggplot. The scatterplot should be averages of var. '1' over var.'2' for one dataset, and the barplot should be the average value of '1' over my control dataset.
My data looks like this:
> dput(lapply(ubbs6, head))
list(structure(c(96L, 96L, 100L, 88L, 93L, 100L, 61L, 61L, 70L,
40L, 58L, 70L, 7807L, 7357L, 7695L, 6400L, 6009L, 7735L), .Dim = c(6L,
3L), .Dimnames = list(NULL, c("1", "2", "3"))), structure(c(99L,
96L, 100L, 96L, 96L, 96L, 66L, 67L, 70L, 63L, 57L, 62L, 7178L,
6028L, 6124L, 6082L, 6873L, 5629L, 31L, 27L, 60L, 42L, 12L, 18L
), .Dim = c(6L, 4L), .Dimnames = list(NULL, c("1", "2",
"3", "4"))), structure(c(99L, 95L, 95L, 100L, 96L, 95L, 69L,
58L, 56L, 70L, 61L, 65L, 6067L, 6331L, 6247L, 5988L, 7538L, 6162L,
50L, 36L, 67L, 10L, 55L, 70L), .Dim = c(6L, 4L), .Dimnames = list(
NULL, c("1", "2", "3", "4"))))
Example of what I've tried so far:
aggregate(ubbs6[[2]][,'1'], list(ubbs6[[2]][,'2']), mean)
m162 <- aggregate(ubbs6[[2]][,'1'], list(ubbs6[[2]][,'2']), mean)
m163 <- aggregate(ubbs6[[3]][,'1'], list(ubbs6[[3]][,'2']), mean)
m161 <- mean(ubbs6[[1]][,'1'])
ggplot(m162, aes_(x = m162[,'Group.1'], y = m162[,'x']))+
geom_point()+
geom_smooth(method = 'lm', formula = 'y ~ sqrt (x)')
I would like to do two things:
add a barplot of one x,y value of my control set (ubbs6[[1]])
throw this into a lapply structure so I can do this for 11 similar datasets
Any help would be greatly appreciated!
**EDIT: edited out specific details that aren't needed for others to understand the code **
Saving your data in d, you can try
ggplot(as.data.frame(d[[2]]),aes(age, FPAR) ) +
coord_cartesian(ylim = c(90,100)) +
geom_point() +
geom_smooth(method = 'lm', formula = 'y ~ sqrt (x)') +
geom_col(data=data.frame(x=max(as.data.frame(d[[2]])$age),
y=mean(as.data.frame(d[[1]])$FPAR)),
aes(x,y), inherit.aes = FALSE)
You have to use coord_cartesian to specify the y-limits and inherit.aes = FALSE. Otherwise the bar is not correctly drawn.
When you have to combine your second and third dataframe in one plot, you can try
library(tidyverse)
d %>%
.[2:3] %>%
map(as.data.frame) %>%
bind_rows(.id = "id") %>%
mutate(max = max(age),
Mean = mean(d[[1]][1])) %>%
ggplot(aes(age, FPAR, color=id)) +
geom_point() +
geom_smooth(method = 'lm', formula = 'y ~ sqrt (x)', se=FALSE) +
geom_col(data = . %>% distinct(max, Mean),
aes(max, Mean), inherit.aes = FALSE)

Adding axis labels and title to ggballoonplot()

The code I used and the result can be seen in the image below. The main problem is that the title doesn't appear in the center and the x and y labels don't appear at all. How do I fix this?
The graph and code
You should upload your code as a snippet and your data so we can reproduce this on our own machines easily...
Take the example below. You can recreate the data set and then run the code immediately.
Using ggtitle, xlab, ylab you can plot the text and center it with theme.
If this does not help you have the wrong print / render settings.
balloon <- data.table(structure(list(Genera = c("Prevotella", "Treponema", "Fusobacterium","Selenomonas", "Veillonella", "Porphyromonas", "Streptococcus","Leptotrichia", "Aggregatibacter", "Succiniclasticum"), S1 = c(97L,28L, 11L, 40L, 5L, 13L, 10L, 24L, 0L, 16L), S3 = c(5370L, 3760L,5551L, 2087L, 533L, 873L, 1330L, 5877L, 1213L, 44L), S4 = c(7892L,8004L, 11017L, 19712L, 5115L, 2695L, 7451L, 13611L, 301L, 2557L), S5 = c(23L, 79L, 30L, 7L, 0L, 34L, 0L, 2L, 2L, 0L), S6 = c(8310L,3379L, 38058L, 1133L, 2506L, 17811L, 12103L, 403L, 668L, 3L),S2 = c(7379L, 14662L, 10085L, 148L, 1502L, 5222L, 1010L,2463L, 4790L, 28L), S7 = c(6238L, 18977L, 2674L, 2198L, 27L,2999L, 174L, 1197L, 5268L, 5L), S8 = c(20019L, 18674L, 15306L,1472L, 1898L, 9600L, 1683L, 2221L, 3435L, 1109L), S9 = c(153L,12L, 23L, 36L, 15L, 15L, 6L, 41L, 0L, 30L), S10 = c(20103L,29234L, 10857L, 2869L, 4923L, 14206L, 1415L, 4574L, 649L,2160L)), .Names = c("Genera", "S1", "S3", "S4", "S5", "S6","S2", "S7", "S8", "S9", "S10"), class = c("data.table", "data.frame"), row.names = c(NA, -10L)))
library(ggplot2)
library(reshape2)
library(data.table)
balloon<-fread("Downloads/balloon.csv")
balloon
balloon_melted<-melt(balloon)
head(balloon_melted)
p <- ggplot(balloon_melted, aes(x =variable, y = Genera))
p+
geom_point( aes(size=value))+
theme(panel.background=element_blank(),
panel.border = element_rect(colour = "blue", fill=NA, size=1)) +
ggtitle("Pretty title") +
xlab("x lab label") +
ylab("y lab label") +
theme(plot.title = element_text(hjust = 0.5))

Legend with discrete classes in ggplot

I have a dataframe df
df<-structure(list(X = c(540.857881018969, 179.756453922596, 375.597673455575,
-24.7802579900034, 435.072227193852, 316.91305374488, 385.910199259729,
118.282069554042, 47.0931945371692, 46.8541543075746, -1.34917072989697,
-7.85914134272889, -74.1514974421857, -14.1349095526425, -180.103770130757,
-138.258618377921, 189.011576650288, 69.2480232224876, 42.356103377609,
-330.116265944969, 12.2073132306264, 82.0963304851313, -76.2883430762099,
13.7563014075505, 286.364096895997, 238.289414874803, 192.304857815893,
196.85181686938, 545.585335607164, 380.551416666209, 285.533878238757,
349.173312050698, 234.149820388793, 182.680602233473, 123.508718623119,
-59.3666813922188, 309.812271637758, 378.745487723212, 383.286192402579,
407.250999438653, 968.521681377846, 168.221917301957, 308.817578621267,
187.43921153459, 219.229124870899, 273.756349813256, 330.797398870288,
310.938292903419, 285.256002320225, 404.534158133551, 298.161606939762,
96.4609965966811, 237.259654760246, 410.903483047603, 453.107209854966,
476.020674679612, 390.865258755938, -148.590524527974, 109.883635942258,
78.869912902272, 782.478670456904, 245.257050602357, 308.573221705999,
568.136911803784, 163.617826469925, 210.109579924637, 175.657357919781,
175.760684810558, -254.181869342232, 288.452497381572), Y = c(436.783385497984,
55.1825021383702, 526.4133417369, 560, 391.49284084118, -519.814235572849,
11.5525291214872, 162.441016515717, 39.0395567645998, -70.4910326673707,
17.1155716306239, -106.326129257097, -94.9308303585276, -66.4285516217351,
-144.929052323413, -220.613145695315, 157.129576861289, 44.1257786633602,
46.8326830295943, -146.719591499443, 30.8043649939355, -4.10548956954153,
-108.258462657337, 90.3369144331664, 126.866108251153, 42.9489971246803,
690.903947139159, -45.4886732113082, 483.932040393885, 618.930183215125,
590.754048774834, 82.1480000555981, 76.8863707484328, 404.007940533033,
202.629066249886, -46.9675149230141, 557.939170770813, 333.76992898155,
300.979565786038, 224.256197650044, 148.719307398695, 201.195892312115,
466.727302447427, 552.762670615377, 595.145436977735, 481.359543363331,
467.379381521489, 279.980726677847, 545.324660883673, 444.812610935212,
308.198167469197, -638.973101716489, 292, 331.193419647713, 181.896345832773,
629.214319321327, -176.181996958815, 214, 59.1716887350485, -77.1223124726675,
-186.42650026083, 279.123776521767, 515.533437888983, 595.091753601562,
367.15020653978, 713.607404187601, 268.681242669467, 239.81099676255,
91.8453621773515, 246.686055020047), A = c(98.5, 77, 63.2222222222222,
97, 52.5, 3.5, 15.5, 71, 161.833333333333, 153.5, 73, 39, 40,
23, 14, 5.5, 78, 129.5, 73.5, 4, 100, 10, 3, 30, 65.5, 198, 26,
45.5, 20, 42.5, 111.5, 44, 68.5, 102.5, 39.1111111111111, 83.8,
136, 28.6666666666667, 31.5, 56.5, 101, 39.25, 108.5, 52.1666666666667,
54.5, 9.5, 13, 8.5, 8, 52.1428571428571, 66.5, 1, 42, 45, 106,
19, 202.571428571429, 200, 36.6, 83.75, 2, 33, 21.2, 69, 67.5,
14, 83, 16, 4, 99)), .Names = c("X", "Y", "A"), row.names = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,
42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L,
55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L,
68L, 69L, 71L), class = "data.frame", na.action = structure(c(70L,
72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L,
85L, 86L, 87L, 88L, 89L, 90L), .Names = c("70", "72", "73", "74",
"75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85",
"86", "87", "88", "89", "90"), class = "omit"))
I am plotting Y against X with ggplot in a simple scatterplot. However I also want to add a color scheme and a legend based on the variable A. The idea is to have four classes (e.g. 0-50, 50-100, 100-150 and 150-200) in the legend.
I only managed to get a gradient legend so far as such:
library(ggplot2)
ggplot(df, aes(x=X, y=Y, colour=A))+
geom_point(shape=3)+
geom_abline(slope=1)+
theme_bw(base_size = 14, base_family = "Helvetica") +
theme(panel.grid = element_line(colour="grey", size=0.5),
axis.text.x = element_text(hjust = 1),
legend.position="bottom",
legend.box="horizontal",
legend.key = element_blank(),
legend.text=element_text(size=12))+
xlab("Predicted")+
ylab("Observed")+
scale_colour_gradient(name= "Stand age", low= "#67a9cf", high ="#ef8a62")+
guides(colour = guide_colourbar(title.position="top", title.hjust = 0.5),
size = guide_legend(title.position="top", title.hjust = 0.5))
In addition, I would like to have the legend box in the lower right corner inside the panel. Anyone knows how to do it?
library(RColorBrewer)
library (ggplot)
vecP <- c(0, 50, 100, 150, 200, 250)
labP <- c("0-50", "50-100", "100-150", "150-200", "200-250")
df$Age <- cut(df$A, breaks=vecP, labels =labP)
colorsP <- brewer.pal(length(labP), "RdBu")
ggplot(df, aes(x=X, y=Y, colour=Age))+
geom_point(shape=1)+
geom_abline(slope=1)+
theme_bw(base_size = 14, base_family = "Helvetica") +
theme(panel.grid = element_line(colour="grey", size=0.5),
axis.text.x = element_text(hjust = 1),
legend.position=c(1, 0),
legend.direction = "vertical",
legend.justification = c(1,0),
legend.key = element_blank())+
xlab("Predicted")+
ylab("Observed")+
scale_fill_manual(name = "Stand age", values = setNames(colorsP, labP),breaks=rev(labP),labels=rev(labP))
library(ggplot2)
plt <- ggplot(df, aes(x=X, y=Y, colour=A))+
geom_point(shape=3)+
geom_abline(slope=1)+
theme_bw(base_size = 14, base_family = "Helvetica") +
theme(panel.grid = element_line(colour="grey", size=0.5),
axis.text.x = element_text(hjust = 1),
legend.position=c(1, 0),
legend.direction = "horizontal",
legend.justification = c(1,0))+
xlab("Predicted")+
ylab("Observed")+
scale_colour_gradient(name= "Stand age", low= "#67a9cf", high ="#ef8a62")+
guides(colour = guide_colourbar(title.position="top", title.hjust = 0.5),
size = guide_legend(title.position="top", title.hjust = 0.5))
Just changed,
legend.position = c(1,0)
legend.direction = "horizontal"
legend.justification = c(1,0)

Resources