Rollo99
Rollo99

Reputation: 1613

why does changing x-axis not work in plot?

I would simply like to change the x-axis in plot. However, what I usually do doesn't seem to work.

The dataset:


data1 = c(-1.68038051095608, -2.12317945962401, -2.71086209338424, -3.22489682411764, 
-2.51769032592554, -1.33242532610804, -2.13564807610995, -2.03415137348661, 
-1.58909921518124, -0.68844714029518, -1.94691881575563, -1.16714425518695, 
-2.15153420569546, -2.04779261960842, -0.867515299774483, -1.47986823637587, 
-0.650513604798111, -1.61361732632524, -1.48596960028163, -2.20004804407501, 
-2.64689217553021, -2.67436545120372, -3.48049123019991, -2.28510809912552, 
-2.32172665536549, -1.76823348887895, -2.763353378483, -2.09381469041352, 
-2.08400217235893, -1.59720187270177, -2.06034560841579, -2.56317571167687, 
-1.32635640217861, -1.48729782102413, -2.00732693090646, -3.40272319833461, 
-2.49810610074565, -3.32810591309226, -1.92402348117091, -1.35397391665409, 
-1.76640461987233, -2.51735528772741, -2.35332514240503, -2.35272634525907, 
-2.68468172493552, -2.92171051216825, -3.16925509035157, -2.23093489115309, 
-3.06530983495044, -2.30445613039677, -3.30583207178147, -2.82188405397887, 
-2.51962207875813, -2.66982767393931, -2.98041595989062, -2.55306093500464, 
-3.36871970472208, -2.6201699311654, -2.49494986723432, -2.78910296607766, 
-2.57178346785915, -3.28445145020619, -2.42213276484722, -2.38709926337358, 
-2.99568739417641, -2.65138790450656, -1.58074660422518, -1.91906056315361, 
-2.90337379582732, -2.20117735700684, -1.29303194740331, -2.2494711668963, 
-1.53224235243069, -2.04481468859739, -0.929377541149286, -0.0656900034556943, 
-2.20508088335906, -0.578502434372448, -0.858925093713048, -2.96970181427872, 
-2.97862851965814, -3.31779605911778, -2.08207766441392, -2.4848907725881, 
-1.99650762208841, -2.17076028347941, -1.27061305909, -1.92537835567221, 
-1.49409886851971, -2.32667673830125, -1.90852299188928, -2.88390697795999, 
-2.9172411130509, -3.5788915130035, -2.92151370364156, -2.71478221054495, 
-3.40505963822921, -3.21129210245385, -2.66481268506047, -3.27494238949828, 
-3.37230078139583, -3.53346393522174, -3.99626066800013, -3.41713548837371, 
-3.67409843863548, -3.50182175058264, -3.37467436298585, -3.40580625489191, 
-2.99003362011982, -3.66653724632765, -3.61588309823573, -3.18167532125541, 
-3.35619362672467, -2.8806023910338, -2.23185392156432, -0.626338062660262, 
-2.00482507082523, -1.7359344838577, -0.800609837957656, -1.93997314433068, 
-0.93482911672218, -2.63652739226028, -1.93546315176661, -1.17524044037369, 
-2.274143231604, -2.69679235052359, -2.23731851002543, -1.86716852238077, 
-0.716926538642468, -1.30258784685856, -1.08194224250233, -0.930472602419788, 
1.13081617308243, 2.57874064965174, -0.388461792958877, -0.340421132850094, 
0.638071432169484, -1.57318833539501, 4.40961161388978, 1.62060735764472, 
-0.674369921428344, 1.76679629890753, -0.748857461264583, 0.520407646064921, 
-0.575555044309392, 4.34511452569889, 3.34194433067617, 3.21304624521961, 
1.97147139745885, 3.45052500620869, -0.399783726365629, -0.519126753266423, 
-0.924795366280377, 4.49023560078066, 2.40210157814194, 3.48634734098189, 
2.72330542145941, 2.44175433903678, 1.83754103018167, 2.00744190890702, 
2.84495436947268, 3.68678762972081, 3.16657087164265, 2.54382298188246, 
3.23525009614645, 2.25680930181096, 1.44116917115332, -0.510916286085479, 
-0.212859586548733, -0.740774827758169, -0.740774827758169, -0.740774827758169, 
-0.41701391913469, -1.22601225946438, -0.632288989161026, -1.04309509051099, 
-1.17291896442253, -1.17291896442253, -1.17291896442253, -1.11302828507398, 
-2.78220449305609, -2.86224137302201, -2.86224137302201, 1.34122649704384, 
1.33515381941954, 2.56866171677324, 0.963485086246604, 4.22653525972732, 
5.35095831479335, 3.93448842430935)

data2 = c(-3.43328245753891, -2.56261291255484, -3.43328245753891, -2.38847900355802, 
-1.82254379931838, -3.78155027553254, -4.21688504802457, -2.0402111855644, 
-1.12600816333112, -4.21688504802457, -1.21307511782953, -2.78028029880086, 
-4.21688504802457, -3.69448332103413, -2.0402111855644, -1.77901032206917, 
-2.30141204905962, -2.64967986705325, -1.43074250407555, -2.91088073054847, 
-3.04148116229608, -3.2591485485421, -3.47681593478811, -2.34494552630882, 
-2.30141204905962, -4.21688504802457, -2.95441420779767, -3.2591485485421, 
-3.04148116229608, -2.38847900355802, -2.78028029880086, -3.47681593478811, 
-2.56261291255484, -3.2591485485421, -3.78155027553254, -2.91088073054847, 
-3.69448332103413, -2.78028029880086, -3.12854811679448, -2.17081161731201, 
-2.21434509456121, -3.73801679828333, -3.3462155030405, -2.95441420779767, 
-3.69448332103413, -3.43328245753891, -2.64967986705325, -3.12854811679448, 
-1.69194336757077, -2.95441420779767, -3.78155027553254, -3.54211615066192, 
-4.21688504802457, -3.56388288928652, -3.60741636653572, -2.64967986705325, 
-4.21688504802457, -2.78028029880086, -3.82508375278174, -1.60487641307236, 
-3.91215070728015, -2.25787857181041, -2.64967986705325, -2.0402111855644, 
-3.2591485485421, -2.56261291255484, -2.95441420779767, -3.2591485485421, 
-3.43328245753891, -2.64967986705325, -2.78028029880086, -4.21688504802457, 
-2.47554595805643, -3.3462155030405, -2.21434509456121, -3.47681593478811, 
-2.0402111855644, -1.77901032206917, -2.64967986705325, -3.65094984378493, 
-2.86734725329926, -3.2591485485421, -2.38847900355802, -3.69448332103413, 
-3.47681593478811, -3.56388288928652, -4.21688504802457, -3.12854811679448, 
-2.86734725329926, -3.43328245753891, -2.30141204905962, -2.56261291255484, 
-2.30141204905962, -3.43328245753891, -3.78155027553254, -3.3462155030405, 
-4.21688504802457, -2.78028029880086, -2.78028029880086, -3.60741636653572, 
-1.51780945857395, -2.47554595805643, -4.21688504802457, -4.21688504802457, 
-2.64967986705325, -2.86734725329926, -2.91088073054847, -3.60741636653572, 
-4.21688504802457, -0.821273822586697, -2.30141204905962, -2.86734725329926, 
-3.3026820257913, -2.86734725329926, -3.86861723003094, -1.03894120883271, 
-3.3462155030405, -2.0402111855644, -2.25787857181041, -3.2591485485421, 
-2.64967986705325, -1.60487641307236, -2.78028029880086, -2.21434509456121, 
-4.21688504802457, -3.04148116229608, -2.56261291255484, -2.91088073054847, 
-2.86734725329926, -3.60741636653572, -3.78155027553254, -3.3026820257913, 
0.48473049488941, 0.920065267381446, -2.25787857181041, -3.82508375278174, 
0.136462676895782, -2.34494552630882, 4.0109421520749, -1.16954164058032, 
-3.04148116229608, 0.136462676895782, -2.0402111855644, -2.30141204905962, 
-2.47554595805643, -0.385939050094661, -0.255338618347051, 1.5730674261195, 
-1.73547684481997, 3.53207390233366, -0.951874254334307, -2.0402111855644, 
-0.385939050094661, 0.136462676895782, 2.66140435734959, 1.87780176686392, 
2.18253610760835, 2.31313653935596, 1.22479960812587, 1.79073481236552, 
2.53080392560198, 3.31440651608764, 3.05320565259242, 3.31440651608764, 
0.876531790132242, 4.0544756293241, 3.14027260709083, -0.211805141097847, 
-0.603606436340679, 0.136462676895782, 1.5730674261195, -0.603606436340679, 
-0.73420686808829, -3.3462155030405, -0.951874254334307, -1.03894120883271, 
-1.69194336757077, -2.56261291255484, -1.82254379931838, -1.30014207232794, 
-2.86734725329926, -1.86607727656758, -2.86734725329926, 2.74847131184799, 
1.31186656262428, 2.31313653935596, 0.528263972138614, 1.5730674261195, 
3.31440651608764, 3.18380608434003)

The code:

plot(data1,  ylim=c(-5,6), type = "l", lwd = 2, col = "blue", ylab = "", xlab= "", main = "", xaxs="n") 
lines(data2, type = "l", col = "red", lwd= 2)
axis(1, at=seq(2002, 2020,1), labels = seq(2002, 2020,1))
abline(h=0, col = "black", lty = 2)
legend("topleft", legend=c("X1", "X2"), col=c("blue", "red"), lty=1, cex=0.8)

By doing so, nothing happens. I can't convert the series into ts objects as there frequency is not always regular. I don't understand why it doesn't work. I also tried to build a unique dataframes with dates in a third column but it doesn't work either.

Can anyone help me?

Thanks

Upvotes: 2

Views: 299

Answers (1)

Ian Campbell
Ian Campbell

Reputation: 24790

Here's an approach with xaxt = 'n':

The other problem you have is you are trying to add x-axis labels at points that are outside of the plot range. Since you don't provide x values in your call to plot, they just get integer indices. So the x-axis range is 1:190. Your at = seq(2002, 2020,1) is out of range.

plot(data1,  ylim=c(-5,6), type = "l", lwd = 2, col = "blue", ylab = "",
     xlab= "", main = "", xaxt="n") 
lines(data2, type = "l", col = "red", lwd= 2)
axis(1, at=seq(1, length(data1),10), labels = seq(2002, 2020,1))
abline(h=0, col = "black", lty = 2)
legend("topleft", legend=c("X1", "X2"), col=c("blue", "red"), lty=1, cex=0.8)

enter image description here

Upvotes: 2

Related Questions