ggplot2 - R - Writing a generic function that will colour different lines in a ggplot line graph based on the parameters -
i found title difficult word want able colour different lines of plot based on parameters passed function call. can't provide data following code generate data frame in same shape , style mine. generate base plot i'm trying accomplish.
a = c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5) b = c("a", "b", "c", "d", "e", "a", "b", "c", "d", "e", "a", "b", "c", "d", "e", "a", "b", "c", "d", "e", "a", "b", "c", "d", "e") c = sample(1:100, 25, replace = true) df = data.frame(a,b,c) ggplot(df, aes(x = a, y = c, group = b, colour = b))+ geom_line(linetype = "dashed", size = 1, colour = "grey") essentially i'd able pass function vector containing values "a", "b", "e" , plot change. "a", "b", , "e" lines in colour , there legend on side name , colour of line.
i've tried few methods, closest being successful adding lines in loop, couldn't legend work , lines came in same colour.
i realize question duplicate i've been searching no avail couple days being pointed answered question may helpful. thanks!
you should delete colour argument in geom_line:
a = c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5) b = c("a", "b", "c", "d", "e", "a", "b", "c", "d", "e", "a", "b", "c", "d", "e", "a", "b", "c", "d", "e", "a", "b", "c", "d", "e") c = sample(1:100, 25, replace = true) df = data.frame(a,b,c) ggplot(df, aes(x = a, y = c, group = b, colour = b))+ geom_line(linetype = "dashed", size = 1) otherwise overriding color mapping.
Comments
Post a Comment