In the plot below, direct label positions were tweaked a bit vertically, but they get clipped at the left/right edges. Is there any way to avoid clipping (similar to xpd=TRUE
) or adjust the clipped labels inwards in the plot frames?
Here's the code for this example:
library(car)
library(reshape2)
library(ggplot2)
library(directlabels)
library(nnet)
## Sec. 8.2 (Nested Dichotomies)
# transform data
Womenlf <- within(Womenlf,{
working <- recode(partic, " 'not.work' = 'no'; else = 'yes' ")
fulltime <- recode(partic,
" 'fulltime' = 'yes'; 'parttime' = 'no'; 'not.work' = NA")})
mod.working <- glm(working ~ hincome + children, family = binomial,
data = Womenlf)
mod.fulltime <- glm(fulltime ~ hincome + children, family = binomial,
data = Womenlf)
predictors <- expand.grid(hincome = 1:50,
children = c("absent", "present"))
fit <- data.frame(predictors,
p.working = predict(mod.working, predictors, type = "response"),
p.fulltime = predict(mod.fulltime, predictors, type = "response"),
l.working = predict(mod.working, predictors, type = "link"),
l.fulltime = predict(mod.fulltime, predictors, type = "link")
)
fit <- within(fit, {
`full-time` <- p.working * p.fulltime
`part-time` <- p.working * (1 - p.fulltime)
`not working` <- 1 - p.working
})
# Figure 8.10
fit2 = melt(fit,
measure.vars = c("full-time","part-time","not working"),
variable.name = "Participation",
value.name = "Probability")
gg <- ggplot(fit2,
aes(x = hincome, y = Probability, colour = Participation)) +
facet_grid(~ children, labeller = function(x, y) sprintf("%s = %s", x, y)) +
geom_line(size = 2) + theme_bw()
direct.label(gg, list("top.bumptwice", dl.trans(y = y + 0.2)))
As @rawr pointed out in the comment, you can use the code in the linked question to turn off clipping, but the plot will look nicer if you expand the scale of the plot so that the labels fit. I haven't used directlabels and am not sure if there's a way to tweak the positions of individual labels, but here are three other options: (1) turn off clipping, (2) expand the plot area so the labels fit, and (3) use geom_text instead of directlabels to place the labels.
Updating to
ggplot2
v2.0.0 anddirectlabels
v2015.12.16One approach is to change
direct.label
's method. There aren't too many other good options for labelling lines, butangled.boxes
is a possibility.OR
Original answer
One approach is to change
direct.label
's method. There aren't too many other good options for labelling lines, butangled.boxes
is a possibility. Unfortunately,angled.boxes
does not work out of the box. The functionfar.from.others.borders()
needs to be loaded, and I modified another function,draw.rects()
, to change the colour of the box boundaries to NA. (Both functions are available here.)(Or adapt answers from here)