0
votes

I've got a dataframe that looks like:

df<-data.frame(Date=as.Date(c("06-08-10","06-09-10","06-10-10","06-11-10","06-13-10")),closed_this_year_cum_gv=c(3,5,6,7,NA),opened_this_year_cum_gv=c(2,5,6,8,10),closed_last_year_cum_gv=c(5,6,7,8,10),opened_last_year_cum_gv=c(5,6,8,10,NA))

and have this framework for a plot using ggplot2:

ggplot(df, aes(x=Date))+
  geom_line(aes(y=closed_this_year_cum_gv, color="blue"),linetype="dashed")+
  geom_line(aes(y=opened_this_year_cum_gv, color="blue"))+
  geom_line(aes(y=closed_last_year_cum_gv, color="red"),linetype="dashed")+
  geom_line(aes(y=opened_last_year_cum_gv, color="red"))+
  xlab("Date")+
  ylab("Millions of Dollars")+
  ggtitle("Cummulative Sum of TGV for Opened and Closed Cases - 2013 vs. 2012")

I tried this with the sample data but for some reason the lines aren't showing up (they're showing up with my real data). I want the NAs to not be graphed, which is why they aren't 0.

In my real data, it graphs, but the legend title has "blue" and it's contents are "blue" and "red" as labels. I want them to be labeled by year and opened/closed. I've tried various methods but nothing seems to override the legend.

How do I control the legend title and labels?

Edit: changed to class "Date"

2

2 Answers

0
votes

ggplot is generelly happier to be fed with data in 'long' format, as opposed to wide. Then it is, among other things, easier to map different aesstetics to variables in the data set.

# some data massage before the plot
# reshape data from wide to long format 
library(reshape2)
df2 <- melt(df)

# convert variable 'Date' to class 'Date'
df2$Date <- as.Date(df2$Date, format = "%m-%d-%y")

# create two variables
# var1: opened vs closed
df2$var1 <- ifelse(grepl(x = df2$variable, pattern = "opened"), "Opened", "Closed")

# set factor levels so that 'opened' comes before 'closed'
df2$var1 <- factor(df2$var1, levels = c("Opened", "Closed"))

# var2: this vs last year 
df2$var2 <- ifelse(grepl(x = df2$variable, pattern = "this"), "This year", "Last year")


# plot
# use default colours, slightly pale 'red' and 'blue'
ggplot(df2, aes(x = Date, y = value, linetype = var1, colour = var2, group = interaction(var1, var2))) +
  geom_line()

# if you want to set colours to red and blue, add this
+ scale_colour_manual(values = c("red", "blue"))

Update following comment
If you only want one legend, one possibility is to let linetype and colour to depend on 'variable'.

# set factor levels so that 'opened' comes before 'closed', and 'last' before 'this'
df2$variable <- factor(df2$variable,
                   levels = c("opened_last_year_cum_gv",
                              "closed_last_year_cum_gv",
                              "opened_this_year_cum_gv",
                              "closed_this_year_cum_gv")
                       )


ggplot(df2, aes(x = Date, y = value, linetype = variable, colour = variable, group = variable)) +
  geom_line() +
  scale_colour_manual(values = rep(c("red", "blue"), each = 2),
                      name = "",
                      labels =  c("Opened last year",
                                  "Closed last year",
                                  "Opened this year",
                                  "Closed this year")) +
  scale_linetype_manual(values = rep(c("solid", "dashed"), 2),
                        name = "",
                        labels =  c("Opened last year",
                                    "Closed last year",
                                    "Opened this year",
                                    "Closed this year"))
0
votes

You need to specify appropriate mappings in aes(). Try this:

ggplot(df, aes(x=Date)) +
  geom_line(aes(y=closed_this_year_cum_gv, color="this", linetype="closed")) +
  geom_line(aes(y=opened_this_year_cum_gv, color="this", linetype="opened")) +
  geom_line(aes(y=closed_last_year_cum_gv, color="last", linetype="closed")) +
  geom_line(aes(y=opened_last_year_cum_gv, color="last", linetype="opened")) +
  xlab("Date") +
  ylab("Millions of Dollars") +
  ggtitle("Cummulative Sum of TGV for Opened and Closed Cases - 2013 vs. 2012") +
  scale_colour_manual(name="year", values=c("this"="blue", "last"="red")) +
  scale_linetype_manual(name="type", values=c(2, 1))