Reputation: 4229
The distribution of colours should be according to values.
Example:
colour.palette <- colorRampPalette(c("darkgreen","yellow","red"), space = "rgb")
par(mfrow=c(1,2))
x1 <- c(0.1,0.2,0.3,0.4,0.5,0.6,0.65,0.70,0.725,0.750,0.775,0.80,0.825,0.85,0.875,0.9,1)
x2 <- c(0.01,0.02,0.03,0.04,0.05,0.06,0.065,0.070,0.225,0.350,0.475,0.50)
barplot(as.matrix(x1), horiz=FALSE, col=colour.palette(length(x1)))
barplot(as.matrix(x2), horiz=FALSE, col=colour.palette(length(x2)))
The barplot for x2
colours distribution should be up to yellow and not red. Values corresponding to red
colour are values approx. from 0.8 up to 1.
So how to modify this example that would take any vector length and plot colours that would match the 0-1 interval? There could be also up to 150 data points in 0-1 interval. Thanks.
Upvotes: 1
Views: 54
Reputation: 17648
There is definitely an easier way but what you can do is to bin your data and create matching factor labels. I used your data.
# binning
x1b <- .bincode(x1,seq(0,1,0.1),T)
x2b <- .bincode(x2,seq(0,1,0.1),T)
# which "factor levels" also appear in the second vector?
a <- intersect(x2b,x1b)
# set factor labels
x1bf <- factor(x1b, labels = colour.palette(length(seq(0,1,0.1))-1))
x2bf <- factor(x2b, labels = colour.palette(length(seq(0,1,0.1))-1)[a])
# plot
par(mfrow=c(1,2))
barplot(as.matrix(x1), horiz=FALSE, col=as.character(x1bf))
barplot(as.matrix(x2), horiz=FALSE, col=as.character(x2bf))
or use the length of the longer vector
x1b <- .bincode(x1,seq(0,1,1/length(x1)),T)
x2b <- .bincode(x2,seq(0,1,1/length(x1)),T)
x1bf <- factor(x1b, labels = colour.palette(length(x1))[unique(x1b)])
x2bf <- factor(x2b, labels = colour.palette(length(x1))[unique(x2b)])
Upvotes: 1