Search code examples
rggplot2tidyverselegendribbon

ggplot2: show legend shape based on graph's type


I have the following tibble data:

d1<-structure(list(Date = structure(c(18413, 18414, 18415, 18416, 
  18417, 18418, 18419, 18420, 18421, 18422), class = "Date"), F = c(27240.15794, 
  28169.30859, 29175.23888, 30136.86232, 31124.0537, 32096.49071, 
  33077.44196, 34053.47994, 35032.35319, 36009.5903), L95 = c(27075.72202, 
  27869.77696, 28635.16841, 29389.18107, 30126.3899, 30842.16607, 
  31542.19984, 32223.65431, 32889.86495, 33539.92631), H95 = c(27404.59386, 
  28468.84022, 29715.30936, 30884.54358, 32121.71751, 33350.81536, 
  34612.68407, 35883.30556, 37174.84142, 38479.25429), A = c(27043L, 
  27762L, 28649L, 29359L, 29921L, 30644L, 31131L, 31848L, 32510L, 
  33140L)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", 
 "data.frame")) 

This is my code:

 colors <- c("A" = "black", "F" = "blue", "B" = "red")
 d1 %>% 
  ggplot(aes(x = Date, y = A, color="A")) +
  geom_point(shape=8,fill="blue", size=1.5) + 
  geom_line() + 
  geom_point(aes(x=Date, y = F,color="F"),shape=24,size=1.5) +
  geom_line() +
  geom_ribbon(aes(x = Date, y = F,ymax = L95,ymin = H95,color="B"),
              fill = "gray95", 
              alpha = 0.6,
              linetype=6,
              show.legend = TRUE)+
  theme_bw() +
  theme(legend.position = "bottom",
        #legend.box = "vertical", 
        legend.title=element_blank(),
        legend.box = NULL,
        legend.box.background = element_blank(),
        #legend.background = element_blank(),
        legend.key.size = unit(0.5, "cm"),
        legend.key.width = unit(0.5,"cm"),
        legend.direction = "horizontal",
        #legend.background = element_rect(color = "steelblue", linetype = "solid"),
        axis.text.x = element_text(color="#000000",
                                   size=10, 
                                   angle=45,
                                   hjust = 1),
        axis.text.y = element_text(color="#000000",
                                   size=10,
                                   angle=45,
                                   hjust = 1),
        axis.title.x =element_blank(),
        axis.title.y=element_text(size=14),
        panel.border = element_blank(), 
        #panel.grid.major = element_blank(),
        panel.background = element_rect(fill="#FFFFFF"),
        axis.line = element_line(colour = "black",
                                 size = 0.5, 
                                 linetype = "solid"),
        #legend.position = c(0, 160000),legend.justification = c(0, 1)
  ) + 
  
  #scale_shape_manual(values = colors)+
     scale_color_manual(values = colors)+
      scale_y_continuous("Test",expand = expansion(mult = c(0.01,0.03))) +
      scale_x_date(date_breaks = "1 week", date_minor_breaks = "1 week",
               date_labels = "%b/%d",
               expand = expansion(mult = c(0.01,0.03)))

The output:

enter image description here

I want to match the shape of the legend with the shape in the graph and get rid off the small boxes around each legend. Also, for some reason after I used some of the recommended legend customizations commands, the connected line of the F data changed to be points ONLY.

I found some suggestions from the previous questions to melt the data ( using reshape2 package). I did try it but I couldn't get the same ribbon graph effects especially the filled area between L95 and H95. Therefore, I eliminate this suggestion. Another suggestion was to create a list of shapes as variables same as in my code (for colors) and then use scale_shape_manual. I tried but I didn't get any changes in the results. I'm not sure what I'm missing. Any suggestions?


Solution

  • Try this reshaping data to long to avoid potential pitfalls:

    library(dplyr)
    library(tidyr)
    library(ggplot2)
    #Inputs
    colors <- c("A" = "black", "F" = "blue", "B" = "red")
    #Code
    d1 %>%
      pivot_longer(-c(Date,L95,H95)) %>%
      ggplot(aes(x = Date, y = value, color=name)) +
      geom_point(aes(shape=name), size=1.5) + 
      geom_line()+
      geom_ribbon(aes(x = Date,ymax = L95,ymin = H95,color="B",shape='B'),
                  fill = "gray95", 
                  alpha = 0.6,
                  linetype=6,
                  show.legend = F)+
      scale_shape_manual('Var',values = c('A'=8,'F'=24,'B'=1))+
      scale_color_manual('Var',values = colors)+
      scale_y_continuous("Test",expand = expansion(mult = c(0.01,0.03))) +
      scale_x_date(date_breaks = "1 week", date_minor_breaks = "1 week",
                   date_labels = "%b/%d",
                   expand = expansion(mult = c(0.01,0.03)))+
      theme_bw() +
      theme(legend.position = "bottom",
            legend.title=element_blank(),
            legend.box = NULL,
            legend.box.background = element_blank(),
            legend.key.size = unit(0.5, "cm"),
            legend.key.width = unit(0.5,"cm"),
            legend.direction = "horizontal",
            axis.text.x = element_text(color="#000000",
                                       size=10, 
                                       angle=45,
                                       hjust = 1),
            axis.text.y = element_text(color="#000000",
                                       size=10,
                                       angle=45,
                                       hjust = 1),
            axis.title.x =element_blank(),
            axis.title.y=element_text(size=14),
            panel.border = element_blank(), 
            panel.background = element_rect(fill="#FFFFFF"),
            axis.line = element_line(colour = "black",
                                     size = 0.5, 
                                     linetype = "solid"),
            legend.key = element_rect(colour = NA, fill = NA)
      )+
      guides(color = guide_legend(override.aes = list(shape = c(8,NA,24))))
    

    Output:

    enter image description here