本文介绍了在hyperSpec对象中用不同颜色绘制多个光谱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图在每张光谱上绘制不同颜色的同一曲线图上的多个光谱。 我使用了hyperSpec包和ggplot2中的'hyperSpec'对象。 我的数据集相当大,但其中的一部分看起来像这样: 我们直接尝试使用 ggplot2 ,因为这会给我们带来最大的灵活性: $ b $ as.numeric(rownames(dataTable)) #使用x(前面的rownames)作为ID变量 dataTable.m = melt(dataTable,id.var =x) $ b $ ggplot(dataTable.m,aes(x,value,color = variable,group = variable))+ geom_line()+ geom_point()+ scale_x_reverse() color 讲述 ggplot 给每个变量(这是光谱标识符)的值赋予一个单独的颜色。 group 告诉 ggplot ,每个谱图都应该绘制成一条单独的线。你可以做更多的定制,但这是基本的想法。 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.asc4000 98.35901 97.04647 98.65234 99.17536 97.111733999 98.35578 97.04401 98.65169 99.17371 97.114373998 98.35255 97.03779 98.65102 99.17253 97.116993997 98.34935 97.03048 98.65038 99.17188 97.122393996 98.34452 97.02479 98.64652 99.17108 97.128773995 98.33943 97.02187 98.64160 99.16943 97.133893994 98.33523 97.02140 98.63806 99.16646 97.136413993 98.33336 97.02289 98.63696 99.16242 97.136303992 98.33389 97.02617 98.63755 99.15876 97.134753991 98.33560 97.03071 98.63850 99.15775 97.133493990 98.33731 97.03488 98.63908 99.16082 97.133843989 98.33895 97.03685 98.63897 99.16680 97.135683988 98.34147 97.03625 98.63773 99.17248 97.137083987 98.34556 97.03472 98.63520 99.17517 97.135533986 98.35042 97.03425 98.63230 99.17450 97.130283985 98.35414 97.03502 98.63068 99.17167 97.123783984 98.35526 97.03520 98.63142 99.16771 97.120503983 98.35411 97.03351 98.63443 99.16319 97.123813982 98.35249 97.03137 98.63891 99.15940 97.133683981 98.35214 97.03208 98.64386 99.15863 97.14675My 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 columndataTable$x = as.numeric(rownames(dataTable))# melt using x (the former rownames) as the ID variabledataTable.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. 这篇关于在hyperSpec对象中用不同颜色绘制多个光谱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!