I seem unable to correctly color axis text on a faceted plot when the scales parameter is set to \"free\". Consider the following dataset:
I don't think it's a bug. The problem is that v here is basically a string of characters, length 26, which defines colours for the first 26 breaks on the x-axis. When the x-axis has 26 breaks exactly, well & good; when it has less than that (which is the case when you set scales="free"), it simply restarts at the beginning for each axis. Q is red here because it's in the fourth position in the second plot, although the v[4]'s red was meant for D, in the first plot.
Based on what I've tried & read here on SO, one can't map aesthetics into theme(), which controls the appearance of axis text in ggplot.
It's possible to hack a solution by hiding the axis & using geom_text() instead to simulate an axis, since the latter does accept aesthetics mapped from the data. It may not be very elegant, though:
g2 <- ggplot(cbind(X, v), #add v to X
aes(x = V1, y = V2)) +
geom_point() +
# make space to accommodate the fake axis
expand_limits(y = -0.05) +
# create a strip of white background under the fake axis
geom_rect(ymin = -5, ymax = 0, xmin = 0, xmax = nrow(X) + 1, fill = "white") +
# fake axis layer, aligned below y = 0
geom_text(aes(colour = v, label = V1), y = 0, vjust = 1.1) +
# specify the font colours for fake axis
scale_colour_manual(values = c("black", "red"), guide = F) +
# hide the actual x-axis text / ticks
theme(axis.text.x = element_blank(), axis.ticks.x = element_blank())
g2 + facet_wrap( ~V3, scales = "free" )