Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

r - ggplot2: color individual words in title to match colors of groups

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 ggplot2 object. Here is some code to make a line chart with everything like the econimist article except the colored words in the title. At the bottom I show the desired output.

This question is not about theoretical ways to display this info (like directly labeling or a legend) but rather specifically about coloring individual words in titles.

data <- data.frame(
    group = rep(c('affluence', 'poverty'), each = 6),
    year = rep(c(1970, 1980, 1990, 2000, 2010, 2012), 2),  
    concentration = c(.125, .12, .14, .13, .145, .146, .068, .09, .125, .119, .13, .135)
)

library(ggplot2)

ggplot(data, aes(year, concentration, color = group)) +
    geom_line(size = 1.5) +
    geom_point(size = 4) +
    scale_y_continuous(limits = c(0, .15)) +
    labs(
        x = NULL, y = NULL, 
        title = 'Concentration of affluence and poverty nationwide'
    ) +
    theme_minimal() +
    theme(
        legend.position = 'none'
    ) +
    scale_color_manual(values = c('#EEB422', '#238E68'))

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This solution is based on Displaying text below the plot generated by ggplot2 and Colorize parts of the title in a plot (credits to the contributors there!).

By using phantom placeholders for text, we avoid (most of the) hardcoding of positions.

# create text grobs, one for each color
library(grid)
t1 <- textGrob(expression("Concentration of " * phantom(bold("affluence")) * "and" * phantom(bold("poverty")) * " nationwide"),
               x = 0.5, y = 1.1, gp = gpar(col = "black"))

t2 <- textGrob(expression(phantom("Concentration of ") * bold("affluence") * phantom(" and poverty nationwide")),
               x = 0.5, y = 1.1, gp = gpar(col = "#EEB422"))

t3 <- textGrob(expression(phantom("Concentration of affluence and ") * bold("poverty") * phantom(" nationwide")),
               x = 0.5, y = 1.1, gp = gpar(col = "#238E68"))

# plot and add grobs with annotation_custom
ggplot(data, aes(year, concentration, color = group)) +
  geom_line(size = 1.5) +
  geom_point(size = 4) +
  annotation_custom(grobTree(t1, t2, t3)) +
  scale_y_continuous(limits = c(0, 0.15)) +
  scale_color_manual(values = c("#EEB422", "#238E68")) +
  coord_cartesian(clip = "off") +
  labs(x = NULL, y = NULL) +
  theme_minimal() +
  theme(legend.position = 'none',
        # add some extra margin on top
        plot.margin = unit(c(4, 1, 1, 1), "lines"))

enter image description here


With a larger number of colored words, the creation of the different expressions should be done more programmatically. See e.g. the nice multiTitle function in a similar question for base plot: title: words in different colors?, which should be useful in ggplot as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...