I am using the EMD package for R. This package has a spectrogram function for displaying a Hilbert Spectrum (calculated with hilbertspec). The output however, is really vague and black-white.
This function does not seem to have an option for outputting color images. How can I get the spectrum displayed clearly and if possible in color.
The gray levels are hardcoded in the spectrogram function (gray()), to override this setting you could use, for instance, the following:
# define a color palette
colors <- colorRampPalette(c("#007FFF", "blue", "#000077"))
gray <- function(x) colors(255*x) # redefine gray palette
spectrogram(test1$amplitude[,1], test1$instantfreq[,1])
gray <- grDevices::gray # reset gray palette function
Another option is to use the source of the spectrogram function to define your own plot function which has an argument for the color palette.
Related
I am trying to apply a colormap when plotting an array of numbers in Julia 1.4.1, but all plot entries appear to only use the first color in the map. For instance:
using Plots
plot([1:10], rand(10,5), c=:viridis)
No colour change occurs even when I increase the number of plot entries to 1000:
plot([1:10], rand(10,1000), c=:viridis)
I have tried to apply the map using color or cgrad too, but had no success. So this leaves me wondering: how can I apply this colourmap such that it correctly spans the number of plot entries?
Do you want each line to use a different color?
I think you are looking for the palette argument.
First,create the color palette based on viridis, as suggested by Anshul in the comments (see end for alternative method).
julia> using Plots
julia> p = Plots.palette(:viridis, 11)
In the last code, 11 is the number of lines
Now, let's plot using the palette argument.
julia> plot( [1:10], rand(10,11), palette=p )
Alternative method to create the palette is creating a color gradient function
julia> C(g::ColorGradient) = RGB[ g[z] for z in range(0,stop=1,length=11) ]
We define the palette using that function
julia> g = :viridis
julia> p = C(cgrad(g))
In R i have a cloud of data around zero ,and some data around 1, i want to "rescale" my heat colors to distinguish lower numbers.This has to be done in a rainbow way, i don't want "discrete colors".I tried with breaks in image.plot but it doesn't work.
image.plot(X,Y,as.matrix(mymatrix),col=heat.colors(800),asp=1,scale="none")
I tried :
lowerbreak=seq(min(values),quantile2,len=80)
highbreak=seq(quantile2+0.0000000001,max(values),len=20)
break=c(lowerbreak,highbreak)
ii <- cut(values, breaks = break,
include.lowest = TRUE)
colors <- colorRampPalette(c("lightblue", "blue"))(99)[ii]
Here's an approach using the "squash" library. With makecmap(), you specify your colour values and breaks, and you can also specify that it should be log stretched using the base parameter. It's a bit complex, but gives you granular control. I use it to colorize skewed data, where I need more definition in the "low end".
To achieve the rainbow palette, I used the built-in "jet" colour function, but you can use any colour set - I give an example for creating a greyscale ramp with "colorRampPalette".
Whatever ramp you use, it will take some playing with the base value to optimize for your data.
install.packages("squash")
library("squash")
#choose your colour thresholds - outliers will be RED
minval=0 #lowest value to get a colour
maxval=2.0 #highest value to get a colour
n.cols=100 #how many colours do you want in your palette?
col.int=1/n.cols
#create your palette
colramp=makecmap(x=seq(minval,maxval,col.int),
n=n.cols,
breaks=prettyLog,
symm=F,
base=10,#to give ramp a log(base) stretch
colFn=jet,
col.na="red",
right=F,
include.lowest=T)
# If you don't like the colFn options in "makecmap", define your own!
# Here's an example in greyscale; pass this to "colFn" above
user.colfn=colorRampPalette(c("black","white"))
Example for using colramp in a plot (assuming you've already created colramp as above somewhere in your program):
varx=1:100
vary=1:100
plot(x,y,col=colramp$colors) #colors is the 2nd vector in the colramp list
To select specific colours, subset from the list via, e.g., colors[1:20] (if you try this with the example above, the first colors will repeat 5 times - not really useful but you get the logic and can play around).
In my case, I had a grid of values that I wanted to turn into a coloured raster image (i.e. colour mapping some continuous data). Here's example code for that, using a made up matrix:
#create a "dummy matrix"
matx=matrix(data=c(rep(2,50),rep(0,500),rep(0.5,500),rep(1,500),rep(1.5,500)),nrow=50,ncol=41,byrow=F)
#transpose the matrix
# the output of "savemat" is rotated 90 degrees to the left
# so savemat(maty) will be a colorized version of (matx)
maty=t(matx)
#savemat creates an image using colramp
savemat(x=maty,
filename="/Users/KeeganSmith/Desktop/matx.png",
map=colramp,
outlier="red",
dev="png",
do.dev.off=T)
When using colorRampPalette, you can set the bias argument to emphasise low (or high) values.
Something like colorRampPalette(heat.colors(100),bias=3) will result focus the 'ramp' on the lower, helping them to be more visually distinguishable.
How can I define a "heated body" or "black-body radiation" color scheme for color-coding sequential data in R?
Standard R color palettes don't give an indication of ordinal, interval or ratio scaling. The rainbow palette is problematic for multiple reasons described by Borland & Taylor ("Rainbow Color Map (Still) Considered Harmful", IEEE Computer Graphics and Applications, 2007). (See also xkcd's painbow.) They instead recommend using a "black-body radiation" color palette, which is elsewhere called a "heated body" palette:
Unfortunately, I don't seem to find an R function to create this palette. ColorBrewer and the RColorBrewer package are not helpful (although this palette comes close), especially if I need more than 9 colors.
This question at CrossValidated is related: Most effective use of colour in heat/contour maps
Take RGB values for four colors from the "Default Color Map" page in the ParaView wiki: (0,0,0), (1,0,0), (1,1,0), (1,1,1)
Interpolate using grDevices::colorRamp()
Here is a function:
blackBodyRadiationColors <- function(x, max_value=1) {
# x should be between 0 (black) and 1 (white)
# if large x come out too bright, constrain the bright end of the palette
# by setting max_value lower than 1
foo <- colorRamp(c(rgb(0,0,0),rgb(1,0,0),rgb(1,1,0),rgb(1,1,1)))(x*max_value)/255
apply(foo,1,function(bar)rgb(bar[1],bar[2],bar[3]))
}
And here is an example, where we indicate the years in a seasonplot of the AirPassengers data using the rainbow vs. the black-body radiation color palette:
library(forecast)
n.colors <- ceiling(length(AirPassengers)/frequency(AirPassengers))
colors.blackBody <- blackBodyRadiationColors(seq(0,0.6,length.out=n.colors))
colors.rainbow <- rainbow(n.colors)
opar <- par(mfrow=c(2,1),mai=c(.5,1.2,.1,.1))
seasonplot(AirPassengers, col=colors.rainbow,
pch=19,year.labels=TRUE,xlab="",main="",ylab="Rainbow\npalette")
seasonplot(AirPassengers, col=colors.blackBody,ylab="Black-body\nradiation palette",
pch=19,year.labels=TRUE,xlab="",main="",ylab="Black-body\nradiation palette")
par(opar)
I have a raster image that I try to plot using rasterVis package in BuRdTheme. But I find the colors to be dull.
However, what I need is something like following. How can I create this desired pallete in raster or rasterVis or if any other package that would be appropriate?
I tried creating new palette with rasterTheme
rainbTheme5 <- rasterTheme(region = rev(rainbow(n = 5)))
levelplot(r,par.settings=rainbTheme5)
What I get is
The histogram of the image looks like
You've got some pixels down at about 305 (which I guess is blue) so most of your pixels are over 320 and so all in the brown/red end of your palette.
What you might want is a palette that changes over the peak in your data. You can do this by creating a palette that starts with a bunch of colours for the low values. For example:
> rep(rev(rainbow(n=5)),c(5,1,1,1,1))
[1] "#CC00FFFF" "#CC00FFFF" "#CC00FFFF" "#CC00FFFF" "#CC00FFFF" "#0066FFFF"
[7] "#00FF66FF" "#CCFF00FF" "#FF0000FF"
That has 9 colours, and the first five are identical. In this case, 5/9 of the range of the data would all be that colour, and the highest 4/9 of the dataset would be coloured by the full range of the data. Since your peak spans about that amount, you'll get the effect you are after. Adjust the palette to suit.
Note that you should always ask what question any graphic is trying to answer, and adjust colours/scales etc to help answer that question, rather than just trying to make it not look "dull", unless this is for the wall of a gallery.
Finally I could find what I was looking for:
myTheme <- rasterTheme(region = rep(rev(rev(colorRamps::matlab.like(n=12))),c(1,1,1,1,1,1,1,1,1,1,1,1)))
Using levelplot and using levelplot with customised theme
I'd like to plot a parallel co-ordinate plot for a dataset mtcars. I want to set a variable on color. I used the code :
library(GGgally)
ggparcoord(data=mtcars, columns=1:10 , groupColumn=11)
It generated the graph but all the lines are in shades of blue. However I have trouble comprehending the graph and making observations due to similar colors used. How can I introduce a different set of colors like blue, green and red etx for the same variable.
You can use the ggparcoord()'s coloring function for this by turning the grouping-column into a factor.
mtcars[,11] <- as.factor(mtcars[,11])
ggparcoord(data=mtcars, columns=1:10 , groupColumn=11)