'Controlling color=date in ggplot2

I am doing a simple enough line plot of before/after using the following code:

ggplot() +
  geom_line(data = preMax, aes(x=buffer, y=max, color=date, linetype= "dashed")) +
  geom_line(data = postMax, aes(x=buffer, y=max, color=date, linetype= "solid"))

This gets me the plot below:

Example plot

There are a few issues that I can't seem to work out.

  1. The line type legend is reversed
  2. The colour palette for the months is reversed (this is the priority)
  3. Labelling each line to the right of the plot (I appreciate this could get messy - these are just diagnostic plots)

Although I've found plenty of solutions for 3) I can't work out the syntax for my implementation.

I have no idea what to do for 1) and 2).

Extracts of pre/post dataframes (please excuse scruffy headers read from CSV import - 'Masked' column is redundant here):

> dput(preMax[sample(1:nrow(preMax), 20),])
structure(list(type = c("Masked", "Masked", "Masked", "Masked", 
"Masked", "Masked", "Masked", "Masked", "Masked", "Masked", "Masked", 
"Masked", "Masked", "Masked", "Masked", "Masked", "Masked", "Masked", 
"Masked", "Masked"), buffer = c(200, 1700, 600, 400, 900, 300, 
1300, 600, 1500, 100, 400, 1700, 2000, 1800, 300, 1000, 100, 
1000, 1900, 1800), max = c(44.864783808451, 45.9712411935412, 
17.5829020162918, 12.4868639407346, 49.6003614709968, 45.2344134295274, 
21.1563882993978, 10.5763618088791, 37.6644106683852, 11.057020709424, 
33.3232497692325, 20.7153387275033, 37.6645113431222, 49.9596451668152, 
11.0570186079038, 45.6029948612003, 21.156362982432, 37.6638859359373, 
20.2727947583503, 21.5957031868673), date = c("2011-07-13", "2011-07-13", 
"2010-02-16", "2010-12-01", "2010-08-11", "2011-07-13", "2010-11-15", 
"2010-01-15", "2011-04-24", "2010-01-15", "2011-10-17", "2010-03-20", 
"2011-04-24", "2010-08-11", "2010-01-15", "2011-07-13", "2010-03-20", 
"2011-04-24", "2010-03-20", "2010-11-15")), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))
> dput(postMax[sample(1:nrow(postMax), 20),])
structure(list(type = c("Masked", "Masked", "Masked", "Masked", 
"Masked", "Masked", "Masked", "Masked", "Masked", "Masked", "Masked", 
"Masked", "Masked", "Masked", "Masked", "Masked", "Masked", "Masked", 
"Masked", "Masked"), buffer = c(1000, 500, 100, 200, 600, 1300, 
300, 1100, 400, 1400, 100, 1400, 1900, 1800, 900, 1800, 400, 
1700, 800, 1300), max = c(14.7852734070896, 14.9722784687354, 
15.2029852639159, 43.729031912867, 15.1848393092082, 51.7461939052593, 
24.5462660651035, 14.7020628187789, 51.660784286412, 51.6686452841226, 
37.4059816958464, 31.1296293850412, 16.3389454899921, 14.9904419060676, 
9.31826862882747, 37.5157587930802, 42.542644563124, 37.6102405759581, 
24.2312294619778, 42.7078563816514), date = c("2017-12-04", "2017-12-04", 
"2018-02-22", "2017-08-14", "2018-02-22", "2017-07-13", "2017-03-07", 
"2017-12-04", "2017-07-13", "2017-07-13", "2017-09-15", "2017-10-17", 
"2018-02-22", "2017-12-04", "2017-01-18", "2017-09-15", "2018-05-13", 
"2017-09-15", "2017-03-07", "2018-05-13")), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))


Solution 1:[1]

ggplot is meant to use each aesthetic once. It'd be best to bind_rows on your pre and post data, and then plot the combined data:

library(tidyverse)
df <- bind_rows(list(pre = preMax, post = postMax), .id = 'time') 

ggplot() +
  geom_line(data = df, aes(x=buffer, y=max, color=date, linetype= time)) +
  guides(color = guide_legend(ncol = 2))

enter image description here

As an alternative to putting date on the color aesthetic, you could use faceting for clarity (though this looks a little odd with the limited sample provided, it should give you the general idea):

ggplot() +
  geom_line(data = df, aes(x=buffer, y=max, linetype= time)) +
  facet_wrap(facets = ~date)

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1