On 2012-Aug-15, at 02:10 , Lingbing <
feng...@gmail.com> wrote:
>
> Thanks for you solution. My question is exactly about missing single points when using geom_line. As you said "Both points on either side of the 1993 point are missing".
> Adding points separately is feasible, however when we have many single points, adding them one by one would be a nuisance.
I don't know a one-line way to do this.
But to continue on Dennis' idea, you could detect all such points and plot them in one call to geom_point. Here is a way to do it:
# create data with points surrounded by NAs
set.seed(1)
n <- 100
d <- data.frame(x=1:n, y=runif(n))
i <-
sample.int(n, 10)
i <- c(i, i+2)
d$y[i] <- NA
# test plot
library("ggplot2")
ggplot(d) + geom_line()
ggplot(d, aes(x=x, y=y)) + geom_line() + geom_point()
# detect points surrounded by NA
nas <-
is.na(d$y)
# in that vector a point surrounded by NAs is a FALSE surrounded by two TRUE
# in numeric terms that's a zero surrounded by two ones
# i.e. a value of 2/3 in a moving average with a window of size 3
# NB: I am using a package to compute the moving average but you could easily recode it "by hand" to make this more portable
library("pastecs")
movAvg <- decaverage(as.numeric(nas))
nasAvg <- as.numeric(extract(movAvg, component="filtered"))
isolated <- which(nasAvg == 2 / 3)
# This would be easy to package in a function called something like isolated(), which would return the indexes of the isolated values given a vector and to use the function afterwards
# now plot the original data with geom_line and only the isolated points with geom_point
ggplot(mapping=aes(x=x, y=y)) + geom_line(data=d) + geom_point(data=d[isolated,])
ggplot(mapping=aes(x=x, y=y)) + geom_line(data=d) + geom_point(data=d[isolated,], size=1, colour="red")