1

I am struggling to add a dashed line to my legend. I include the linetype = 2 in my geom line for the value-weighted line however, the dashed rendition of the line does not show in the legend. I am wondering if this is possible with just a minor addition to the ggplot code. Thanks for the help.

ggplot(data, aes(x=Date)) + 
  geom_line(aes(y=data$`Equal-Weighted`, col="Equal-Weighted")) + 
  geom_line(aes(y=data$`Value-Weighted`, col="Value-Weighted"), linetype = 2) + 
  labs(title=, 
       subtitle=, 
       caption="", y="", x = "Year") +  # title and caption
  scale_x_date(labels = lbls, breaks = brks) +  # change to monthly ticks and labels
  scale_color_manual(name="", 
                     values = c("Equal-Weighted"="#0000FF", "Value-Weighted"="#FF0000")) +  # line color
  theme(panel.grid.minor = element_blank()) +  # turn off minor grid
  theme(axis.text.x = element_text(angle = 90, vjust=0.5, size = 8),  # rotate x axis text
        panel.grid.minor = element_blank()) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, NA)) 
Rnovice
  • 119
  • 7
  • Can you provide a reproducible example of your dataset as described in this pot: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example ? – dc37 Apr 27 '20 at 17:27
  • Unfortunately, I am unable to right now – Rnovice Apr 27 '20 at 17:33

2 Answers2

2

To elaborate a bit on the answer from @dc37, I think that the critical step is to change the shape of your data to a "long" format, rather than "wide" format. It is hard without your actual data, but I am guessing you are working with wide format data here.

A couple of sample charts might make this point clearer:

library(tidyverse)
# the latest version of tidyverse will include the "pivot_longer" function from
# tidyr package

# providing some sample data to work with
df = tibble(Date = seq(as.Date("2018-01-01"), by = "year", length.out = 3),
            Equal_Weighted = c(100, 200, 300),
            Value_Weighted = c(100, 250, 400))
df

# changing the shape of the sample data
df_long <- df %>% 
  pivot_longer(-Date, names_to="Variable", values_to="Value")
df_long


# example chart 1 - I don't think you can get a legend if you have multiple
# calls for geom_line, as shown below
df_chart_1 <- df %>% 
  ggplot(aes(x=Date)) + 
  geom_line(aes(y=Equal_Weighted), linetype = 1) +
  geom_line(aes(y=Value_Weighted), linetype = 2) +
  labs(title="Chart with Fake Data", 
       subtitle="Sample 1; Based on Wide Format Data", 
       caption="", y="", x = "Year") +
  scale_x_date(date_labels = "%Y", breaks = df$Date) +
  theme(panel.grid.minor = element_blank()) + 
  theme(axis.text.x = element_text(angle = 90, vjust=0.5, size = 8),
        panel.grid.minor = element_blank()) 
df_chart_1

# Long format data, with legend
# note that including "linetype=" inside the aes of geom_line automatically creates 
# the legend
df_chart_2 <- df_long %>% 
  ggplot(aes(x=Date, y=Value)) + 
  geom_line(aes(linetype=Variable)) +
  labs(title="Chart With Fake Data", 
       subtitle="Sample 2: Based on Long Format Data", 
       caption="", y="", x = "Year") +
  scale_x_date(date_labels = "%Y-%b", breaks = df$Date) +
  theme(panel.grid.minor = element_blank()) + 
  theme(axis.text.x = element_text(angle = 90, vjust=0.5, size = 8),
        panel.grid.minor = element_blank()) 
df_chart_2

Chart with Long Format Data

Piranha
  • 116
  • 6
1

Without data, it is difficult to be sure of what would be the possible answer to your question, however, based on your code, you can try:

library(tidyr)
library(dplyr)
library(ggplot2)

data %>% pivot_longer(cols = `Equal-Weighted`:`Value-Weighted`, names_to = "var", values_to = "val") %>%
  ggplot(aes(x = Date, y = val, color = var, group = var, linetype = var))+
  geom_line()+
  scale_color_manual(name="", values = c("#0000FF", "#FF0000"))+
  scale_linetype_manual(name = "", values = c(2,4))+
  scale_x_date(labels = lbls, breaks = brks) +  # change to monthly ticks and labels+
  labs(title=, 
       subtitle=, 
       caption="", y="", x = "Year") +  # title and caption
  theme(panel.grid.minor = element_blank()) +  # turn off minor grid
  theme(axis.text.x = element_text(angle = 90, vjust=0.5, size = 8),  # rotate x axis text
        panel.grid.minor = element_blank()) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, NA)) 

Does it answer your question ?

If not, please provide a reproducible example of your dataset by reading this post: How to make a great R reproducible example

dc37
  • 15,840
  • 4
  • 15
  • 32
  • hi, thanks for trying anyway. my r could not find the pivot_longer function. – Rnovice Apr 27 '20 at 17:50
  • Did you load `tidyr` library ? If so, which version is it ? `pivot_longer` has been introduced in replacement of `gather` in `tidyr 1.0.0` – dc37 Apr 27 '20 at 17:51
  • great. so i updated the package. i'm getting the following error unfortunately, Error in grid.Call.graphics(C_lines, x$x, x$y, index, x$arrow) : invalid line type: must be length 2, 4, 6 or 8 In addition: Warning message: Removed 240 rows containing missing values (geom_path). – Rnovice Apr 27 '20 at 17:58
  • https://stackoverflow.com/questions/14875582/changing-the-line-type-in-the-ggplot-legend also helped. – Rnovice Apr 27 '20 at 18:08
  • Glad you figure it out the solution to your issue ;) – dc37 Apr 27 '20 at 18:13
  • For your error, you have to replace values in `scale_linetype_manual` by `values = c(2,4)`. I edited my answer for that. – dc37 Apr 27 '20 at 18:14