JC3019
JC3019

Reputation: 383

Add a legend to time-series data ggplot

I asked a similar question earlier however, the previous code seems unable to solve my problem.

Specifically I want to add a legend to my plot below. I want the darkred line to be named as "Cluster 1", the steelblue line to named as "Cluster 2" and the black line to be labelled as "Cluster 3". I have provided my attempt below. I'd expect the code to only need a small change to it. The legend does not need a title but can be called "Legend" or "Key" if necessary.

mat1 <- data.frame(matrix(nrow =10, ncol =4))
colnames(mat1) = c("Date", "Cluster 1", "Cluster 2", "Cluster 3")
date.start = as.Date("2000-01-01")
date.end = as.Date("2000-10-01")
date = seq(from = date.start, to = date.end, by = "month")
mat1[,1] = date
mat1[,2:4] = rnorm(30,0,1)
mat1 %>% ggplot(aes(x= Date, group=1)) + 
  geom_line(aes(y = `Cluster 1`), color = "darkred", linetype = "solid", size=1) +
  geom_line(aes(y = `Cluster 2`), color="steelblue", linetype="twodash", size =1) + 
  geom_line(aes(y= `Cluster 3`), color = "black", linetype = "longdash", size = 1) +
  scale_colour_manual("Legend",
                      values = c("Cluster 1" = "darkred", "Cluster 2" ="steelblue", "Cluster 3" = "black"),
                      breaks = c("Cluster 1", "Cluster 2", "Cluster 3")) +
  scale_linetype_manual("Legend",
                        values = c("solid", "twodash", "longdash"),
                        breaks = c("Cluster 1", "Cluster 2", "Cluster 3")) +
  xlab("Date") + ylab("Average Coefficient") + ggtitle("HML Factor") + theme_classic() +
  theme(
    plot.title = element_text(color = "black", size = 12, face = "bold"),
    axis.title = element_text(size = 12)
  )

Upvotes: 1

Views: 250

Answers (1)

pwrignall
pwrignall

Reputation: 26

I would suggest adjusting your data structure first in order to achieve what you're looking to do.

You can do this with tidyr's gather function, and do it after your mat1[,2:4] = rnorm(30,0,1) line:

mat1 <- mat1 %>% 
  tidyr::gather(cluster, value, `Cluster 1`:`Cluster 3`)

Then add a single geom_line call to replace the three you currently have:

mat1 %>% ggplot(aes(x= Date, y = value)) + 
  # geom_line(aes(y = `Cluster 1`), color = "darkred", linetype = "solid", size=1) +
  # geom_line(aes(y = `Cluster 2`), color="steelblue", linetype="twodash", size =1) + 
  # geom_line(aes(y= `Cluster 3`), color = "black", linetype = "longdash", size = 1) +
  geom_line(aes(colour = cluster, linetype = cluster), size = 1) +
  scale_colour_manual(values = c("Cluster 1" = "darkred", "Cluster 2" ="steelblue", "Cluster 3" = "black")) +
  scale_linetype_manual(values = c("Cluster 1" = "solid", "Cluster 2" = "twodash", "Cluster 3" = "longdash")) +
  xlab("Date") + ylab("Average Coefficient") + ggtitle("HML Factor") +     theme_classic() +
  theme(
    plot.title = element_text(color = "black", size = 12, face = "bold"),
    axis.title = element_text(size = 12)
  )

Edit: added linetype to geom_line aesthetics

Upvotes: 1

Related Questions