I have a problem with geom_dl()
where it is not placing my label correctly because two groups have the same label. I can see that
data$groups <- data$label
inside of the GeomDl call is causing the trouble, but I can't figure out how to fix it.
This is what it currently looks like:
and this is what it should look like:
Here are the data and the ggplot code:
dat <- structure(list(level = structure(c(3L, 3L, 1L, 1L, 2L, 2L), .Label = c("2", "3", "1"), class = "factor"), year = c(2013L, 2014L, 2013L, 2014L, 2013L, 2014L), mean = c(9.86464372862218, 9.61027271206025, 18.3483708337732, 15.3459903281993, 6.75036415837688, 7.33169996044336), pchange = c(" 68%", " 68%", " 76%", " 76%", " 76%", " 76%")), .Names = c("level", "year", "mean", "pchange"), row.names = c(413L, 414L, 419L, 420L, 425L, 426L), class = "data.frame")
ggplot(dat, aes(x = year, y = mean)) +
geom_line(aes(color = level)) +
geom_dl(aes(label=pchange, color=level), method=list("last.qp"))
Here's some voodoo with invisible unicode chars:
dat$pchange2 <- dat$pchange
dat$pchange2[3:4] <- paste0(dat$pchange[3:4], "\u200B")
ggplot(dat, aes(x = year, y = mean)) +
geom_line(aes(color = level)) +
geom_dl(aes(label=pchange2, color=level), method=list("last.qp"))
If you have multiple lines with the same label, you can add the same char once, twice, etc. The same idea can be used to write a minimal preprocessing function which would be a more or less universal solution.
Here's how a complete solution might look like (by @fishgal64):
require(magrittr)
require(dplyr)
dat <- select(dat, level, pchange) %>%
unique() %>%
mutate(pchange2 = ifelse(duplicated(pchange), paste0(pchange, "\u200B"), pchange)) %>%
merge(dat)