"Labelling" ggplot2 中的刻度线?
"Labelling" the tick marks in ggplot2?
我想制作一个情节来帮助人们理解 ggplot2
中情节的不同部分。我还在草稿中,但我目前正在努力标记 axis.ticks
因为它们不在情节中。直觉上我认为我可以用 cowplot::add_sub
做到这一点,但我遇到了一些麻烦。
library(ggplot2)
library(tibble)
library(ggdark)
library(ggrepel)
library(cowplot)
green = "#32CD32"
blue = "#49f9fe"
pink = "#E64E8D"
label_df <-
tribble( ~x, ~y, ~label,
300, 7.2, "panel.grid.major",
275, 6.7, "panel.grid.minor",
-100, 6.3, "axis.ticks")
x <-
mtcars %>%
ggplot(aes(hp, cyl)) +
geom_point(alpha = 0) +
dark_theme_light() +
geom_text_repel(aes(x, y, label = label), data = label_df[1,], nudge_x = -20, nudge_y = .5, col = pink) +
geom_text_repel(aes(x, y, label = label), data = label_df[2,], nudge_x = 20, nudge_y = .2, col = pink) +
#geom_text_repel(aes(x, y, label = label), data = label_df[3,], nudge_x = 20, nudge_y = .2) +
theme(panel.grid.major = element_line(colour = green, size = 1),
panel.grid.minor = element_line(colour = green),
axis.ticks = element_line(colour = green),
#axis.title = element_text(colour = blue),
axis.text = element_text(colour = blue)
) +
labs(x = "scale_x_continuous",
subtitle = "* disclaimer: all colors were adjusted using `theme()`",
y = "scale_y_continuous",
title = "Learn ggplot2 from ggplot2")
# + scale_x_continuous(breaks = seq(50, 300, by = 50), labels = c("control", "axis", "labels", "with", "'labels'", "argument")) +
# scale_y_continuous(labels = rev(c("control", "frequency", "with", "'breaks'", "argument")))
p <- add_sub(plot = x, label = "sss", x = 150, y = 6.5, colour = "black", size = 13)
ggdraw(p)
您可以将 x 设置为 -Inf 以指向 y 轴刻度或将 y = -Inf 设置为指向 x 轴刻度。
label_df <-
tribble( ~x, ~y, ~label,
300, 7.2, "panel.grid.major",
275, 6.7, "panel.grid.minor",
-Inf, 6.3, "axis.ticks")
我想制作一个情节来帮助人们理解 ggplot2
中情节的不同部分。我还在草稿中,但我目前正在努力标记 axis.ticks
因为它们不在情节中。直觉上我认为我可以用 cowplot::add_sub
做到这一点,但我遇到了一些麻烦。
library(ggplot2)
library(tibble)
library(ggdark)
library(ggrepel)
library(cowplot)
green = "#32CD32"
blue = "#49f9fe"
pink = "#E64E8D"
label_df <-
tribble( ~x, ~y, ~label,
300, 7.2, "panel.grid.major",
275, 6.7, "panel.grid.minor",
-100, 6.3, "axis.ticks")
x <-
mtcars %>%
ggplot(aes(hp, cyl)) +
geom_point(alpha = 0) +
dark_theme_light() +
geom_text_repel(aes(x, y, label = label), data = label_df[1,], nudge_x = -20, nudge_y = .5, col = pink) +
geom_text_repel(aes(x, y, label = label), data = label_df[2,], nudge_x = 20, nudge_y = .2, col = pink) +
#geom_text_repel(aes(x, y, label = label), data = label_df[3,], nudge_x = 20, nudge_y = .2) +
theme(panel.grid.major = element_line(colour = green, size = 1),
panel.grid.minor = element_line(colour = green),
axis.ticks = element_line(colour = green),
#axis.title = element_text(colour = blue),
axis.text = element_text(colour = blue)
) +
labs(x = "scale_x_continuous",
subtitle = "* disclaimer: all colors were adjusted using `theme()`",
y = "scale_y_continuous",
title = "Learn ggplot2 from ggplot2")
# + scale_x_continuous(breaks = seq(50, 300, by = 50), labels = c("control", "axis", "labels", "with", "'labels'", "argument")) +
# scale_y_continuous(labels = rev(c("control", "frequency", "with", "'breaks'", "argument")))
p <- add_sub(plot = x, label = "sss", x = 150, y = 6.5, colour = "black", size = 13)
ggdraw(p)
您可以将 x 设置为 -Inf 以指向 y 轴刻度或将 y = -Inf 设置为指向 x 轴刻度。
label_df <-
tribble( ~x, ~y, ~label,
300, 7.2, "panel.grid.major",
275, 6.7, "panel.grid.minor",
-Inf, 6.3, "axis.ticks")