Gradient of n colors ranging from color 1 and color 2
colorRampPalette
could be your friend here:
colfunc <- colorRampPalette(c("black", "white"))
colfunc(10)
# [1] "#000000" "#1C1C1C" "#383838" "#555555" "#717171" "#8D8D8D" "#AAAAAA"
# [8] "#C6C6C6" "#E2E2E2" "#FFFFFF"
And just to show it works:
plot(rep(1,10),col=colfunc(10),pch=19,cex=3)
One colour gradient according to value in column scatterplot in R
dat <- read.delim(text='"value"; "avg.sal"; "avg.temp"
2,5698; 34,27254; 4,44
5,4361; 34,30686; 4,64
2,27; 34,3538; 8,05
5,6015; 34,50136; 5,01
2,27; 34,37596; 7,4"', sep=";", dec=",")
bluefunc <- colorRampPalette(c("lightblue", "darkblue"))
plot( dat$avg.sal, dat$avg.temp,
col=bluefunc(5)[findInterval(dat$value, seq(2:6))] )
To respond to the followup question. When cex
is specified inside a vector it does control size of the "points":
dat$size <- 1:5
bluefunc <- colorRampPalette(c("lightblue", "darkblue"))
plot( dat$avg.sal, dat$avg.temp,
cex=dat$size,
col=bluefunc(5)[findInterval(dat$value, seq(2:6))] )
How to add gradient color to a surface3d in R when Z axis is between 0 and 1
I found the answer. I needed to order the cut values and then remap the values of the range with the colors. Not elegant, but working...
data.col = as.data.frame(matrix(as.factor(cut(s$z,ordered_result = T,
include.lowest = TRUE,
right = TRUE,
breaks = nb.div)),
dim(s$z)[1],
dim(s$z)[2],byrow = FALSE))
range = levels(cut(s$z,ordered_result = T,
include.lowest = TRUE,
right = TRUE,
breaks = nb.div))
library(plyr)
for(i in 1:ncol(data.col)){
data.col[,i] <- mapvalues(data.col[,i],
from=range,
to=rbPal(nb.div),
warn_missing = FALSE)
}
Related Topics
How Does One Reorder Columns in a Data Frame
Long/Bigint/Decimal Equivalent Datatype in R
How to Use an Image as a Point in Ggplot
Table of Interactions - Case With Pets and Houses
Rename Multiple Columns by Names
R on Macos Error: Vector Memory Exhausted (Limit Reached)
How to Quickly Form Groups (Quartiles, Deciles, etc) by Ordering Column(S) in a Data Frame
Why Does X[Y] Join of Data.Tables Not Allow a Full Outer Join, or a Left Join
Dplyr: "Error in N(): Function Should Not Be Called Directly"
Check If the Number Is Integer
As.Date With Dates in Format M/D/Y in R
R Shiny: Handle Action Buttons in Data Table
Create New Variables With Mutate_At While Keeping the Original Ones
Don't Drop Zero Count: Dodged Barplot
How to Suppress Warnings Globally in an R Script
Convert Type of Multiple Columns of a Dataframe At Once
How to Assign from a Function Which Returns More Than One Value