Skip to Content

jitter geom_line()

Is there a way to jitter the lines in geom_line()? I know it kinda defies the purpose of this plot, but if you have a plot with few lines and would like them all to show it could be handy. Maybe some other solution to this visibility problem.

Please see below for code,

A  <- c(1,2,3,5,1)
B  <- c(3,4,1,2,3)
id <- 1:5
df <- data.frame(id, A, B)
 
 
# install.packages(reshape2)
require(reshape2) # for melt
dfm <- melt(df, id=c("id"))
 
# install.packages(ggplot2)
require(ggplot2)
p1 <- ggplot(data = dfm, aes(x = variable, y = value, group = id, 
color= as.factor(id))) + geom_line() + labs(x = "id # 1 is hardly 
visible as it is covered by id # 5") + scale_colour_manual(values = 
c('red','blue', 'green', 'yellow', 'black')) 
 
 
p2 <- ggplot(subset(dfm, id != 5), aes(x = variable, y = value, 
group = id, color= as.factor(id))) + geom_line() + labs(x = "id # 
5 removed, id # 1 is visible") + scale_colour_manual(values = 
c('red','blue', 'green', 'yellow', 'black')) 
 
# install.packages(RODBC)
require(gridExtra)
 
grid.arrange(p1, p2)