我想为一系列的情节创造一个传奇。我只需要图例,而不需要实际的图形,因为我正在分别创建它们,并且已经有了正确的格式。然后我将加入这个普遍的传说。
目前,我有两条不同颜色线的代码分开,但理想情况下,我会有一条红色直线和一条红色虚线。然而,我不能使线冲向其中一个,并有两个红色。目前代码有一个是红色的,另一个是紫色的。都是直线。
看上去是这样的:
# 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)
发布于 2022-05-12 12:49:33
使用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))
https://stackoverflow.com/questions/72215564
复制相似问题