DHBI
DHBI

Reputation: 79

Conditional text formatting with ggplot

I've had a good look around this site and others on how to set the hjust and vjust according to a value in a particular column. The following shows how the data is structured (but is a simplified subset of many entries for many years):

    YearStart <- c(2001,2002,2003,2001,2002,2003)
    Team <- c("MU","MU","MU","MC","MC","MC")
    Attendance <- c(67586,67601,67640,33058,34564,46834)
    Position <- c(3,1,3,1,9,16)
    offset <-c()

    df <- data.frame(YearStart,Team,Attendance,Position)

so

> head(df)
  YearStart Team Attendance Position
1      2001   MU      67586        3
2      2002   MU      67601        1
3      2003   MU      67640        3
4      2001   MC      33058        1
5      2002   MC      34564        9
6      2003   MC      46834       16

what I would like to acheive is a vjust value based on the Team. In the following, MU would be vjust=1 and MC would be vjust=-1 so I can control where the data label is located from the data group with which it is associated.

I've tried to hack around a couple of examples that use a function containing a lookup table (it's not a straight ifelse as I have many values for Team) but I can't seem to pass a string to the function through the aes method along these lines:

lut <- list(MU=1,MC=-1)
vj <-function(x){lut[[x]]}
p=ggplot(df, aes(YearStart, Attendance, label=Position, group=Team))+
   geom_point()+
   geom_text(aes(vjust = vj(Team) ) )
print(p)

The following is pseudo(ish)code which applies the labels twice to each group in each location above and below the points.

p=ggplot(df, aes(YearStart, Attendance, label=Position, group=Team))+
   geom_point()+
   geom_text(aes(Team="MU"), vjust=1)+
   geom_text(aes(Team="MC"), vjust=-1)
print(p)

I've tried several other strategies for this and I can't tell whether I'm trying this from the wrong direction or I'm just missing a very trivial piece of ggplot syntax. I've accomplished a stop-gap solution by labelling them manually in Excel but that's not sustainable :-)

Upvotes: 3

Views: 2935

Answers (1)

Andrie
Andrie

Reputation: 179388

To specify an aesthetic, that aesthetic should be a column in your data.frame.

(Notice also that your lookup function should have single brackets, not double.)

And a final thought: vjust and hjust are strictly only defined between [0, 1] for left/bottom and right/top justification. In practise, however, it is usually possible to extend this. I find that settings of (-0.2, 1.2) work quite well, in most cases.

lut <- list(MU=-0.2, MC=1.2)
vj <- function(x) lut[x]

df$offset <- vj(df$Team)

library(ggplot2)
ggplot(df, aes(YearStart, Attendance, label=Position, group=Team)) +
    geom_point(aes(colour=Team)) +
    geom_text(aes(vjust = offset))

enter image description here

Upvotes: 5

Related Questions