I'm trying to plot multiple spectra on the same plot with each spectrum having different colors. I use a 'hyperSpec' object from the hyperSpec package and ggplot2. My data set is pretty big, but a portion of it looks like this:
> dataTable
1_6-5p.asc 1_6-25p.asc 1_6-50p.asc 1_6-75p.asc 1_6-95p.asc
4000 98.35901 97.04647 98.65234 99.17536 97.11173
3999 98.35578 97.04401 98.65169 99.17371 97.11437
3998 98.35255 97.03779 98.65102 99.17253 97.11699
3997 98.34935 97.03048 98.65038 99.17188 97.12239
3996 98.34452 97.02479 98.64652 99.17108 97.12877
3995 98.33943 97.02187 98.64160 99.16943 97.13389
3994 98.33523 97.02140 98.63806 99.16646 97.13641
3993 98.33336 97.02289 98.63696 99.16242 97.13630
3992 98.33389 97.02617 98.63755 99.15876 97.13475
3991 98.33560 97.03071 98.63850 99.15775 97.13349
3990 98.33731 97.03488 98.63908 99.16082 97.13384
3989 98.33895 97.03685 98.63897 99.16680 97.13568
3988 98.34147 97.03625 98.63773 99.17248 97.13708
3987 98.34556 97.03472 98.63520 99.17517 97.13553
3986 98.35042 97.03425 98.63230 99.17450 97.13028
3985 98.35414 97.03502 98.63068 99.17167 97.12378
3984 98.35526 97.03520 98.63142 99.16771 97.12050
3983 98.35411 97.03351 98.63443 99.16319 97.12381
3982 98.35249 97.03137 98.63891 99.15940 97.13368
3981 98.35214 97.03208 98.64386 99.15863 97.14675
My code for plotting:
> spc <- new('hyperSpec',dataTable)
> p <- qplotspc(spc)
> p <- p + scale_x_reverse()
> print(p)
I've tried adding a scale_x_manual with custom colors but I couldn't get it to work, I'm pretty new to ggplot.
Please help and thank you!!!
Let's try using ggplot2
directly, as that will give us the most flexibility:
library(ggplot2)
library(reshape2) # For melt function
# Turn rownames into a data column
dataTable$x = as.numeric(rownames(dataTable))
# melt using x (the former rownames) as the ID variable
dataTable.m = melt(dataTable, id.var="x")
ggplot(dataTable.m, aes(x, value, colour=variable, group=variable)) +
geom_line() + geom_point() +
scale_x_reverse()
colour
tells ggplot
to give each value of variable
(which is the spectrum identifier) a separate color. group
tells ggplot
that each spectrum should be plotted as a separate line. There's a lot more customization you can do, but this is the basic idea.