ggplot2:为标题中的单个单词上色以匹配组的颜色 [英] ggplot2: color individual words in title to match colors of groups

查看:124
本文介绍了ggplot2:为标题中的单个单词上色以匹配组的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在《经济学人》上看到了一个折线图,其中标题带有颜色的字词以匹配折线图中使用的组的颜色.我想知道如何使用ggplot2对象执行此操作.这是一些代码,可以制作折线图,除了标题中的彩色文字外,还可以包含经济论文章等所有内容.在底部,我显示了所需的输出.

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.

这个问题不是关于显示此信息的理论方法(如直接标记或图例),而是关于为标题中的单个单词着色.

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'))

推荐答案

此解决方案基于为图表中的标题加上颜色(此处的贡献者致谢!).

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!).

通过对文本使用phantom占位符,我们避免了(大部分)位置的硬编码.

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"))

随着大量彩色单词的出现,不同的expression的创建应以编程方式完成.参见例如类似于base图的类似问题中的multiTitle函数:

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.

这篇关于ggplot2:为标题中的单个单词上色以匹配组的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆