3

If want to create a boxplot with overlaid points which are labeled at the right hand side. I tried geom_dl form the directlabels package, but get stuck.

library(ggplot2)
library(directlabels)

set.seed(0)
x <- data.frame(label=LETTERS[1:15], 
                x="a",
                y = rnorm(15))
x$xpos <- as.numeric(x$x) + .25

g <- ggplot(x, aes(x=x, y=y)) + 
  geom_boxplot(width=.4) +
  geom_point(col="blue")

Position labels without overlap control using method last.points.

g + geom_dl(aes(x=xpos, label=label), method = "last.points") 

enter image description here

Using method last.qp to avoid overlaps fails.

g + geom_dl(aes(x=xpos, label=label), method = "last.qp")   # fails

Error in approx(df[, x.var], df[, tiebreak.var], xvals) : 
  need at least two non-NA values to interpolate

Any ideas, how to get geom_dl running or achieve proper placemant in another way?

Add-on

Using method last.bumpup as @lukeA suggested below works quite fine. However, some labels are still overlapping. Is there a way to tweak this?

enter image description here

Add-on 2

I think the problem only arises when using a factor with more than one level on x.

set.seed(0)
x <- data.frame(label=LETTERS[1:24], 
                g1 = c("a"),
                g2 = c("a", "b"),
                y = rnorm(24))
x$g1 <- as.factor(x$g1)
x$g2 <- as.factor(x$g2)
x$xpos1 <- as.numeric(x$g1) + .25
x$xpos2 <- as.numeric(x$g2) + .25

The label placement for the first plot is fine. For the second with two levels the overlaps remain.

# one group
ggplot(x, aes(x=g1, y=y)) + 
  geom_boxplot(width=.4) +
  geom_point(col="blue") +
  geom_dl(aes(x=xpos1, label=label), method= "last.bumpup")

enter image description here

Two levels

# two groups
ggplot(x, aes(x=g2, y=y)) + 
  geom_boxplot(width=.4) +
  geom_point(col="blue") +
  geom_dl(aes(x=xpos2, label=label), method= "last.bumpup")

enter image description here

2
  • I use directlabels_2015.12.16 and ggplot2_2.1.0. Commented Apr 24, 2016 at 18:45
  • Ah, thanks! My version of directlabels was way out of date Commented Apr 24, 2016 at 19:17

1 Answer 1

2

You could use the last.bumpup method, which combines last.points and bumpup (last.bumup <- list("last.points","bumpup")):

g + geom_dl(aes(x=xpos, label=label), method = "last.bumpup")  

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

nice, thanks! However, I still get overlap (see add-on above). Any ideas for this?
Strange, there should not be any overlaps. Maybe reduce font size and/or increase the y limits or the device height: g + geom_dl(aes(x=xpos, label=label), method = list("last.bumpup", cex=.8)) + ylim(c(min(x$y), max(x$y)*10)) or ggsave(tf <- tempfile(fileext = ".png"), g + geom_dl(aes(x=xpos, label=label), method = "last.bumpup") + ylim(c(min(x$y), max(x$y)*15)), dpi = 320, height=12); shell.exec(tf)
Thanks for the code. I figured that the problem only arises when using multiple levels on x (see add-on 2 above). A bug?
I don't know. If everything fails, I'd use facets like + facet_wrap(~g2, scales = "free_x") + theme(panel.margin=unit(0, "cm")).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.