在hyperSpec对象中用不同颜色绘制多个光谱 [英] Plotting multiple spectra with different colors in a hyperSpec object
问题描述
我试图在每张光谱上绘制不同颜色的同一曲线图上的多个光谱。
我使用了hyperSpec包和ggplot2中的'hyperSpec'对象。
我的数据集相当大,但其中的一部分看起来像这样:
> 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
我的绘图代码: p>
> spc< - new('hyperSpec',dataTable)
> p< - qplotspc(spc)
> p< - p + scale_x_reverse()
>打印(p)
我尝试添加自定义颜色的scale_x_manual,但无法获取工作,我很新ggplot。
请帮助和谢谢你!!!
我们直接尝试使用 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.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.
这篇关于在hyperSpec对象中用不同颜色绘制多个光谱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!