How put geom_vline legend in a facet_wrap plot?

Hi community

I'm try to add the geom_vline() legend, the vertical line in the plot, but I didn't could.

Try for many way but was impossible. Maybe the problem is because I'm other DF for make this line.

If exits other form for make this plot is very well welcome. Because Im a little confused about this legend.

data: df_.xlsx - Google Sheets

library(readxl)
library(tidyverse)
library(hrbrthemes)
library(lubridate)

df_<- read.excell('path')

T1_data_nor7 <- structure(list(DATE = structure(c(19353, 19353, 19353, 19361, 19360), 
                                                class = "Date"), 
                               Treatment = c("T1", "T2", "T3", "T4", "T2"), 
                               Accession = c("BEAN2", "BEAN2", "BEAN2", "BEAN2", "BEAN1"),
                               Flower_number = c(1, 2, 1, 1, 3), agregar_vline = c(TRUE,  TRUE, TRUE, TRUE, TRUE)), 
                          class = c("grouped_df", "tbl_df", "tbl","data.frame"), row.names = c(NA, -5L),
                          groups = structure(list(DATE = structure(c(19353, 19353, 19353, 19360, 19361), class = "Date"), 
                                                  Accession = c("BEAN2", "BEAN2", "BEAN2", "BEAN1", "BEAN2"), 
                                                  Treatment = c("T1", "T2", "T3", "T2", "T4"), 
                                                  .rows = structure(list(1L, 2L, 3L, 5L, 4L), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"), 
                                             row.names = c(NA, -5L), .drop = TRUE))


ggplot(df_) +
  geom_line(aes(DATE, value, color = name, fill=Treatment))+
  theme_ipsum()+
  scale_color_manual(values = c('#FF3900','#1E11C4','#0BDA00'))+
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1,size = 10),
        axis.text.y = element_text(size = 9),
        panel.border = element_rect(colour = "#1D1504", fill = NA),
        panel.background = element_rect(fill = '#F9FBE6', colour = 'red'),
        legend.position = 'bottom')+
  scale_x_date(date_breaks = "2 day", date_minor_breaks = "2 day",
               date_labels = "%d-%b")+
  labs(title='BEANS Heat Accumulation',
       y='Heat Accumulation') +
  facet_wrap(.~Accession +Treatment, scales = 'free_y') +
  geom_vline(data =T1_data_nor7[T1_data_nor7$agregar_vline, ], 
             aes (xintercept = DATE) , linetype =1, size= 0.8,
             color='#D1008D')

The idea is add the line legend with this color '#D1008D'.

ggplot2 only generates legends for mapped variables, so whenever you want something to appear on a legend it has to be mapped to an aesthetic even if the mapping is made manually. You can add an arbitrary variable to your scale_color_manual() definition and then map that variable to the color aesthetic.

library(tidyverse)
library(lubridate)

url <- "https://docs.google.com/spreadsheets/d/1U0WSSld7MsF2DmUrwL6nUqq_Fu57uCsb/export?format=xlsx"

download.file(url, "data.xlsx")

df_ <- readxl::read_xlsx("data.xlsx")

T1_data_nor7 <- structure(list(DATE = structure(c(19353, 19353, 19353, 19361, 19360), 
                                                class = "Date"), 
                               Treatment = c("T1", "T2", "T3", "T4", "T2"), 
                               Accession = c("BEAN2", "BEAN2", "BEAN2", "BEAN2", "BEAN1"),
                               Flower_number = c(1, 2, 1, 1, 3), agregar_vline = c(TRUE,  TRUE, TRUE, TRUE, TRUE)), 
                          class = c("grouped_df", "tbl_df", "tbl","data.frame"), row.names = c(NA, -5L),
                          groups = structure(list(DATE = structure(c(19353, 19353, 19353, 19360, 19361), class = "Date"), 
                                                  Accession = c("BEAN2", "BEAN2", "BEAN2", "BEAN1", "BEAN2"), 
                                                  Treatment = c("T1", "T2", "T3", "T2", "T4"), 
                                                  .rows = structure(list(1L, 2L, 3L, 5L, 4L), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"), 
                                             row.names = c(NA, -5L), .drop = TRUE))

df_ %>% 
    mutate(DATE = as.Date(DATE)) %>% 
    ggplot() +
    geom_line(aes(DATE, value, color = name)) +
    # theme_ipsum()+
    scale_color_manual(values = c("type_b" = '#FF3900', "type_c" = '#1E11C4',
                                  "type_d" = '#0BDA00', "FLOR" = '#D1008D')) +
    theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1,size = 10),
          axis.text.y = element_text(size = 9),
          panel.border = element_rect(colour = "#1D1504", fill = NA),
          panel.background = element_rect(fill = '#F9FBE6', colour = 'red'),
          legend.position = 'bottom') +
    scale_x_date(date_breaks = "2 day", date_minor_breaks = "2 day",
                 date_labels = "%d-%b") +
    labs(title='BEANS Heat Accumulation',
         y='Heat Accumulation') +
    facet_wrap(.~Accession +Treatment, scales = 'free_y') +
    geom_vline(data =T1_data_nor7[T1_data_nor7$agregar_vline, ], 
               aes (xintercept = DATE, color = "FLOR") , linetype =1, size= 0.8)

Created on 2023-02-22 with reprex v2.0.2

NOTE: Please try to provide minimal and self-contained reproducible examples, even if the sample data comes from a link, you should include code in your reprex to download and read that file (like I did with your link).

Tnks @andresrcs for the fast response.
I will take your recommendations into account. I have never really worked with files from drive. But I know how to do it next time.
thanks

You know how to put lines in the legend box, for edit + and put -.

Use the key_glyph you prefer, draw_key_path gives you what you want I think

    geom_vline(data =T1_data_nor7[T1_data_nor7$agregar_vline, ], 
               aes (xintercept = DATE, color = "FLOR") , linetype =1, size= 0.8, key_glyph = draw_key_path)
1 Like

Again thank you very much!

Was the solution.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.