user1609452
user1609452

Reputation: 4444

Arrows pointing to data from label

I have a number of related time series I want to plot together. I am using ggplot2 Here is an example of what my data looks like:

set.seed(0)
require(ggplot2)
id <- LETTERS[1:18]
appDates <- as.Date("2000-01-01", origin = '1970-01-01') + 1:10
appRate <- runif(18, 1,4)
appRank <- rank(-appRate - colSums(anorm))

anorm <- array(rnorm(18*11), c(11,18))
tempDf <-lapply(seq_along(appDates), function(x) data.frame(adate = appDates[x], group = 1:18, id = id, arate = appRate + colSums(anorm[1:(x+1),]), ranked = appRank))

tempDf <- do.call(rbind, tempDf)
ggplot(tempDf, aes(x = adate, y = arate, group = group, color = id)) + geom_line()

This is fine but I would like arrows going from the id labels to the relevant time series as it is hard to pick out a particular path with the colors being similar.

enter image description here

I have tried `directlabels' but I cant seem to quite get it

p <- ggplot(tempDf, aes(x = adate, y = arate, group = group, color = id)) + geom_line()
require(directlabels)
direct.label(p,list(last.points, hjust=0.8, vjust = 1))

enter image description here

A crude example done by hand of what I am sort of looking for

enter image description here

With the addition of final rankings I have added differing line thickness to aid identification.

p <- ggplot(tempDf, aes(x = adate, y = arate, group = group, color = id, size = ranked)) + geom_line()
p + scale_size(range=c(2.6, 0.4), guide=FALSE)+
       guides(colour = guide_legend(override.aes = list(size=seq(2.6,0.4, length.out = 18)[appRank])))

enter image description here

Upvotes: 1

Views: 734

Answers (1)

nograpes
nograpes

Reputation: 18323

Although this doesn't answer your question exactly, I wanted to include some pictures of what I mean, so I put it in an answer.

If rank is really what you want to show, then I especially recommend the heat map. Except that instead of using the rate as the y-axis, you use the rank, and you use the rate as the fill color. Here is what I mean:

# I think your rank was broken -- but I might be missing something.
tempDf$real.rank<-unlist(by(tempDf$arate,tempDf$adate,rank)) 
ggplot(tempDf, aes(x = adate ,fill = arate, y = real.rank)) +   ]
  geom_tile() +
  geom_text(aes(label=id),color='white')

enter image description here

If you really wanted to emphasize the change in rank, you could draw lines in between the letters:

ggplot(tempDf, aes(x = adate ,fill = arate, y = real.rank)) +   
  geom_tile() +
  geom_text(aes(label=id),color='white') +
  geom_line(aes(group=id,color=id))

enter image description here

In either case, I think that you add an extra dimension of data with a heatmap, the rank itself, at the expense of making it more difficult to tell the exact rate.

Upvotes: 1

Related Questions