我最近在《经济学人》上看到了一张折线图,其中标题包含彩色单词以匹配折线图中使用的组的颜色 https://www.economist.com/blogs/graphicdetail/2018/04/daily-chart-1。我想知道如何使用 ggplot2 对象来做到这一点。下面是一些代码,用于制作折线图,其中包含除标题中的彩色单词之外的所有内容,如经济学家文章。在底部我显示了所需的输出。
这个问题不是关于显示此信息的理论方法(例如直接标记或图例),而是关于为标题中的单个单词着色。
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'))