Home > Mobile >  Make dotted geom_vline on legend
Make dotted geom_vline on legend

Time:05-13

I'm trying to make a legend for a series of plots. I only need the legend, not the actual graphs as I am creating them separately and have the correct formatting for these already. I will then add this universal legend.

Currently I have the code for 2 different color lines separated, but I'd ideally have one red straight line and one red dashed line. However, I can't make the line dashed on one of them and have both red. Currently the code has one as red and the other as purple. Both being straight lines.

This is what it looks like: enter image description here

# 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)

CodePudding user response:

Use scale_linetype_manual, and just have both lines colored red:

  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'))

enter image description here

To have additional labels :

 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))

enter image description here

  • Related