Question

I'm trying to plot a simple graph that show the increase in wealth for two different investment strategies. When using the standard graph from R it works, but when I try to use ggplot2 I get these weird spikes in the lines.

Does any have any idea what could be causing this?

I've tried to simply the code as much as possible:

For the standard graph

 ind.ts = ts(cbind(ind.passive,ind.active), start=c(insample.endstart,1),frequency=12)
plot(log(ind.ts),type="lines", col=c("blue","red"))
legend(x="topleft", legend=c("Passive","Active"), col=c("blue","red"), lty=1)

For the ggplot graph

  testers=data.frame(ind.ts)
ggplot(testers, aes(date)) + 
  geom_line(aes(y = log(ind.passive), colour = "Passive",size="1")) + 
  geom_line(aes(y = log(ind.active), colour = "Active",size="1"))

The Ind.ts data set

structure(c(1, 1.026669, 1.066102329621, 1.09764083483818, 1.13073909657189, 
1.17422279926966, 1.201650295415, 1.24229131005623, 1.24436842112664, 
1.29675757602449, 1.29281154272065, 1.34840890311535, 1.37447769243928, 
1.42187380670767, 1.43432089001159, 1.44828830683852, 1.47037760009442, 
1.50663270057995, 1.51269991046518, 1.44617893190248, 1.47609892782461, 
1.55880475075062, 1.60230787373457, 1.72267003659376, 1.6884336922865, 
1.7947931958647, 1.80827747714523, 1.73407842742553, 1.83823238001199, 
1.94879470474019, 2.03637158997651, 2.19836698633073, 2.07500122615881, 
2.18823196806907, 2.11573803119891, 2.21303659177769, 2.25083083069207, 
2.27667036862841, 2.44006700098487, 2.56495939036328, 2.59127330874902, 
2.54554769994283, 2.64902166839781, 2.62135793511473, 2.24229384954953, 
2.38534322797539, 2.58003017155629, 2.73574015247005, 2.89313822640227, 
3.01496249083961, 2.92082933195062, 3.03735873897812, 3.15584610338566, 
3.08028252428619, 3.25121048184135, 3.15027015001163, 3.13383204036887, 
3.04763285626648, 3.24152630621501, 3.30661615444381, 3.5011906754359, 
3.32628169286315, 3.26271977599422, 3.58162126961968, 3.47465973202375, 
3.4018482373392, 3.48660188432426, 3.43296051433394, 3.64465402445034, 
3.45302176049876, 3.43920276741325, 3.16710336206381, 3.18321124976327, 
3.29673729577483, 2.9957319937214, 2.80662641161774, 3.02543381329387, 
3.04403720581181, 2.97111425050939, 2.94227958670819, 2.75683358891715, 
2.53472102032527, 2.58379068455775, 2.78122846592754, 2.80549468429276, 
2.76500859050373, 2.71079783207832, 2.81360212906206, 2.64401226073284, 
2.62324090041252, 2.43641368348514, 2.24723834303094, 2.26148583412576, 
2.01595857860056, 2.19346574740491, 2.32192606890168, 2.18514140418268, 
2.12856372294559, 2.09571359900937, 2.1165869064555, 2.29149953181808, 
2.41150994529845, 2.44221328992199, 2.48518647497146, 2.53301388868229, 
2.50620193667058, 2.64742390960003, 2.6698343529948, 2.80897010046677, 
2.86115795596334, 2.89979789415863, 2.85611823847891, 2.81197121886675, 
2.84980347964538, 2.90496997540435, 2.80930350417434, 2.81972040156782, 
2.85016210302314, 2.89418855702854, 3.00999951213804, 3.11183381563269, 
3.03729294841303, 3.09892873421517, 3.04396923311387, 2.98710484387007, 
3.08097760069353, 3.08499827646243, 3.20047593194697, 3.16912086924169, 
3.19575099190593, 3.14371138275373, 3.25904157854143, 3.26071346687123, 
3.3485896948034, 3.35499219829987, 3.3971510302637, 3.44342702159796, 
3.34200432210381, 3.3473849490624, 3.36955802696499, 3.4464479715823, 
3.53637269205683, 3.65311189099431, 3.71864871831875, 3.7710110109214, 
3.82954087282191, 3.75144504580245, 3.79450413203817, 3.96444479409563, 
4.09921609487092, 4.03197255405065, 3.90887240000293, 3.96507025849778, 
4.11298323942078, 4.18000430130714, 4.00202389816178, 3.973681564915, 
3.73688988046171, 3.6132997214452, 3.59812747591486, 3.77562310430174, 
3.82238042082541, 3.50029900180582, 3.47233161278139, 3.52122551422096, 
3.20811814149644, 2.67119786498117, 2.47785656351383, 2.50381211101664, 
2.29590056094204, 2.04999813136234, 2.23149881591877, 2.44744541933286, 
2.58359925545577, 2.59022877114527, 2.78828284344458, 2.88774646903593, 
2.99667515359443, 2.94310059519847, 3.1174675330616, 3.17829867703423, 
3.06610473373492, 3.15882374088307, 3.34981254190434, 3.40448483240076, 
3.13064849939144, 2.96722864772321, 3.17659630110655, 3.0311907820197, 
3.30193068028814, 3.42901538831107, 3.42659107443153, 3.65581631094671, 
3.74411158648869, 1, 1.026669, 1.066102329621, 1.09764083483818, 
1.13073909657189, 1.17422279926966, 1.201650295415, 1.24229131005623, 
1.24436842112664, 1.29675757602449, 1.29281154272065, 1.34840890311535, 
1.37447769243928, 1.42187380670767, 1.43432089001159, 1.44828830683852, 
1.47037760009442, 1.50663270057995, 1.51269991046518, 1.44617893190248, 
1.47609892782461, 1.55880475075062, 1.60230787373457, 1.72267003659376, 
1.6884336922865, 1.7947931958647, 1.80827747714523, 1.73407842742553, 
1.83823238001199, 1.94879470474019, 2.03637158997651, 2.19836698633073, 
2.07500122615881, 2.18823196806907, 2.11573803119891, 2.21303659177769, 
2.25083083069207, 2.27667036862841, 2.44006700098487, 2.56495939036328, 
2.59127330874902, 2.54554769994283, 2.64902166839781, 2.62135793511473, 
2.24229384954953, 2.2509042579318, 2.25833224198298, 2.39462710945113, 
2.53239958556629, 2.63903386731532, 2.55663795191, 2.6586375796394, 
2.76235103162114, 2.69620929852, 2.84582464870417, 2.75747033083585, 
2.74308185064955, 2.66763064126559, 2.83734797029354, 2.89432191753704, 
3.06463539645259, 2.91153540595201, 2.85589887587967, 3.13503728790702, 
3.04141253434097, 2.97767973468385, 3.05186564759377, 3.00491269460554, 
3.19021063591839, 3.02247255089243, 3.01037661574376, 3.02584995154869, 
3.04040428981563, 3.05344762421894, 3.06587515604951, 3.07715757662378, 
3.08709679559627, 3.09641982791897, 3.10543040961822, 3.1145293207184, 
3.12325000281641, 3.13012115282261, 3.13575537089769, 3.14064714927629, 
3.14507546175677, 3.14941566589399, 3.15395082445288, 3.15865021118131, 
2.96826256970236, 2.97253686780273, 2.97675787015501, 2.98092533117323, 
2.98494958037031, 2.98900911179961, 2.99295460382719, 2.99603734706913, 
2.99900342404273, 3.00194244739829, 3.00488435099674, 3.00770894228668, 
3.01053618869243, 3.16820398996663, 3.20854156316688, 3.26499906051237, 
3.32783396743193, 3.29260884488666, 3.47814406068718, 3.5075865501609, 
3.69038091563598, 3.75894450266758, 3.80970904817611, 3.75232340078343, 
3.69432373797752, 3.74402716954827, 3.81650404749639, 3.69081893620424, 
3.70450449281968, 3.74449832332416, 3.80233958892455, 3.95449020757537, 
4.08827852027806, 3.99034789660332, 4.07132402646909, 3.99911909485966, 
3.92441155104859, 4.04774010845184, 4.05302240929337, 4.20473514411804, 
4.16354135391111, 4.19852759190803, 4.1301587686014, 4.28167777318631, 
4.28387427388395, 4.39932468556512, 4.40773619436392, 4.4631238073823, 
4.52392047988646, 4.39067292607189, 4.39774190948286, 4.42687255189128, 
4.52788935665288, 4.64603104574667, 4.79940117659781, 4.88550243370598, 
4.95429519347499, 5.03119080917292, 4.92858973500145, 4.9851600879798, 
5.20842546768007, 5.38548589145385, 5.29714238089044, 5.13541532685947, 
5.20924719301373, 5.40357295030192, 5.49162417152709, 5.25779630592764, 
5.22056059248906, 4.90946738678263, 4.91815714405724, 4.9233212090585, 
4.92863839596428, 4.93573563525447, 4.94338602548911, 4.95010903048378, 
4.95718768639737, 4.96184744282258, 4.96462607739057, 4.96542041756295, 
4.96556938017547, 4.96611559280729, 4.9673571217055, 4.9682512459874, 
4.96889711864938, 4.96964245321718, 4.97038789958516, 4.9711334577701, 
5.14846373047568, 5.34266893085295, 5.24715269570716, 5.55802550431702, 
5.66647925598276, 5.46645253824657, 5.63175806300315, 5.97226541900844, 
6.06973876291208, 5.58152539525601, 5.29016976962365, 5.2908574916937, 
5.04867378086891, 5.04933010846042, 5.24366872567485, 5.2399614518858, 
5.59049391317115, 5.72551552216206), .Dim = c(194L, 2L), .Dimnames = list(
NULL, c("ind.passive", "ind.active")), .Tsp = c(1995, 2011.08333333333, 
12), class = c("mts", "ts", "matrix"))

The date data set

structure(c(1995.1, 1995.2, 1995.3, 1995.4, 1995.5, 1995.6, 1995.7, 
1995.8, 1995.9, 1995.1, 1995.11, 1995.12, 1996.1, 1996.2, 1996.3, 
1996.4, 1996.5, 1996.6, 1996.7, 1996.8, 1996.9, 1996.1, 1996.11, 
1996.12, 1997.1, 1997.2, 1997.3, 1997.4, 1997.5, 1997.6, 1997.7, 
1997.8, 1997.9, 1997.1, 1997.11, 1997.12, 1998.1, 1998.2, 1998.3, 
1998.4, 1998.5, 1998.6, 1998.7, 1998.8, 1998.9, 1998.1, 1998.11, 
1998.12, 1999.1, 1999.2, 1999.3, 1999.4, 1999.5, 1999.6, 1999.7, 
1999.8, 1999.9, 1999.1, 1999.11, 1999.12, 2000.1, 2000.2, 2000.3, 
2000.4, 2000.5, 2000.6, 2000.7, 2000.8, 2000.9, 2000.1, 2000.11, 
2000.12, 2001.1, 2001.2, 2001.3, 2001.4, 2001.5, 2001.6, 2001.7, 
2001.8, 2001.9, 2001.1, 2001.11, 2001.12, 2002.1, 2002.2, 2002.3, 
2002.4, 2002.5, 2002.6, 2002.7, 2002.8, 2002.9, 2002.1, 2002.11, 
2002.12, 2003.1, 2003.2, 2003.3, 2003.4, 2003.5, 2003.6, 2003.7, 
2003.8, 2003.9, 2003.1, 2003.11, 2003.12, 2004.1, 2004.2, 2004.3, 
2004.4, 2004.5, 2004.6, 2004.7, 2004.8, 2004.9, 2004.1, 2004.11, 
2004.12, 2005.1, 2005.2, 2005.3, 2005.4, 2005.5, 2005.6, 2005.7, 
2005.8, 2005.9, 2005.1, 2005.11, 2005.12, 2006.1, 2006.2, 2006.3, 
2006.4, 2006.5, 2006.6, 2006.7, 2006.8, 2006.9, 2006.1, 2006.11, 
2006.12, 2007.1, 2007.2, 2007.3, 2007.4, 2007.5, 2007.6, 2007.7, 
2007.8, 2007.9, 2007.1, 2007.11, 2007.12, 2008.1, 2008.2, 2008.3, 
2008.4, 2008.5, 2008.6, 2008.7, 2008.8, 2008.9, 2008.1, 2008.11, 
2008.12, 2009.1, 2009.2, 2009.3, 2009.4, 2009.5, 2009.6, 2009.7, 
2009.8, 2009.9, 2009.1, 2009.11, 2009.12, 2010.1, 2010.2, 2010.3, 
2010.4, 2010.5, 2010.6, 2010.7, 2010.8, 2010.9, 2010.1, 2010.11, 
2010.12, 2011.1, 2011.2), .Tsp = c(1995, 2011.08333333333, 12
), class = "ts")
Was it helpful?

Solution

The spikes are in your data, specifically in the crummy way the dates are stored. January, February, March 1995 are coded as 1995.10, 1995.20, 1995.30, but then October, November, and December are 1995.10, 1995.11, 1995.12. When you pass your time series to ggplot you maybe saw a warning like:

Don't know how to automatically pick scale for object of type ts. Defaulting to continuous

So ggplot just converted to numerics, giving October the same x value as January and inserting Nov and Dec before February, causing your spikes. Since your samples (as far as I checked) are spaced every month, you could add a new column to your data like this:

ind.df <- as.data.frame(ind.ts)
ind.df$date <- seq(as.Date('1995-01-01'), as.Date('2011-02-01'), by = "month")

Then, ggplot works best with long-format data, so we can melt it

library(reshape2)
ind.melt <- melt(ind.df, id.vars = "date")
ggplot(ind.melt, aes(x = date, y = value, color = variable) +
    geom_line(size = 1)

enter image description here

And the spikes are gone.

One other note, in ggplot don't put anything inside aes() that isn't mapping to a data column. In your post, inside aes() you have size = "1". You don't need the quotes around 1, and since it applies to the whole layer you should put it outside of aes().

OTHER TIPS

The following example illustrates that for a very simple example, the plots from the basic R plotting and ggplot2 are the same, i.e. basic plotting does not get rid of the spikes, nor does ggplot2 introduces spikes. You need to make your example more complete, i.e. provide us with a sample of your data that reproduces the issue you see.

x = 1:100
y = runif(100)
y[50] = 5

plot(x, y)

enter image description here

library(ggplot2)
qplot(x, y, geom = 'line')

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top