我正在嘗試為一系列情節創造傳奇。我只需要圖例,而不是實際的圖表,因為我正在單獨創建它們并且已經為它們設定了正確的格式。然后我將添加這個普遍的傳說。
目前,我將 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)
uj5u.com熱心網友回復:
使用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://www.uj5u.com/yidong/474405.html
上一篇:如何組合分組條形圖并使它們像Marimekko圖表?
下一篇:如何制作geom_col
