'ggplot2: how to color a graph by multiple variables

I am fairly certain I have seen a solution for this somewhere, but as I have been unable to find it, here is my problem.

I have some time series data identified by multiple variables, I would like to be able to graph and differentiate color using multiple variables in ggplot2.

Sample data:

date <- c("2016-04-01 UTC", "2016-05-01 UTC", "2016-06-01 UTC", "2016-04-01 UTC",
          "2016-05-01 UTC", "2016-06-01 UTC", "2016-04-01 UTC", "2016-05-01 UTC",
          "2016-06-01 UTC", "2016-04-01 UTC")
temp <- c(80.24018,  85.88911, 104.23125,  85.13571,  91.21129, 104.88333,  97.81116,
          107.40484, 121.03958,  87.91830)
id <- c("A","A","A","A","A","B","B","B","B","B")
location <- c("N","S","S","N","N","S","N","S","N","S")

df <- data.frame(date,temp,id,location)

My attempt at graphing

library(ggplot2)

ggplot(df) + 
  geom_line(aes(x=date,y=temp,colour=factor(location), group=interaction(location,id)))

Using this code it is only coloring by location. I would like to the lines to be colored by location and id.



Solution 1:[1]

Two options:

library(ggplot2)

df <- data.frame(date = c("2016-04-01 UTC", "2016-05-01 UTC", "2016-06-01 UTC", "2016-04-01 UTC", "2016-05-01 UTC", "2016-06-01 UTC", "2016-04-01 UTC", "2016-05-01 UTC", "2016-06-01 UTC", "2016-04-01 UTC"),
                 temp = c(80.24018,  85.88911, 104.23125,  85.13571,  91.21129, 104.88333,  97.81116, 107.40484, 121.03958,  87.91830),
                 id = c("A","A","A","A","A","B","B","B","B","B"),
                 location = c("N","S","S","N","N","S","N","S","N","S"))

df$date <- as.Date(df$date)    # parse dates to get a nicer x-axis

Map id to color and location to linetype:

ggplot(df, aes(date, temp, color = id, linetype = location)) + geom_path()

...or plot all interactions as different colors:

ggplot(df, aes(date, temp, color = id:location)) + geom_path()

Solution 2:[2]

I want to provide another way of doing that. I don't know why but color=id:location doesn't work for me. I solved it by using tidyr::unite

This way I did it:

library(ggplot2)

df <- data.frame(date = c("2016-04-01 UTC", "2016-05-01 UTC", "2016-06-01 UTC", "2016-04-01 UTC", "2016-05-01 UTC", "2016-06-01 UTC", "2016-04-01 UTC", "2016-05-01 UTC", "2016-06-01 UTC", "2016-04-01 UTC"),
                 temp = c(80.24018,  85.88911, 104.23125,  85.13571,  91.21129, 104.88333,  97.81116, 107.40484, 121.03958,  87.91830),
                 id = c("A","A","A","A","A","B","B","B","B","B"),
                 location = c("N","S","S","N","N","S","N","S","N","S"))

df$date <- as.Date(df$date) 

df <- tidyr::unite(df,"id_loc",id,location,remove = F)
ggplot(df,aes(date, temp, color = id_loc)) + geom_path()

plot generated

Solution 3:[3]

The colon syntax didn't work for me either, but this did:

ggplot(df, aes(date, temp, color = interaction(id, location, sep=':'))) + geom_path()

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 alistaire
Solution 2 Manuel Hernández Banadik
Solution 3 David Pritchard