Xaviermoros
Xaviermoros

Reputation: 147

Adding a legend to ggplot2 facets with multiple lines?

I have got a problem with my ggplot for faceted multiple line plots for seven metropolitan regions in South Korea.

The structure of my csv dataset is just similar to a panel data with both cross-sectional and time series dimensions for cities over time.

Below is the format of my dataset:

Year   City    VKT index  GDP index 
2012   Seoul      100      100
2013   Seoul      94       105
2014   Seoul      96       110
..............................
2012   Busan      100      100
2013   Busan      97       105
..............................
2012   Daegu     100       100       
2013   Daegu     104       114

My code is also as follows:

deccity <- read_csv("decouplingbycity.csv")

deccity %>% filter(is.na(Year) == FALSE) %>%
ggplot(deccity, mapping = aes(x=Year)) +
  geom_line(size = 1, aes(y = `GDP index`), color = "darkred") +
  geom_line(size = 1,aes(y = `VKT index`), color="steelblue", linetype="twodash")+
  labs(y="Index: 1992=100",
       title = "Decoupling by city")+
  facet_wrap(~City)

You can see the plot I've got now. However there is a problem, and the obvious issue is that I can't see my legends for 'VKT index' and 'GDP index' variables. I would greatly appreciate if anyone could chime in and sort out another way to do this.

Kindly refer to my multi-panel plots without legend for deeper insights into what i'm looking for:

enter image description here

Upvotes: 1

Views: 2226

Answers (2)

Xaviermoros
Xaviermoros

Reputation: 147

This is how I've tweaked my code to get the multi-panel plots i wanted. Thanks to datanovia for the great reference materials! I've also included a legend.

dahir %>% filter(is.na(Year) == FALSE) %>% 
ggplot(dahir, mapping = aes(x = Year, y = value)) + 
  geom_line(size = 1.2, aes(color = variable, linetype = variable)) + 
  scale_color_manual(values = c("darkred", "steelblue"))+
  labs(y="Index: 2012=100",
       title = "Decoupling by City")+
  facet_wrap (~City)

Decoupling in metropolitan cities

Upvotes: 0

Paul
Paul

Reputation: 2977

My advice is to reshape your data in a "tidy" way, this will avoid you a lot of trouble in the future (not only with ggplot2). See this wonderful documentation.

Here the problem is not the facet_grid() function but the way to tell ggplot2 the data to include in the legend; this data has to bee inside aes().

As you do not provide a reproductible dataset, I use mtcars dataset that is included within RStudio. Just copy-paste the piece of code below and it will run.

# very usefull set of packages
library(tidyverse)

# here is what you are trying to do
ex_plot1 = ggplot(data = mtcars, aes(x = disp)) +
  geom_line(aes(y = mpg), color = "red") +
  geom_line(aes(y = qsec), color = "green")
plot(ex_plot1) # see there is no legend

# reshape your data this way:
ex_data2 = pivot_longer(data = mtcars, 
                        cols = c("mpg", "qsec"),
                        values_to = "values",
                        names_to = "colored_var")
# and then plot it, legend appears
ex_plot2 = ggplot(data = ex_data2, aes(x = disp, y = values, color = colored_var)) +
  geom_line()
plot(ex_plot2)

[EDIT] added the outputs

plot without legend, ex_plot1

plot without legend, ex_plot1

plot with legend, ex_plot2

plot with legend, ex_plot2

Upvotes: 3

Related Questions