R:将自定义图例添加到 ggplot
R: add custom legend to ggplot
我有以下情节:
并想补充一个图例如下:
这是我用来生成情节的代码:
library(data.table)
library(ggplot2)
blue <- "#4472C4"
green <- "#548235"
red <- "#C55A11"
redblood <- "#C00000"
DT <- data.table(student = c("Jane", "Sam", "Tim", "Kate", "Claire"),
grade = c(10, 14, 8, 9, 19))
b0 <- 13
DT[, gradeHat := b0]
DT[, e := grade - gradeHat]
DT[, SS := sum(e**2)]
DT[, id := 1:nrow(DT)]
DT[, xmin := id]
DT[, xmax := id + abs(e)/20*3]
DT[, ymin := min(grade, gradeHat), id]
DT[, ymax := max(grade, gradeHat), id]
DT[, student := factor(student, levels = student)]
gg <- ggplot(DT) +
geom_segment(aes(x = student, xend = student, y = grade, yend = gradeHat),
color = redblood, size = 1.3) +
geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax),
fill = redblood, alpha = .4) +
geom_hline(yintercept = b0, color = green, alpha = .7, size = 1, linetype = "dashed") +
geom_point(aes(student, grade), color = blue, size = 4) +
geom_point(aes(student, gradeHat), color = green, size = 4) +
scale_y_continuous(breaks = 0:20, limits = c(0, 20)) +
coord_fixed(.15) +
theme_classic()
plot(gg)
是的(几乎)可能:
gg <- ggplot(DT) +
geom_segment(aes(x = student, xend = student, y = grade, yend = gradeHat,
color = "Error"), size = 1.3) +
geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = "SS"),
alpha = .4) +
geom_hline(aes(alpha = "Grade", yintercept = 13), linetype = 2,
color = green, size = 1, key_glyph = "pointrange") +
geom_point(aes(student, grade, shape = "Grade"), color = blue, size = 4) +
geom_point(aes(student, gradeHat), color = green, size = 4) +
scale_y_continuous(breaks = 0:20, limits = c(0, 20)) +
scale_shape_manual(values = 19, name = NULL, guide = guide_legend(order = 1)) +
scale_color_manual(values = redblood, name = NULL,
guide = guide_legend(order = 3)) +
scale_alpha_manual(breaks = "Grade", values = 1,
guide = guide_legend(order = 2,
override.aes = list(color = green)),
name = "") +
scale_fill_manual(values = redblood, name = NULL) +
coord_fixed(.15) +
theme_classic() +
theme(legend.position = "bottom",
legend.key.width = unit(20, "points"))
plot(gg)
不是您问题的完整解决方案...但我目前能想到的最好的解决方案:
要在 ggplot 中获得图例,您必须在美学上进行映射,即不是将颜色设置为参数,而是必须在 color
或 fill
上映射 [=13] =].为此,您可以使用占位符或标签,例如fill="SS"
在 geom_rect
.
要获得正确的颜色,您可以使用 scale_color/fill_manual
。使用标签可以轻松分配正确的颜色。
接下来,要获得图例的正确样式,您可以使用 guide_legend
及其参数 override.aes
来设置颜色图例的形状和线型。
默认情况下 color
和 fill
图例之间有一些间距,但是可以在 theme()
中通过 legend.spacing
和 legend.margin
.
最后一部分是为图例标签着色。这可以通过 ggtext
包实现,该包通过主题选项 legend.text = element_markdown()
允许使用 HTML 和 CSS.
设置图例条目的样式
不幸的是,我无法弄清楚如何在您的 gradeHat
上戴上宽帽。
library(data.table)
library(ggplot2)
blue <- "#4472C4"
green <- "#548235"
red <- "#C55A11"
redblood <- "#C00000"
DT <- data.table(student = c("Jane", "Sam", "Tim", "Kate", "Claire"),
grade = c(10, 14, 8, 9, 19))
b0 <- 13
DT[, gradeHat := b0]
DT[, e := grade - gradeHat]
DT[, SS := sum(e**2)]
DT[, id := 1:nrow(DT)]
DT[, xmin := id]
DT[, xmax := id + abs(e)/20*3]
DT[, ymin := min(grade, gradeHat), id]
DT[, ymax := max(grade, gradeHat), id]
DT[, student := factor(student, levels = student)]
ggplot(DT) +
geom_segment(aes(x = student, xend = student, y = grade, yend = gradeHat, color = "error"),
, size = 1.3) +
geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = "SS"), alpha = .4) +
geom_hline(yintercept = b0, color = green, alpha = .7, size = 1, linetype = "dashed") +
geom_point(aes(student, grade, color = "grade"), size = 4) +
geom_point(aes(student, gradeHat, color = "gradeHat"), size = 4) +
scale_color_manual(breaks = c("grade", "gradeHat", "error"),
values = c(grade = blue, gradeHat = green, error = red),
labels = c(grade = glue::glue("<span style = 'color: {blue};'>grade</span>"),
gradeHat = glue::glue("<span style = 'color: {green};'>gradeHat</span>"),
error = glue::glue("<span style = 'color: {red};'>error</span>"))) +
scale_fill_manual(values = c(SS = redblood), labels = c(SS = glue::glue("<span style = 'color: #C0000066; '>SS</span>"))) +
guides(color = guide_legend(order = 1, override.aes = list(shape = c(16, 16, NA), linetype = c("blank", "dashed", "solid")))) +
scale_y_continuous(breaks = 0:20, limits = c(0, 20)) +
coord_fixed(.15) +
theme_classic() +
theme(legend.position = "bottom",
legend.spacing = unit(0, "pt"),
legend.margin = margin(r = 0, l = 0),
legend.text = ggtext::element_markdown()) +
labs(color = NULL, fill = NULL)
我有以下情节:
并想补充一个图例如下:
这是我用来生成情节的代码:
library(data.table)
library(ggplot2)
blue <- "#4472C4"
green <- "#548235"
red <- "#C55A11"
redblood <- "#C00000"
DT <- data.table(student = c("Jane", "Sam", "Tim", "Kate", "Claire"),
grade = c(10, 14, 8, 9, 19))
b0 <- 13
DT[, gradeHat := b0]
DT[, e := grade - gradeHat]
DT[, SS := sum(e**2)]
DT[, id := 1:nrow(DT)]
DT[, xmin := id]
DT[, xmax := id + abs(e)/20*3]
DT[, ymin := min(grade, gradeHat), id]
DT[, ymax := max(grade, gradeHat), id]
DT[, student := factor(student, levels = student)]
gg <- ggplot(DT) +
geom_segment(aes(x = student, xend = student, y = grade, yend = gradeHat),
color = redblood, size = 1.3) +
geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax),
fill = redblood, alpha = .4) +
geom_hline(yintercept = b0, color = green, alpha = .7, size = 1, linetype = "dashed") +
geom_point(aes(student, grade), color = blue, size = 4) +
geom_point(aes(student, gradeHat), color = green, size = 4) +
scale_y_continuous(breaks = 0:20, limits = c(0, 20)) +
coord_fixed(.15) +
theme_classic()
plot(gg)
是的(几乎)可能:
gg <- ggplot(DT) +
geom_segment(aes(x = student, xend = student, y = grade, yend = gradeHat,
color = "Error"), size = 1.3) +
geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = "SS"),
alpha = .4) +
geom_hline(aes(alpha = "Grade", yintercept = 13), linetype = 2,
color = green, size = 1, key_glyph = "pointrange") +
geom_point(aes(student, grade, shape = "Grade"), color = blue, size = 4) +
geom_point(aes(student, gradeHat), color = green, size = 4) +
scale_y_continuous(breaks = 0:20, limits = c(0, 20)) +
scale_shape_manual(values = 19, name = NULL, guide = guide_legend(order = 1)) +
scale_color_manual(values = redblood, name = NULL,
guide = guide_legend(order = 3)) +
scale_alpha_manual(breaks = "Grade", values = 1,
guide = guide_legend(order = 2,
override.aes = list(color = green)),
name = "") +
scale_fill_manual(values = redblood, name = NULL) +
coord_fixed(.15) +
theme_classic() +
theme(legend.position = "bottom",
legend.key.width = unit(20, "points"))
plot(gg)
不是您问题的完整解决方案...但我目前能想到的最好的解决方案:
要在 ggplot 中获得图例,您必须在美学上进行映射,即不是将颜色设置为参数,而是必须在
color
或fill
上映射 [=13] =].为此,您可以使用占位符或标签,例如fill="SS"
在geom_rect
.要获得正确的颜色,您可以使用
scale_color/fill_manual
。使用标签可以轻松分配正确的颜色。接下来,要获得图例的正确样式,您可以使用
guide_legend
及其参数override.aes
来设置颜色图例的形状和线型。默认情况下
color
和fill
图例之间有一些间距,但是可以在theme()
中通过legend.spacing
和legend.margin
.最后一部分是为图例标签着色。这可以通过
设置图例条目的样式ggtext
包实现,该包通过主题选项legend.text = element_markdown()
允许使用 HTML 和 CSS.
不幸的是,我无法弄清楚如何在您的 gradeHat
上戴上宽帽。
library(data.table)
library(ggplot2)
blue <- "#4472C4"
green <- "#548235"
red <- "#C55A11"
redblood <- "#C00000"
DT <- data.table(student = c("Jane", "Sam", "Tim", "Kate", "Claire"),
grade = c(10, 14, 8, 9, 19))
b0 <- 13
DT[, gradeHat := b0]
DT[, e := grade - gradeHat]
DT[, SS := sum(e**2)]
DT[, id := 1:nrow(DT)]
DT[, xmin := id]
DT[, xmax := id + abs(e)/20*3]
DT[, ymin := min(grade, gradeHat), id]
DT[, ymax := max(grade, gradeHat), id]
DT[, student := factor(student, levels = student)]
ggplot(DT) +
geom_segment(aes(x = student, xend = student, y = grade, yend = gradeHat, color = "error"),
, size = 1.3) +
geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = "SS"), alpha = .4) +
geom_hline(yintercept = b0, color = green, alpha = .7, size = 1, linetype = "dashed") +
geom_point(aes(student, grade, color = "grade"), size = 4) +
geom_point(aes(student, gradeHat, color = "gradeHat"), size = 4) +
scale_color_manual(breaks = c("grade", "gradeHat", "error"),
values = c(grade = blue, gradeHat = green, error = red),
labels = c(grade = glue::glue("<span style = 'color: {blue};'>grade</span>"),
gradeHat = glue::glue("<span style = 'color: {green};'>gradeHat</span>"),
error = glue::glue("<span style = 'color: {red};'>error</span>"))) +
scale_fill_manual(values = c(SS = redblood), labels = c(SS = glue::glue("<span style = 'color: #C0000066; '>SS</span>"))) +
guides(color = guide_legend(order = 1, override.aes = list(shape = c(16, 16, NA), linetype = c("blank", "dashed", "solid")))) +
scale_y_continuous(breaks = 0:20, limits = c(0, 20)) +
coord_fixed(.15) +
theme_classic() +
theme(legend.position = "bottom",
legend.spacing = unit(0, "pt"),
legend.margin = margin(r = 0, l = 0),
legend.text = ggtext::element_markdown()) +
labs(color = NULL, fill = NULL)