shifted spectra with colour labels

122 views
Skip to first unread message

baptiste auguie

unread,
Aug 11, 2011, 9:44:06 PM8/11/11
to ggplot2
Dear list,

I've spent some time tweaking ggplot2 for the attached plot, using
directlabels and geom_text, but finally resorted to a hack shown
below. I'm curious to hear if

- this kind of plot has a name

- I missed a more obvious way to achieve the same end result. In
particular, I am not happy with the step where the colors are
hard-coded in the data, and plotted with scale_colour_identity. This
was necessary to obtain the same colours for the y-axis labels, which
I could not otherwise get to stand outside the plot area when using
geom_text().

The data consist in multiple spectra taken at different times. I wish
to plot them with a common axis, and offset them regularly along the
(arbitrary units) y axis to avoid undesirable overlap between
successive lines. The idea behind the representation is to see a trend
in spectral features such as peak shifts or (dis)appearance of peaks.

All the best,

baptiste

Here's a plot of the data,

library(hyperSpec)
plot(laser)

and now the ggplot2 hack to shift the spectra, plot them in gradual
colors, and label the different times with the corresponding colors,

library(hyperSpec)
library(ggplot2)
library(RColorBrewer)
library(scales)

## unobtrusive theme to focus on the spectra
theme_minimal <- function (base_size = 12, base_family = "", ...){
modifyList(ggplot2::theme_bw (base_size = base_size, base_family =
base_family),
list (axis.line = theme_blank(),
legend.background = theme_rect(colour = NA),
legend.key = theme_rect(colour = NA),
panel.background = theme_rect(fill = "white", colour = NA),
panel.border = theme_blank(),
strip.background = theme_blank(),
plot.background = theme_blank()))
}

## define a color palette function
palet <- gradient_n_pal(brewer.pal(3, "Set1")[c(2,1,3)])

##' shift the origin of spectra to avoid y overlap
## and hard-code the colors associated with each time step
spreadplot <- function(x, colors = palet, s=1){

test <- apply(x[[]], 2, diff)
shift <- max(-test[test < 0])
x$origins <- s*seq(0, by=shift, length=nrow(x))
x$scale <- colors(scales::rescale(x$origins))
x

}

## make a long format data.frame
toplot <- as.long.df(spreadplot(laser))

## select every other time step for the labels
lab <- subset(toplot, .wavelength == max(toplot$.wavelength) &
t %in% t[seq(1, length(t), by=2)])
levs <- unique(lab$origins)
lab.colors <- palet(rescale(levs))

p <-
ggplot(toplot) +
geom_path(aes(.wavelength, spc + origins, colour=scale, group=t)) +
scale_colour_identity()+
labs(x= expression("wavelength / nm"), y="I / a.u.", colour="t / s") +
theme_minimal() +
scale_y_continuous(breaks=levs, labels=round(lab$t, 1), expand=c(0,0)) +
opts(axis.text.y = theme_text(size = 8, hjust=1, colour=lab.colors))+
opts(legend.position="none")

p

stacked.pdf

Brandon Hurr

unread,
Aug 16, 2011, 6:05:43 AM8/16/11
to baptiste auguie, ggplot2
It plays with my eyes. Like those old Magic Eye pictures. 

I had to edit it slightly to get it to work. It didn't like the "." before wavelength nor the "..." in the function call.

No idea on the name, but it looks cool.  Does it do what you want it to do? I would think that the different colors might actually hinder your perception. Does it work in B&W mode just as well?

B


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

wavelength.png

baptiste auguie

unread,
Aug 16, 2011, 6:04:34 PM8/16/11
to Brandon Hurr, ggplot2
Thanks Brandon.

On Tue, Aug 16, 2011 at 10:05 PM, Brandon Hurr <brando...@gmail.com> wrote:
> It plays with my eyes. Like those old Magic Eye pictures.
> I had to edit it slightly to get it to work. It didn't like the "." before
> wavelength nor the "..." in the function call.

How strange, it works for me in a fresh session, and both of these
dots should be there.

> No idea on the name, but it looks cool.  Does it do what you want it to do?

Yes, I think so. It's quite common in spectroscopy, I believe.

> I would think that the different colors might actually hinder your
> perception. Does it work in B&W mode just as well?

The diverging color scheme makes little sense here, but it does in my
actual data where there is a maximum in the middle.

Thanks for the input,

baptiste

Reply all
Reply to author
Forward
0 new messages