Bala
Bala

Reputation: 67

One colour gradient according to value in column scatterplot in R

I would like to make my scatterplot circles shaded according to the value in one column.

"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

I have my plot ready with salinity on the y-axis and temperature on the x-axis.

plot(df$avg.sal, df$avg.temp)

How do I shade the circles (e.g. from light blue to dark blue) according to the column "value"? The values have a big range but smaller values (e.g. 2) should be light blue and larger values (e.g. 10) should be dark blue. I would prefer not to use GGplot.

Upvotes: 3

Views: 10147

Answers (1)

IRTFM
IRTFM

Reputation: 263481

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))] )

Upvotes: 8

Related Questions