如何将图例添加到 geom_vline?
How to add legened to geom_vline?
我有一个数据框 (df_new),看起来像这样:
date<- c("2020-01-01", "2020-01-02", "2020-01-03", "2020-01-04", "2020-01-05")
A <- c(23, 41, 32, 58, 26)
B <- c(10, 20, 30, 40, 50)
df_new <- data.frame(date, A, B)
df_new$date <- as.Date(df_new$date)
我正在使用以下代码绘制值 A 和 B.
ggplot(data = df_new, aes(x = date)) +
geom_line(aes(y = A, colour = "Value A")) +
geom_line(aes(y = B, colour = "Value B"))+
labs(title = 'A & B Distribution',
x = '',
y = 'Value Count',
color = " ") +
theme_bw() +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x)))+
geom_vline(xintercept = as.numeric(as.Date("2020-01-03")), linetype=4)+
theme(text=element_text(size=13),panel.spacing.x=unit(0.6, "lines"),
panel.spacing.y=unit(1, "lines"))
它给了我想要的情节 除了我想在我手动设置的值 B 下添加 geom_vline 的图例画了一条黑线。
有什么指导吗?
您可以使用 scale_color_manual
手动设置带颜色的图例。您将 vline
的 color
命名为黑色,您可以通过 "black"="black"
将其指定为图例中的值。这可以手动更改为您想要的任何内容。您可以使用以下代码:
library(tidyverse)
library(scales)
ggplot(data = df_new, aes(x = date)) +
geom_line(aes(y = A, colour = "Value A")) +
geom_line(aes(y = B, colour = "Value B"))+
labs(title = 'A & B Distribution',
x = '',
y = 'Value Count',
color = " ") +
theme_bw() +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x)))+
geom_vline(xintercept = as.numeric(as.Date("2020-01-03")), linetype=4, color = "black")+
theme(text=element_text(size=13),panel.spacing.x=unit(0.6, "lines"),
panel.spacing.y=unit(1, "lines")) +
scale_color_manual(name = "Legend", values = c("Value A" = "red", "Value B" = "blue", "black" = "black"))
输出:
我有一个数据框 (df_new),看起来像这样:
date<- c("2020-01-01", "2020-01-02", "2020-01-03", "2020-01-04", "2020-01-05")
A <- c(23, 41, 32, 58, 26)
B <- c(10, 20, 30, 40, 50)
df_new <- data.frame(date, A, B)
df_new$date <- as.Date(df_new$date)
我正在使用以下代码绘制值 A 和 B.
ggplot(data = df_new, aes(x = date)) +
geom_line(aes(y = A, colour = "Value A")) +
geom_line(aes(y = B, colour = "Value B"))+
labs(title = 'A & B Distribution',
x = '',
y = 'Value Count',
color = " ") +
theme_bw() +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x)))+
geom_vline(xintercept = as.numeric(as.Date("2020-01-03")), linetype=4)+
theme(text=element_text(size=13),panel.spacing.x=unit(0.6, "lines"),
panel.spacing.y=unit(1, "lines"))
它给了我想要的情节
有什么指导吗?
您可以使用 scale_color_manual
手动设置带颜色的图例。您将 vline
的 color
命名为黑色,您可以通过 "black"="black"
将其指定为图例中的值。这可以手动更改为您想要的任何内容。您可以使用以下代码:
library(tidyverse)
library(scales)
ggplot(data = df_new, aes(x = date)) +
geom_line(aes(y = A, colour = "Value A")) +
geom_line(aes(y = B, colour = "Value B"))+
labs(title = 'A & B Distribution',
x = '',
y = 'Value Count',
color = " ") +
theme_bw() +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x)))+
geom_vline(xintercept = as.numeric(as.Date("2020-01-03")), linetype=4, color = "black")+
theme(text=element_text(size=13),panel.spacing.x=unit(0.6, "lines"),
panel.spacing.y=unit(1, "lines")) +
scale_color_manual(name = "Legend", values = c("Value A" = "red", "Value B" = "blue", "black" = "black"))
输出: