【发布时间】:2018-09-18 23:46:27
【问题描述】:
我最近在《经济学人》上看到了一个折线图,其中title had colored words to match the colors of the groups used in the line chart。我想知道如何使用 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'))
【问题讨论】:
-
@Roland 据说它不再适合我了:
Error in set.gpar(x$gp) : argument must be a 'gpar' object -
好吧,如果提到的方法是唯一的方法,最好导出到
pdf并使用inkscape。 -
@TylerRinker 好的!在我开始回答之前,我在四处挖掘时没有找到那些。那我就关了……