Ed G
Ed G

Reputation: 822

Loess smoother based on a subset in ggplot2

I'm working on some plots where the last two points aren't up to date. The resulting low figures pull my smoothing line down.

dat1<-data.frame(vals=c(30,40,50,30,40,50,30,10,5),
                 q.year=c("q1 09", "q2 09", "q3 09", "q4 09", "q1 10", "q2 10", "q3 10", "q4 10", "q1 11"),
                 dummy=rep("g1", 9)
)
dat1$q.year<-factor(dat1$q.year, unique(dat1$q.year))

ggplot(dat1, aes(x=q.year, y=vals, group=dummy))+
  geom_point()+
  geom_line()+
  geom_smooth(method="loess", se=FALSE, lty=2)

I'd like to plot the smoother without the two low points. I can do this with a subset:

gg1<-ggplot(dat1, aes(x=q.year, y=vals, group=dummy))+
  geom_point()+
  geom_line()+
  geom_smooth(data=subset(dat1, q.year %in% levels(q.year)[1:7]),
              method="loess", se=FALSE, lty=2)
print(gg1)

But, when it comes to reusing the plot with new data (i.e. gg2<- gg1 %+% someMoreData) I have to add another line to subset the new data set (someMoreData).

Is there a better way to do this? Can I refer to the data already passed into the plot?

Apologies if the question isn't quite clear - will update if needed

Thanks

Upvotes: 3

Views: 1577

Answers (1)

Roland
Roland

Reputation: 132706

fun <- function(val,fac,n) {val[fac %in% tail(levels(fac),n)] <- NA; val}

gg1a <- ggplot(dat1, aes(x=q.year, y=vals, group=dummy))+
  geom_point()+
  geom_line()+
  geom_smooth(aes(x=q.year,y=fun(vals,q.year,2)),
              method="loess", se=FALSE, lty=2)
print(gg1a)
#Warning message:
#Removed 2 rows containing missing values (stat_smooth).

dat2<-data.frame(vals=c(50,40,50,30,40,50,30,10,5,5),
                 q.year=c("q1 09", "q2 09", "q3 09", "q4 09", "q1 10", "q2 10", "q3 10", "q4 10", "q1 11","q2 11"),
                 dummy=rep("g1", 10)
)
#make sure that your factor is ordered, 
#otherwise it gets reordered by ggplot according to levels
dat2$q.year <- factor(as.character(dat2$q.year),levels=as.character(dat2$q.year),ordered = TRUE)

gg1a %+% dat2
#Warning message:
#Removed 2 rows containing missing values (stat_smooth).

Upvotes: 2

Related Questions