Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trouble adding geom_vline to ggplot2

Tags:

r

ggplot2

I have a large time series data set, which I've used xts to summarize in 30 second periods. Not sure how to make this set easily reproducible but it looks like this

> str(taonedf)
'data.frame':   480 obs. of  2 variables:
 $ time : POSIXct, format: "2013-01-06 13:00:29" "2013-01-06 13:00:59" "2013-01-06 13:01:29" ...
 $ count: int  20763 12030 22188 12183 21112 11628 21543 12609 20095 12992 ...
> head(taonedf)
                 time count
1 2013-01-06 13:00:29 20763
2 2013-01-06 13:00:59 12030
3 2013-01-06 13:01:29 22188
4 2013-01-06 13:01:59 12183
5 2013-01-06 13:02:29 21112
6 2013-01-06 13:02:59 11628

I've plotted a normal line plot of this and it works fine.

ggplot(data=taonedf, aes(x=time, y=count/30)) + #
  geom_line(color="#009E73") + 
  scale_y_continuous(name="requests per second", labels = format_format(scientific=FALSE, big.mark=",")) +
  scale_x_datetime(name="",labels = date_format("%b %d\n%H:%M") ) +
  labs(title=paste("Requests per Second - All Requests",count,sep="\n")) +
  theme(legend.position = "none")

enter image description here

I want to add some vline annotations. I've created a second dataframe called EV, it looks like this:

> str(ev)
'data.frame':   10 obs. of  2 variables:
 $ dt   : POSIXct, format: "2013-01-06 13:45:00" "2013-01-06 14:18:00" "2013-01-06 14:49:00" ...
 $ event: Factor w/ 9 levels "Event 1",..: 7 8 3 2 5 6 1 4 2 9

> head(ev)
                   dt                                            event
1 2013-01-06 13:45:00                                          Event 1

Now, when I add the vline option I get odd results. I'm using the same date time format between the two so the scale should align.

ggplot(data=taonedf, aes(x=time, y=count/30)) + 
  geom_line(color="#009E73") + 
  geom_vline(data=ev,aes(xtintercept=dt))+
  scale_y_continuous(name="requests per second", labels = format_format(scientific=FALSE, big.mark=",")) +
  scale_x_datetime(name="",labels = date_format("%b %d\n%H:%M") ) +
  labs(title=paste("Requests per Second - All Requests",count,sep="\n")) +
  theme(legend.position = "none")

enter image description here

What am I missing? This doesn't appear to be that hard. All of the documentation and examples show simple numeric X axis so I'm assuming there is some issue with dates in the X axis but I can't pinpoint it. Any help would be appreciated.

> dput(taonedf)
structure(list(time = structure(c(1357506029.996, 1357506059.999, 
1357506089.997, 1357506119.998, 1357506149.998, 1357506179.996, 
1357506209.996, 1357506239.993, 1357506269.999, 1357506299.996, 
1357506329.998, 1357506359.998, 1357506389.999, 1357506419.998, 
1357506449.986, 1357506479.996, 1357506509.99, 1357506539.988, 
1357506569.996, 1357506599.999, 1357506629.991, 1357506659.998, 
1357506689.999, 1357506719.995, 1357506749.996, 1357506779.998, 
1357506809.998, 1357506839.997, 1357506869.996, 1357506899.996, 
1357506929.997, 1357506959.994, 1357506989.998, 1357507019.999, 
1357507049.999, 1357507079.998, 1357507109.998, 1357507139.999, 
1357507169.998, 1357507199.99, 1357507229.999, 1357507259.999, 
1357507289.999, 1357507319.998, 1357507349.997, 1357507379.997, 
1357507409.999, 1357507439.998, 1357507469.994, 1357507499.996, 
1357507529.996, 1357507559.996, 1357507589.995, 1357507619.988, 
1357507649.999, 1357507679.994, 1357507709.996, 1357507739.996, 
1357507769.994, 1357507799.991, 1357507829.999, 1357507859.999, 
1357507889.999, 1357507919.999, 1357507949.999, 1357507979.999, 
1357508009.999, 1357508039.999, 1357508069.998, 1357508099.999, 
1357508129.999, 1357508159.999, 1357508189.999, 1357508219.998, 
1357508249.999, 1357508279.999, 1357508309.999, 1357508339.999, 
1357508369.999, 1357508399.999, 1357508429.998, 1357508459.999, 
1357508489.999, 1357508519.999, 1357508549.999, 1357508579.999, 
1357508609.999, 1357508639.999, 1357508669.999, 1357508699.999, 
1357508729.999, 1357508759.998, 1357508789.999, 1357508819.998, 
1357508849.999, 1357508879.998, 1357508909.999, 1357508939.996, 
1357508969.999, 1357508999.999, 1357509029.999, 1357509059.999, 
1357509089.999, 1357509119.999, 1357509149.999, 1357509179.999, 
1357509209.999, 1357509239.999, 1357509269.999, 1357509299.999, 
1357509329.999, 1357509359.999, 1357509389.999, 1357509419.999, 
1357509449.999, 1357509479.999, 1357509509.999, 1357509539.999, 
1357509569.976, 1357509599.999, 1357509629.999, 1357509659.999, 
1357509689.999, 1357509719.999, 1357509749.996, 1357509779.999, 
1357509809.999, 1357509839.999, 1357509869.999, 1357509899.999, 
1357509929.999, 1357509959.996, 1357509989.999, 1357510019.997, 
1357510049.998, 1357510079.997, 1357510109.999, 1357510139.999, 
1357510169.999, 1357510199.999, 1357510229.999, 1357510259.999, 
1357510289.999, 1357510319.999, 1357510349.999, 1357510379.999, 
1357510409.999, 1357510439.999, 1357510469.999, 1357510499.999, 
1357510529.999, 1357510559.999, 1357510589.999, 1357510619.999, 
1357510649.999, 1357510679.999, 1357510709.999, 1357510739.983, 
1357510769.999, 1357510799.999, 1357510829.999, 1357510859.999, 
1357510889.999, 1357510919.999, 1357510949.999, 1357510979.999, 
1357511009.997, 1357511039.999, 1357511069.999, 1357511099.999, 
1357511129.999, 1357511159.999, 1357511189.999, 1357511219.999, 
1357511249.999, 1357511279.999, 1357511309.999, 1357511339.999, 
1357511369.999, 1357511399.999, 1357511429.999, 1357511459.999, 
1357511489.999, 1357511519.999, 1357511549.999, 1357511579.999, 
1357511609.999, 1357511639.999, 1357511669.999, 1357511699.999, 
1357511729.999, 1357511759.999, 1357511789.996, 1357511819.999, 
1357511849.999, 1357511879.999, 1357511909.999, 1357511939.993, 
1357511969.999, 1357511999.998, 1357512029.999, 1357512059.999, 
1357512089.999, 1357512119.999, 1357512149.999, 1357512179.998, 
1357512209.999, 1357512239.999, 1357512269.999, 1357512299.999, 
1357512329.997, 1357512359.993, 1357512389.997, 1357512419.999, 
1357512449.999, 1357512479.998, 1357512509.999, 1357512539.999, 
1357512569.999, 1357512599.999, 1357512629.999, 1357512659.995, 
1357512689.999, 1357512719.999, 1357512749.999, 1357512779.995, 
1357512809.999, 1357512839.999, 1357512869.999, 1357512899.999, 
1357512929.999, 1357512959.999, 1357512989.997, 1357513019.996, 
1357513049.999, 1357513079.999, 1357513109.999, 1357513139.999, 
1357513169.999, 1357513199.993, 1357513229.999, 1357513259.999, 
1357513289.999, 1357513319.999, 1357513349.998, 1357513379.999, 
1357513409.999, 1357513439.999, 1357513469.999, 1357513499.999, 
1357513529.999, 1357513559.999, 1357513589.999, 1357513619.999, 
1357513649.999, 1357513679.999, 1357513709.999, 1357513739.999, 
1357513769.999, 1357513799.998, 1357513829.997, 1357513859.999, 
1357513889.999, 1357513919.999, 1357513949.999, 1357513979.998, 
1357514009.999, 1357514039.996, 1357514069.999, 1357514099.999, 
1357514129.999, 1357514159.999, 1357514189.999, 1357514219.999, 
1357514249.999, 1357514279.999, 1357514309.999, 1357514339.993, 
1357514369.999, 1357514399.999, 1357514429.999, 1357514459.999, 
1357514489.999, 1357514519.999, 1357514549.988, 1357514579.997, 
1357514609.999, 1357514639.998, 1357514669.984, 1357514699.999, 
1357514729.999, 1357514759.999, 1357514789.999, 1357514819.999, 
1357514849.999, 1357514879.999, 1357514909.999, 1357514939.996, 
1357514969.999, 1357514999.999, 1357515029.999, 1357515059.998, 
1357515089.999, 1357515119.97, 1357515149.998, 1357515179.999, 
1357515209.999, 1357515239.999, 1357515269.999, 1357515299.999, 
1357515329.999, 1357515359.999, 1357515389.999, 1357515419.999, 
1357515449.999, 1357515479.999, 1357515509.999, 1357515539.999, 
1357515569.999, 1357515599.999, 1357515629.995, 1357515659.999, 
1357515689.999, 1357515719.999, 1357515749.999, 1357515779.999, 
1357515809.995, 1357515839.999, 1357515869.999, 1357515899.999, 
1357515929.999, 1357515959.999, 1357515989.999, 1357516019.999, 
1357516049.999, 1357516079.999, 1357516109.999, 1357516139.999, 
1357516169.999, 1357516199.999, 1357516229.999, 1357516259.998, 
1357516289.998, 1357516319.999, 1357516349.999, 1357516379.999, 
1357516409.999, 1357516439.999, 1357516469.999, 1357516499.999, 
1357516529.999, 1357516559.999, 1357516589.999, 1357516619.999, 
1357516649.999, 1357516679.999, 1357516709.999, 1357516739.999, 
1357516769.999, 1357516799.999, 1357516829.999, 1357516859.999, 
1357516889.999, 1357516919.999, 1357516949.999, 1357516979.999, 
1357517009.999, 1357517039.999, 1357517069.999, 1357517099.999, 
1357517129.999, 1357517159.998, 1357517189.999, 1357517219.999, 
1357517249.999, 1357517279.999, 1357517309.999, 1357517339.999, 
1357517369.999, 1357517399.998, 1357517429.999, 1357517459.999, 
1357517489.999, 1357517519.999, 1357517549.999, 1357517579.999, 
1357517609.999, 1357517639.999, 1357517669.999, 1357517699.999, 
1357517729.999, 1357517759.999, 1357517789.999, 1357517819.999, 
1357517849.999, 1357517879.999, 1357517909.999, 1357517939.999, 
1357517969.999, 1357517999.999, 1357518029.999, 1357518059.976, 
1357518089.999, 1357518119.998, 1357518149.998, 1357518179.999, 
1357518209.987, 1357518239.999, 1357518269.998, 1357518299.991, 
1357518329.998, 1357518359.999, 1357518389.994, 1357518419.994, 
1357518449.995, 1357518479.999, 1357518509.999, 1357518539.998, 
1357518569.983, 1357518599.999, 1357518629.998, 1357518659.994, 
1357518689.999, 1357518719.988, 1357518749.999, 1357518779.999, 
1357518809.999, 1357518839.999, 1357518869.999, 1357518899.999, 
1357518929.999, 1357518959.999, 1357518989.999, 1357519019.999, 
1357519049.999, 1357519079.998, 1357519109.999, 1357519139.999, 
1357519169.999, 1357519199.999, 1357519229.999, 1357519259.999, 
1357519289.999, 1357519319.999, 1357519349.999, 1357519379.999, 
1357519409.999, 1357519439.999, 1357519469.999, 1357519499.999, 
1357519529.999, 1357519559.999, 1357519589.999, 1357519619.999, 
1357519649.999, 1357519679.999, 1357519709.999, 1357519739.999, 
1357519769.999, 1357519799.999, 1357519829.997, 1357519859.999, 
1357519889.999, 1357519919.999, 1357519949.999, 1357519979.999, 
1357520009.999, 1357520039.999, 1357520069.999, 1357520099.999, 
1357520129.999, 1357520159.999, 1357520189.999, 1357520219.999, 
1357520249.999, 1357520279.999, 1357520309.999, 1357520339.999, 
1357520369.999, 1357520399.999), tzone = "", tclass = c("POSIXct", 
"POSIXt"), class = c("POSIXct", "POSIXt")), count = c(20763L, 
12030L, 22188L, 12183L, 21112L, 11628L, 21543L, 12609L, 20095L, 
12992L, 21552L, 12447L, 21113L, 12236L, 21705L, 12018L, 21140L, 
11820L, 21571L, 12803L, 21146L, 12081L, 21171L, 12440L, 21353L, 
11708L, 21476L, 12210L, 21364L, 12041L, 21907L, 11934L, 22207L, 
12403L, 21629L, 12676L, 21046L, 12196L, 21673L, 12190L, 21830L, 
11652L, 20943L, 12350L, 20848L, 11800L, 21085L, 12367L, 21519L, 
12325L, 22217L, 12195L, 22405L, 11869L, 21380L, 12145L, 21842L, 
12224L, 21793L, 12856L, 34934L, 24073L, 41005L, 33964L, 46240L, 
41287L, 52697L, 62618L, 78594L, 68193L, 76617L, 63747L, 90556L, 
75830L, 104609L, 51063L, 67046L, 66977L, 82513L, 87228L, 107474L, 
141878L, 127290L, 70953L, 98879L, 87814L, 117309L, 113463L, 150979L, 
198271L, 170456L, 108325L, 119583L, 111803L, 117067L, 186768L, 
226191L, 235546L, 228039L, 165570L, 159472L, 161707L, 137614L, 
180049L, 254616L, 302166L, 336723L, 234902L, 202560L, 210679L, 
173053L, 162839L, 262536L, 306859L, 249385L, 300646L, 219594L, 
209819L, 166758L, 173716L, 268453L, 310940L, 264778L, 289798L, 
202234L, 236882L, 217502L, 181157L, 196976L, 201901L, 228233L, 
221241L, 220140L, 122623L, 76699L, 105589L, 381687L, 264571L, 
187083L, 175972L, 202483L, 198547L, 196964L, 206402L, 181260L, 
189319L, 162374L, 160412L, 186897L, 184529L, 160056L, 177326L, 
184240L, 160864L, 156540L, 150392L, 157610L, 138447L, 148423L, 
147318L, 148463L, 114389L, 163761L, 126624L, 167519L, 138240L, 
133005L, 120187L, 155814L, 132751L, 140000L, 120323L, 124415L, 
129450L, 116635L, 125364L, 108176L, 118877L, 143640L, 132457L, 
118641L, 114330L, 135960L, 148066L, 130787L, 130230L, 130436L, 
107109L, 129405L, 116093L, 135293L, 119048L, 147364L, 127028L, 
145576L, 139960L, 139896L, 139433L, 127806L, 124845L, 141319L, 
132821L, 129279L, 111905L, 130898L, 133135L, 138201L, 121460L, 
143846L, 92964L, 100614L, 85637L, 139594L, 124302L, 106071L, 
128247L, 120788L, 176300L, 144378L, 126209L, 117886L, 111001L, 
105855L, 122387L, 152357L, 103217L, 134069L, 106021L, 91796L, 
103335L, 99422L, 115839L, 147787L, 128868L, 123416L, 109312L, 
129782L, 109397L, 130418L, 113709L, 103774L, 133272L, 137311L, 
138079L, 132308L, 119744L, 164226L, 149361L, 135044L, 110185L, 
151246L, 141811L, 160525L, 128407L, 159161L, 142969L, 150370L, 
128705L, 151884L, 171663L, 150428L, 154910L, 165016L, 163729L, 
169727L, 144913L, 163476L, 159984L, 155767L, 142334L, 177964L, 
169230L, 135086L, 139350L, 174013L, 164427L, 154289L, 143392L, 
187156L, 139426L, 159207L, 187435L, 198519L, 132559L, 163582L, 
179069L, 150413L, 161463L, 173357L, 162457L, 136248L, 144086L, 
151073L, 130237L, 144066L, 179840L, 135843L, 147757L, 206373L, 
140734L, 177374L, 176168L, 154999L, 136136L, 187568L, 142357L, 
152180L, 168528L, 131228L, 140622L, 145363L, 93070L, 58613L, 
82024L, 86640L, 77493L, 71205L, 87641L, 89232L, 99214L, 89311L, 
87948L, 90790L, 91326L, 106916L, 97318L, 89452L, 91658L, 82069L, 
92559L, 89194L, 81721L, 83490L, 96388L, 90145L, 79861L, 90301L, 
77676L, 262966L, 227355L, 256477L, 238905L, 241260L, 206168L, 
229477L, 215515L, 245217L, 232026L, 225308L, 223537L, 198524L, 
237840L, 233483L, 193081L, 216570L, 212949L, 203150L, 240861L, 
209596L, 200673L, 180099L, 187726L, 187642L, 188402L, 176871L, 
216090L, 203310L, 184723L, 195702L, 204137L, 276952L, 313717L, 
323208L, 308448L, 321638L, 378236L, 352163L, 413678L, 395997L, 
354317L, 366915L, 339465L, 346781L, 394895L, 355176L, 349618L, 
417590L, 335474L, 405686L, 362581L, 356525L, 354142L, 383487L, 
334305L, 327489L, 336201L, 374153L, 341485L, 321473L, 308773L, 
15709L, 8870L, 15563L, 8944L, 15941L, 9342L, 16303L, 8951L, 14969L, 
9385L, 14537L, 9963L, 15676L, 9011L, 16552L, 9587L, 16802L, 9693L, 
15267L, 8946L, 14189L, 9067L, 14359L, 9776L, 167922L, 337364L, 
350941L, 362928L, 364922L, 319641L, 348687L, 321356L, 400161L, 
334171L, 332829L, 323842L, 397809L, 375694L, 384432L, 356825L, 
350846L, 395942L, 359471L, 296926L, 418481L, 322144L, 335658L, 
347212L, 334421L, 375769L, 364300L, 317370L, 373192L, 346713L, 
356341L, 327225L, 305538L, 347815L, 276914L, 322149L, 303627L, 
292363L, 284724L, 305082L, 373363L, 304386L, 438592L, 403579L, 
430549L, 450536L, 432445L, 389779L, 434888L, 375010L, 456096L, 
577393L, 451122L, 432354L, 425547L, 417729L)), .Names = c("time", 
"count"), row.names = c(NA, -480L), class = "data.frame")

> dput(ev)
structure(list(dt = structure(c(1357508700, 1357510680, 1357512540, 
1357515360, 1357517220, 1357517700, 1357518000, 1357518000, 1357519140, 
1357519140), class = c("POSIXct", "POSIXt"), tzone = ""), event = structure(c(7L, 
8L, 3L, 2L, 5L, 6L, 1L, 4L, 2L, 9L), .Label = c("Event 1", 
"Event 2", "Event 3", 
"Event 4", "Event 5", 
"Event 6", "Event 7", 
"Event 8", "Event 9"
), class = "factor")), .Names = c("dt", "event"), row.names = c(NA, 
-10L), class = "data.frame")

Library Versions:

> sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: x86_64-redhat-linux-gnu (64-bit)

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] reshape2_1.2.2   xts_0.9-1        zoo_1.7-9        gdata_2.12.0     data.table_1.8.6 caTools_1.14    
[7] scales_0.2.3     ggplot2_0.9.3   

Simplied code - this still doesnt work

library(scales)
library(ggplot2)
taonedf<-dget("taonedf") #in this thread
ev<-dget("ev") #in this thread

ggplot(data=taonedf, aes(x=time, y=count/30)) +
  geom_line() +
  geom_vline(data=ev,aes(xtintercept=as.numeric(dt)))
like image 409
rjb101 Avatar asked Jan 15 '13 17:01

rjb101


2 Answers

To get geom_vline() display lines as intended, first, library scales should be loaded. Then use as.numeric() in geom_vline().

library(scales)    
+ geom_vline(data=ev,aes(xintercept=as.numeric(dt)))
like image 169
Didzis Elferts Avatar answered Oct 16 '22 08:10

Didzis Elferts


Two things

  • You need to wrap the datetimes for the vline in as.numeric
  • You misspelled xintercept

Fixing those:

library("ggplot2")
library("scales")

ggplot(data=taonedf, aes(x=time, y=count/30)) + 
  geom_line(color="#009E73") + 
  geom_vline(data=ev,aes(xintercept=as.numeric(dt)))+
  scale_y_continuous(name="requests per second", labels = format_format(scientific=FALSE, big.mark=",")) +
  scale_x_datetime(name="",labels = date_format("%b %d\n%H:%M") ) +
  labs(title=paste("Requests per Second - All Requests")) +
  theme(legend.position = "none")

enter image description here

like image 35
Brian Diggs Avatar answered Oct 16 '22 09:10

Brian Diggs