Neicooo
Neicooo

Reputation: 197

Reflect line type in legend ggplot2

In my graph below I would like to have the legend reflect the actual line style i.e. the blue line for "Distance" should also be dashed in the legend. How do I achieve this? Below my code. Thank you!

data <- structure(list(Year = c(1980, 1981, 1982, 1983, 1984, 1985, 1986, 
1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 
1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 
2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016), Region = c(428, 
424, 424, 424, 424, 423, 423, 423, 423, 423, 423, 423, 423, 423, 
423, 427, 427, 429, 428, 428, 427, 427, 426, 429, 431, 429, 429, 
430, 430, 431, 433, 435, 434, 436, 434, 434, 432), Distance = c(45.3851417635712, 
45.1015566515805, 45.0949633134224, 44.9843501596268, 44.472001374814, 
44.4651674044446, 44.4572188553101, 44.2469544378131, 44.2469544378131, 
44.2469544378131, 44.2132321845356, 43.8753401319457, 42.6315852081922, 
42.0628336732444, 42.0628336732444, 41.8727936463309, 41.8727936463309, 
41.7635411376391, 41.7635411376391, 41.7635411376391, 41.7635411376391, 
41.7635411376391, 41.7635411376391, 41.6326581679158, 41.2193523001234, 
41.1576215657083, 41.1576215657083, 41.0389941582308, 40.8914901030284, 
40.8257256986156, 40.8257256986156, 40.670233047203, 40.670233047203, 
40.512318467256, 40.512318467256, 40.512318467256, 40.3362918835934
)), row.names = c(NA, -37L), class = c("tbl_df", "tbl", "data.frame"
))


library(ggplot2)
p <- ggplot(data, aes(x = Year))
p <- p + geom_line(aes(y = data$Region, color = "Region"), size=1.1)
p <- p + scale_x_continuous(breaks = seq(1980, 2015, by = 5))

# first axis
p <- p + geom_line(aes(y = data$Distance*10, color = "Distance"), size=1.1, linetype = "dashed")

# now adding the secondary axis
p <- p + scale_y_continuous(sec.axis = sec_axis(~./10, name = "Distance"))
p <- p + labs(y = "Region")



# modifying colours and theme options
p <- p + theme(legend.position = c(0.8, 0.9))
p <- p + scale_color_manual(values=c('#67a9cf','#ef8a62')) + theme(text=element_text(size=12,  family="serif"))
p <- p +  theme(legend.position="bottom") +  theme(legend.title=element_blank(), axis.text=element_text(size=11), axis.title=element_text(size=11), legend.text=element_text(size=10),panel.grid.minor = element_blank())
p <- p + annotate("rect", xmin=1988, xmax=1997, ymin=-Inf, ymax=Inf, alpha=0.1, fill="#999999") 
p <- p + annotate("text", x = 1992.25, y = 452, label = "xxx", family="serif")+ annotate("text", x = 1992, y = 450, label = "yyy", family="serif")
p <- p + theme(text=element_text(size=12,  family="serif"))
p <- p+ theme(panel.background = element_rect(fill = NA),
              panel.grid.major = element_line(colour = "gray95"), 
              panel.border = element_rect(colour = "gray20", fill=NA, size=0.5))
p

enter image description here

Upvotes: 2

Views: 99

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389135

You can include linetype in aes and assign type of linetype using scale_linetype_manual.

library(ggplot2)

ggplot(data, aes(x = Year)) + 
  geom_line(aes(y = Region, linetype = "Region", color = "Region"), size=1.1) + 
  scale_x_continuous(breaks = seq(1980, 2015, by = 5)) + 
  geom_line(aes(y = Distance*10, linetype = "Distance", color = "Distance"), size=1.1) + 
  scale_y_continuous(sec.axis = sec_axis(~./10, name = "Distance")) + 
  labs(y = "Region") + 
  scale_linetype_manual("colour",values=c("Distance"= "dotted", "Region" = "solid")) + 
  theme(legend.title=element_blank(), 
        legend.position="bottom")

enter image description here

Upvotes: 2

Related Questions