I recently saw a line chart in the Economist where the title had colored words to match the colors of the groups used in the line chart. I was wondering how to do this with
A somewhat cumbersome solution with annotation_custom:
ggplot(dat, aes(year, concentration, color = group)) +
geom_line(size = 1.5) +
geom_point(size = 4) +
scale_y_continuous(limits = c(0, 0.16)) +
labs(x = NULL, y = NULL, title = ' ') +
theme_minimal() +
theme(legend.position = 'none') +
scale_color_manual(values = c('#EEB422', '#238E68')) +
annotation_custom(textGrob('Concentration of', gp = gpar(col = 'black')),
xmin = 1972, xmax = 1972, ymin = 0.165, ymax = 0.165) +
annotation_custom(textGrob('affluence', gp = gpar(col = '#EEB422', fontface = 'bold')),
xmin = 1975.7, xmax = 1975.7, ymin = 0.165, ymax = 0.165) +
annotation_custom(textGrob(' and ', gp = gpar(col = 'black')),
xmin = 1977.65, xmax = 1977.65, ymin = 0.165, ymax = 0.165) +
annotation_custom(textGrob('poverty', gp = gpar(col = '#238E68', fontface = 'bold')),
xmin = 1979.35, xmax = 1979.35, ymin = 0.165, ymax = 0.165) +
annotation_custom(textGrob('nationwide', gp = gpar(col = 'black')),
xmin = 1982, xmax = 1982, ymin = 0.165, ymax = 0.165)
which gives:
Main drawback of this approach is that it requires a lot fiddling with the parameters to get the words of the title on the right spots.