Geo-sp Geo-sp - 10 months ago 45
R Question

How to show y axis values with colors in background of a plot in R?

Suppose I have this data-set:

d <- data.frame("year"=c(2000:2005), "val"=c(rnorm(6,0,1)))

I want to show the
, but I don't want line, points or bar plots. Instead, I want to use
values showing with stretched colors covering entire background of plot area. Any suggestion?

Answer Source

From what you're describing you have two options:

Data: d <- data.frame("year"=c(2000:2005), "val"=c(rnorm(6,0,1)))

(1) Ggplot

ggplot(data=d) + geom_tile(aes(x=year,y=1,fill=val))

enter image description here (2) Base R

barplot(table(d$year),col=colorRampPalette(c('cadetblue1', 'cadetblue4'))(length(d$val))[rank(d$val)])

enter image description here

In the base R example, Dark = higher values of d$val whereas light = lower values of d$val.