r - Assign Colour to each line in ggplot2 -
hello people, trying use geom_line function create line graphs in r. want assign specific colours each line , unable so. when try manually assign colours colour names variables , in legend arranged alphabetically. if don't don't colours @ all. looked around on web , noticed there should grouping variable colours can assigned. unfortunately, in dataset here, each column corresponds different variable. not sure transposing dataset work because trying plot these variables against >2000 values on x-axis. think missing simple here.
ggplot(data=data, aes(xvar))+ geom_line(aes(y=var1))+ geom_line(aes(y=var2))+ geom_line(aes(y=var3))+ geom_line(aes(y=var4))
please feel free redirect section if has been answered before. appreciated.
i able manually without using ggplot2 function code follows:
plot(data$wavelength,data$var1,col="green") par(new=t) plot(data$wavelength,data$var2,col="red") par(new=t) plot(data$wavelength,data$var3,col="purple") par(new=t) plot(data$wavelength,data$var4,col="black") par(new=f)
here shortcuts may helpful:
dat <- data.frame(wave = 1:100, var1 = sort(rnorm(100)), var2 = sort(rnorm(100, 1)), var3 = sort(rnorm(100, 2))) plot(dat$var3, col = 'blue', type = 'l') lines(dat$var2, col = 'red') lines(dat$var1, col = 'green')
library(reshape2) library(ggplot2) dat.m <- melt(dat, id.vars = 'wave') ggplot(dat.m, aes(wave, value, colour = variable)) + geom_line()
ggplot(dat.m, aes(wave, value, colour = variable)) + geom_line() + scale_colour_manual(values = c('pink','orange','white'))
Comments
Post a Comment