How can I use a custom bin to color function with Holoviews HexTiles? - holoviews

I would like to do HexTiles on values which are 0s and 1s. However, the aggregation should not be a plain mean, but rather something 2-dimensional where the color hue is the mean and the alpha transparency is the standard deviation in the bins.
How can I achieve that with HexTiles?

Related

Rescaling colors palette in r

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.

Creating a continuous density heatmap of 2D scatter data in R with each column of dataframe coloured differently

I am curious if there's a way to improve upon the answers mentioned in 1
For example,
1) Can the x and y columns of the data-frame be colored differently rather than red or using a color gradient?. And as specified in ggplot2 documentation, I don't want color the columns according to a factor
2) Furthermore, can the shape of points be altered respectively for each of the columns in the data-fame (e.g. triangles for x values and round for y values)
To achieve the same, afaik, I tried to plot each column separately by tweaking the code mentioned in 1
All i got was the same plot with red color for each point with a failure to change the shape when using the aes() function for each column separately.
Thanks and Regards,
Yogesh

R - Subtracting two smoothScatter plots

I have two smoothScatter plots and hope to subtract them. See Below:
par(mfrow=c(1,2))
set.seed(3)
x1 = rnorm(1000)
y1 = rnorm(1000)
smoothScatter(x1,y1,nrpoints=length(x1),cex=3)
x2 = rnorm(200)
y2 = rnorm(200)
smoothScatter(x2,y2,nrpoints=length(x2),cex=3,colramp=colorRampPalette(c("white","red")))
My hope is that I can produce a 3rd plot which is a colorful subtraction of the 1st plot from the 2nd plot. That is, there will be areas which are blue, red, and then if possible I'd like to make the overlapped areas gray. But I'd like the colors to be consistent with the new densities. For instance, the center of the new plot would be almost fully gray, whereas the outsides may have some gray but also patches of blue and red. Note that the two plots have different numbers of points. How could I do such a thing?
The only way I can think of doing this is to go pixel by pixel and subtract the colors from one plot to another. The problem is, I don't know how to grab the color intensities at each pixel to do this. However, even if I were to achieve this, white minus white would probably give black, which I wouldn't want.
Thanks in advance!
You might consider using slightly transparent colors
#helper function to make transparent ramps
alpharamp<-function(c1,c2, alpha=128) {stopifnot(alpha>=0 & alpha<=256);function(n) paste(colorRampPalette(c(c1,c2))(n), format(as.hexmode(alpha), upper.case=T), sep="")}
And then we can overplot the two graphs with
smoothScatter(x1,y1,nrpoints=length(x1),cex=3, colramp=alpharamp("white",blues9))
par(new=T)
smoothScatter(x2,y2,nrpoints=length(x2),cex=3,colramp= alpharamp("white","red"), axes=F, ann=F)
Here's that this code produces.
If, you still want to get to the actual color values in the plot, that's actually a bit tricky. You'd have to call grDevices:::.smoothScatterCalcDensity directly with your data. Then you'd have to transform the returned fhat values by taking 4th root and rescaling to 0-1. Then you convert to color by taking those values and then those values (let's call them z are converted to indexes using the formula floor((256 - 1e-05) * z + 1e-07)+1. Then those indexes are used to find a value from the 256 colors generated from the ramp you supply. It's all a bit crazy but you can read the source to smoothScatter and image.default to see how it really happens.

Gnuplot color interpolation for set of linear functions

I want to plot N different linear functions in a graph using gnuplot.
Furthermore, I have to colors, lets say red and black.
I want to plot all functions with different colors, so that the first function is red, the Nth is black, and the color of all functions in between is interpolated.
How can I do this using gnuplot?
Note: N is not fixed, so I would like gnuplot to do the interpolation.
Something like this, which I quickly hacked together in Paint:
Here is one possibility to color the lines according to a predefined palette
N=6
set palette defined (0 'red', 1 'black')
f(x, n) = x+n
set samples 100
set style data lines
set key left
plot for [i=0:(N-1)] f(x, i) lw 2 lt palette frac i/(N-1.0) title sprintf('n = %d', i)
linetype palette frac chooses the color from a defined palette using a fractional value. You could also use linetype palette cb to use absolute values.
The result with 4.6.4 is

increasing contrast of ggplot2 scale_colour_gradient in R?

I use scale_colour_gradient/scale_colour_gradient2 to make gradients of colour of points in a scatterplot. The gradient is set from red to dark red, or black to red, as in:
ggplot(iris) + geom_point(aes(x=Sepal.Width, y=Sepal.Length, colour=Sepal.Length)) + scale_colour_gradient(low="red", high="darkred")
I often set scale_colour_gradient to be on a log scale since it represents ratios. my question is how can I increase the contrast between the points of the scale? E.g. make it so the difference between distinct parts of the scale? The scale is always continuous in my case (real numbers). any relevant points on this would help.
It seems black-to-red or red-to-darkred gives you very little color space to work with. You can use hex code to assign more specific colors to your low and high settings, and add a black background to improve contrast. For instance:
+ scale_colour_gradient2(low="#22FF00", mid="white", high="#FF0000", midpoint=median(iris$Sepal.Length)) + theme(panel.grid=element_blank(), panel.background=element_rect(fill="black"))
gives you much more contrast. Note that I am using scale_color_gradient2, which allows you to set a midpoint color and ascribe it to a summary statistic of the data (here, I used the median). I also used two colors at relatively opposite ends of the spectrum. Adding the above to your code produces:
But aside from playing around with the specific colors until you're satisfied (http://www.rapidtables.com/web/color/RGB_Color.htm and iwanthue are good resources for picking colors), I don't know if there exists a way to set a gradient so that contrast is maximized throughout, without creating some ungodly complex rainbow of colors. As you probably know, contrast between any two given points in your data is directly proportional to the difference between the values of those points, so varying that relationship within different locales of your gradient probably isn't desirable, and to my knowledge is not possible in ggplot2.
EDIT: Another way to improve contrast is to color by the rank-order of your desired variable (in this example, Sepal.Length) instead of the variable itself. This creates a uniform distribution, which will "spread out" your data by giving equal distance between quantiles. HOWEVER, this may produce a misleading visualization of your data--if your data are highly skewed, some identical/near-identical values could be represented by fairly contrasting colors. So use with caution.
Compare with above:
iris <- iris[with(iris, order(Sepal.Length)),]
iris$rank <- 1:150
ggplot(iris) + geom_point(aes(x=Sepal.Width, y=Sepal.Length, colour=rank)) + scale_colour_gradient2(low="#22FF00", mid="white", high="#FF0000", midpoint=median(iris$rank)) + theme(panel.grid=element_blank(), panel.background=element_rect(fill="black"))
Also, I realize that red-to-green is the least-colorblind-safe choice of colors possible. So you will want to choose colors so that your spectrum doesn't include red or green.

Resources