R version 2.15.1 (2012-06-22) Platform: x86_64-pc-mingw32/x64 (64-bit) attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] ggplot2_0.9.3 loaded via a namespace (and not attached): [1] colorspace_1.2-1 dichromat_2.0-0 digest_0.6.2 grid_2.15.1 gtable_0.1.2 labeling_0.1 [7] MASS_7.3-23 munsell_0.4 plyr_1.8 proto_0.3-10 RColorBrewer_1.0-5 reshape2_1.2.2 [13] scales_0.2.3 stringr_0.6.2 tools_2.15.1
I'm trying to draw vertical lines on my line plots. They're all plots of weather-related time series data, with the date column generated in R as a series based on a start date (see data snipped below). The vertical lines will represent passage of cold fronts, and there's a column (ncep.front) in the same data frame with 1 in cells on dates where the front passed, otherwise zero. What I want to do is extract an array of front dates for each season, then apply that as a vline layer to each plot. I've got this working intermittently which is very frustrating; I think there's something fundamental I don't understand.In the simplest example, I have a set of dates, the column of 0/1 for cold fronts, and one of my time series variables:date ncep.front hrs.foraging1 2012-08-28 0 11.122 2012-08-29 0 11.373 2012-08-30 0 10.874 2012-08-31 0 10.62 ....I can generate a list of the frontal dates for the 2012 fall season from the main data frame with this code:
>fronts <- which(few.cols[few.front] == 1)>frontdates <- few.cols[fronts,1]
> frontdates
[1] "2012-09-08" "2012-09-13" "2012-09-28" "2012-10-06" "2012-10-10" "2012-10-14" "2012-10-17" "2012-10-25"
[9] "2012-11-04"I've tried a bunch of variations on the following calls to draw the actual plot. This particular one generates the graph but not the lines, and I get warning messages.>ggplot (data = fewest, aes(x = date,y = hrs.foraging) ) +
geom_line() +
geom_vline (aes (xintercept = as.numeric("2012-09-08","2012-09-13","2012-09-28","2012-10-06","2012-10-10","2012-10-14","2012-10-17","2012-10-25","2012-11-04")
)))
Warning messages:
1: In eval(expr, envir, enclos) : NAs introduced by coercion
2: Removed 1 rows containing missing values (geom_segment).
Based on what I've read in the ggplot Springer book and online in various forums, using the date format with the as.numeric call should be at least close to what would work. Why can't I just say "xintercept = frontdates" or "xintercept = as.numeric(frontdates)"? Those also give me errors.
I have tried passing an array of integer offsets for the correct dates ("fronts" as calculated above), but it will only print 5 lines; asking for more than that gives me a different error message. But I'm afraid ggplot doesn't want offsets here (so why did it work for 5 but not 6 or more?); it wants the actual dates to match to the dates on the x-axis, no? So how do I get it to accept multiple dates?
Any suggestions greatly appreciated.
Jennifer Krauel
University of Tennessee, Knoxville
library(ggplot2)
fewest2<- structure(list(date = structure(15580:15653, class = "Date"),
ncep.front = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L,
0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L,
0L, 0L, 0L), hrs.foraging = c(11.12, 11.37, 10.87, 10.62,
11.38, 11.3, 11.58, 11.28, 11.78, 12.05, 14.13, 11.12, 10.85,
10.65, 12.2, 12.78, 14.05, 11.55, 12.22, 11.73, 11.55, 11.1,
12.22, 10.57, 11.43, 11.8, 11.9, 11.68, 11.62, 11.85, 12.17,
11.98, 12.52, 11.87, 11.53, 11.37, 11.67, 11.52, 11.6, 11.92,
11.95, 12.2, 12.65, 12.15, 12.52, 11.92, 12.27, 11.9, 12.28,
11.97, 12.32, 11.1, 12.67, 12.12, 12.03, 12.22, 12.17, 11.97,
12.28, 12.03, 11.85, 11.77, 11.37, 12.33, 12.15, 11.7, 11.85,
12.02, 12.32, 12.27, 11.58, 11.7, 12.18, 12.3)), .Names = c("date",
"ncep.front", "hrs.foraging"), class = "data.frame", row.names = c(NA,
-74L))
#create an array of rows representing frontal passages for the season
fronts <- which(fewest2[,2] == 1)
frontdates <- fewest2[fronts,1]
#try a simple line plot, with vertical bars representing fronts
ggplot (data = fewest2, aes(x = date,y = hrs.foraging)) +
geom_line()+
geom_vline(aes(xintercept = as.numeric(frontdates)))
#this returns an error: Error in data.frame(xintercept = c(15591, 15596, 15611, 15619, 15623, :
# arguments imply differing number of rows: 9, 74
#Try it brute force by entering the dates directly:
ggplot (data = fewest2, aes(x = date,y = hrs.foraging)) +
geom_line()+
geom_vline(aes(xintercept = as.numeric("2012-09-08","2012-09-13","2012-09-28","2012-10-06","2012-10-10","2012-10-14","2012-10-17","2012-10-25","2012-11-04")
))
#now I get a plot, but some warning messages and no vlines:
#Warning messages:
#1: In eval(expr, envir, enclos) : NAs introduced by coercion
#2: Removed 1 rows containing missing values (geom_segment).
Hopefully someone can point out what I'm doing wrong, and how to make this work. I have the attributes included in the ggplot command because I intend for this to work with facets, but I could move them if that would make the difference.
Thanks,
Jennifer