Geo-sp Geo-sp - 2 months ago 8
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
val
against
year
, but I don't want line, points or bar plots. Instead, I want to use
val
values showing with stretched colors covering entire background of plot area. Any suggestion?

Answer

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

library(ggplot2)
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.