I'm using ggplot2 with the directlabels package in a geom_line()
plot, and I would like one of the labels to read "X-M". However, in my data.frame()
"X-M" as column name gets renamed to "X.M", and I couldn't find documentation on how to provide the direct.label
function with custom labels names, nor reading the source helped. (directabels doesn't seem to honor the label names set in the ggplot scale, which is the first thing that I tried.)
Sample code:
library("scales")
library("reshape2")
library("ggplot2")
library("directlabels")
data = data.frame(
C = c(1.2, 1.4, 0.3, -2.0, 0.5),
I = c(1.2, 1.5, -1.3, -3.8, 1.8),
G = c(0.2, 0.3, 0.3, 0.2, 0.2),
"X-M" = c(2.9, -0.7, 0.3, -2.8, 1.5) +
c(-2.7, 0.2, 0.4, 3.6, -2.4),
year = c("2006", "2007", "2008", "2009", "2010"))
p <- ggplot(data = melt(data), aes(year, value, color = variable)) +
geom_line(aes(group = variable)) +
scale_color_hue(breaks = c("C", "I", "G", "X.M"),
labels = c("C", "I", "G", "X-M")) # directlabels doesn't
# use this
# Compare:
p
# with:
direct.label(p, list(last.points, hjust = -0.25))
Resulting graphs can be seen here. The one with directlabels uses "X.M" instead of "X-M". Many thanks in advance!