Skip to content

Instantly share code, notes, and snippets.

@mkuhn
Created August 11, 2011 08:08
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mkuhn/1139146 to your computer and use it in GitHub Desktop.
ggplot: Determining the order in which lines are drawn
library(ggplot2)
df <- data.frame( n=c("a","a","b","b","c","c"), x = rep(c(1,2), 3), y = rep(c(1), 6), l = as.factor(c(1,1,0,0,0,0)))
# Contents of df:
# n x y l
# 1 a 1 1 1
# 2 a 2 1 1
# 3 b 1 1 0
# 4 b 2 1 0
# 5 c 1 1 0
# 6 c 2 1 0
# Group by n: random order of label l
p <- ggplot(df, aes(x,y)) + geom_line(aes(group=n, color=l))
print(p)
# Concatenate label and name, and group by this:
df$o <- as.factor(apply(format(df[,c("l", "n")]), 1, paste, collapse=" "))
p <- ggplot(df, aes(x,y)) + geom_line(aes(group=o, color=l))
print(p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment