在 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
我的绘图代码:
> spc <- new('hyperSpec',dataTable)
> p <- qplotspc(spc)
> p <- p + scale_x_reverse()
> print(p)
我试过添加带有自定义颜色的 scale_x_manual 但我无法让它工作,我是 ggplot 的新手。
请帮忙谢谢!!!
让我们尝试直接使用 ggplot2
,因为这会给我们最大的灵活性:
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
告诉 ggplot
给 variable
的每个值(这是光谱标识符)一个单独的颜色。 group
告诉 ggplot
每个光谱都应该绘制成一条单独的线。您可以进行更多自定义,但这是基本思想。
我正在尝试在同一图上绘制多个光谱,每个光谱具有不同的颜色。 我使用来自 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
我的绘图代码:
> spc <- new('hyperSpec',dataTable)
> p <- qplotspc(spc)
> p <- p + scale_x_reverse()
> print(p)
我试过添加带有自定义颜色的 scale_x_manual 但我无法让它工作,我是 ggplot 的新手。
请帮忙谢谢!!!
让我们尝试直接使用 ggplot2
,因为这会给我们最大的灵活性:
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
告诉 ggplot
给 variable
的每个值(这是光谱标识符)一个单独的颜色。 group
告诉 ggplot
每个光谱都应该绘制成一条单独的线。您可以进行更多自定义,但这是基本思想。