在图例上加点 geom_vline
Make dotted geom_vline on legend
我正在尝试为一系列情节制作传奇。 我只需要图例,而不是实际的图表,因为我正在单独创建它们并且已经为它们设置了正确的格式。然后我会添加这个通用图例。
目前我有分开的 2 条不同颜色线条的代码,但我最好有一条红色直线和一条红色虚线。但是,我不能在其中一个上划线并且都变成红色。目前代码有一个为红色,另一个为紫色。都是直线。
这是它的样子:
# libraries
library(readr) # for function file.path
library(dplyr)
library(ggplot2)
library(tidyverse)
library(cowplot)
library(gridExtra)
library(extrafont)
legend <- data %>%
ggplot(aes_string(x='DateTime', y='Rel_mAOD')) +
geom_line() +
theme_classic() +
geom_vline(aes(xintercept=as.POSIXct('2020-11-03 01:00:00'),
col='red'), size=2) +
geom_vline(aes(xintercept=as.POSIXct('2021-11-01 01:00:00'),
col='purple'),linetype=2, size=2, showguide=TRUE) +
scale_color_manual(
name=NULL,
values=c('red','purple'),
labels=c('Release', 'Main')) +
theme(legend.direction='horizontal',
legend.position='top',
legend.text=element_text(size=30, margin=margin(r=1, unit='inch')),
legend.spacing.x=unit(0.5, 'inch')) +
guides(fill=guide_legend(
keywidth=6,
keyheight=6,
default.unit='inch'))
my_legend<-get_legend(legend)
plot(my_legend)
使用scale_linetype_manual
,将两条线都涂成红色:
ggplot() +
theme_void() +
geom_vline(aes(xintercept=as.POSIXct('2020-11-03 01:00:00'),
linetype = '1', ), size=1.5, color = "red") +
geom_vline(aes(xintercept=as.POSIXct('2021-11-01 01:00:00'),
linetype = '2'), size=1.5, color = "red") +
scale_linetype_manual(name = NULL,
values = c(1, 2), labels = c('Release', 'Main')) +
theme(legend.direction='horizontal',
legend.position='top',
legend.text=element_text(size=40, margin=margin(r=1, unit='inch')),
legend.spacing.x=unit(0.5, 'inch'),
legend.box.margin = margin(50, 20, 50, 20))+
guides(fill=guide_legend(
keywidth=8,
keyheight=6,
default.unit='inch'))
要有额外的标签:
ggplot() +
theme_void() +
geom_vline(aes(xintercept=as.POSIXct('2020-11-03 01:00:00'),
linetype = '1', color = '1' ), size=1.5) +
geom_vline(aes(xintercept=as.POSIXct('2021-11-01 01:00:00'),
linetype = '2', color = '2'), size=1.5) +
geom_vline(aes(xintercept=as.POSIXct('2020-11-03 01:00:00'),
linetype = '3', color = '3'), size=1.5) +
geom_vline(aes(xintercept=as.POSIXct('2021-11-01 01:00:00'),
linetype = '4', color = '4'), size=1.5) +
scale_color_manual(values = c("red", "red", "blue", "purple"),
name = NULL,
labels = c('Release', 'Main', "Next", "Last")) +
scale_linetype_manual(name = NULL,
values = c(1, 2, 1, 1),
labels = c('Release', 'Main', "Next", "Last")) +
theme(legend.direction='horizontal',
legend.position='top',
legend.text=element_text(size=40, margin=margin(r=1, unit='inch')),
legend.spacing.x=unit(0.5, 'inch'),
legend.box.margin = margin(50, 20, 50, 20))
我正在尝试为一系列情节制作传奇。 我只需要图例,而不是实际的图表,因为我正在单独创建它们并且已经为它们设置了正确的格式。然后我会添加这个通用图例。
目前我有分开的 2 条不同颜色线条的代码,但我最好有一条红色直线和一条红色虚线。但是,我不能在其中一个上划线并且都变成红色。目前代码有一个为红色,另一个为紫色。都是直线。
这是它的样子:
# libraries
library(readr) # for function file.path
library(dplyr)
library(ggplot2)
library(tidyverse)
library(cowplot)
library(gridExtra)
library(extrafont)
legend <- data %>%
ggplot(aes_string(x='DateTime', y='Rel_mAOD')) +
geom_line() +
theme_classic() +
geom_vline(aes(xintercept=as.POSIXct('2020-11-03 01:00:00'),
col='red'), size=2) +
geom_vline(aes(xintercept=as.POSIXct('2021-11-01 01:00:00'),
col='purple'),linetype=2, size=2, showguide=TRUE) +
scale_color_manual(
name=NULL,
values=c('red','purple'),
labels=c('Release', 'Main')) +
theme(legend.direction='horizontal',
legend.position='top',
legend.text=element_text(size=30, margin=margin(r=1, unit='inch')),
legend.spacing.x=unit(0.5, 'inch')) +
guides(fill=guide_legend(
keywidth=6,
keyheight=6,
default.unit='inch'))
my_legend<-get_legend(legend)
plot(my_legend)
使用scale_linetype_manual
,将两条线都涂成红色:
ggplot() +
theme_void() +
geom_vline(aes(xintercept=as.POSIXct('2020-11-03 01:00:00'),
linetype = '1', ), size=1.5, color = "red") +
geom_vline(aes(xintercept=as.POSIXct('2021-11-01 01:00:00'),
linetype = '2'), size=1.5, color = "red") +
scale_linetype_manual(name = NULL,
values = c(1, 2), labels = c('Release', 'Main')) +
theme(legend.direction='horizontal',
legend.position='top',
legend.text=element_text(size=40, margin=margin(r=1, unit='inch')),
legend.spacing.x=unit(0.5, 'inch'),
legend.box.margin = margin(50, 20, 50, 20))+
guides(fill=guide_legend(
keywidth=8,
keyheight=6,
default.unit='inch'))
要有额外的标签:
ggplot() +
theme_void() +
geom_vline(aes(xintercept=as.POSIXct('2020-11-03 01:00:00'),
linetype = '1', color = '1' ), size=1.5) +
geom_vline(aes(xintercept=as.POSIXct('2021-11-01 01:00:00'),
linetype = '2', color = '2'), size=1.5) +
geom_vline(aes(xintercept=as.POSIXct('2020-11-03 01:00:00'),
linetype = '3', color = '3'), size=1.5) +
geom_vline(aes(xintercept=as.POSIXct('2021-11-01 01:00:00'),
linetype = '4', color = '4'), size=1.5) +
scale_color_manual(values = c("red", "red", "blue", "purple"),
name = NULL,
labels = c('Release', 'Main', "Next", "Last")) +
scale_linetype_manual(name = NULL,
values = c(1, 2, 1, 1),
labels = c('Release', 'Main', "Next", "Last")) +
theme(legend.direction='horizontal',
legend.position='top',
legend.text=element_text(size=40, margin=margin(r=1, unit='inch')),
legend.spacing.x=unit(0.5, 'inch'),
legend.box.margin = margin(50, 20, 50, 20))