我最近在"经济学人"中看到了一个折线图,其中标题的颜色与匹配折线图中使用的组的颜色相匹配.我想知道如何使用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'))
Run Code Online (Sandbox Code Playgroud)
该解决方案是基于显示由GGPLOT2产生的图下方的文字并在情节的称号彩色化的部分(学分贡献者那里!).
通过使用phantom
文本的占位符,我们避免(大多数)位置的硬编码.
# create text grobs, one for each color
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
p <- 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) +
theme_minimal() +
theme(legend.position = 'none',
# add some extra margin on top
plot.margin = unit(c(4, 1, 1, 1), "lines")) +
scale_color_manual(values = c("#EEB422", "#238E68")) +
annotation_custom(grobTree(t1, t2, t3))
# create gtable and remove clipping
g <- ggplot_gtable(ggplot_build(p))
g$layout$clip[g$layout$name == "panel"] <- "off"
# re-draw
grid.draw(g)
Run Code Online (Sandbox Code Playgroud)
使用大量彩色单词时,expression
应以编程方式更多地创建不同的s.例如,multiTitle
在类似问题的base
情节中看到好的函数:标题:不同颜色的单词?,这也应该是有用的ggplot
.
这是使用ggtext
包的简单且更通用的方法
生产:
library(ggtext)
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point(size = 3) +
scale_color_manual(
name = NULL,
values = c(setosa = "#0072B2", virginica = "#009E73", versicolor = "#D55E00"),
labels = c(
setosa = "<i style='color:#0072B2'>I. setosa</i>",
virginica = "<i style='color:#009E73'>I. virginica</i>",
versicolor = "<i style='color:#D55E00'>I. versicolor</i>")
) +
labs(
title = "**Fisher's *Iris* dataset**
<span style='font-size:11pt'>Sepal width vs. sepal length for
<span style='color:#0072B2;'>setosa</span>,
<span style='color:#D55E00;'>versicolor</span>, and
<span style='color:#009E73;'>virginica</span>
</span>",
x = "Sepal length (cm)", y = "Sepal width (cm)"
) +
theme_minimal() +
theme(
plot.title = element_markdown(lineheight = 1.1),
legend.text = element_markdown(size = 11)
)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1951 次 |
最近记录: |