Rnovice
Rnovice

Reputation: 119

Changing a line to dashed in the legend of my ggplot2

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

Upvotes: 1

Views: 530

Answers (2)

Piranha
Piranha

Reputation: 116

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

Upvotes: 2

dc37
dc37

Reputation: 16178

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

Upvotes: 1

Related Questions