我最近在"经济学人"中看到了一个折线图,其中标题的颜色与匹配折线图中使用的组的颜色相匹配.我想知道如何使用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 …Run Code Online (Sandbox Code Playgroud)