Hoang Le
Hoang Le

Reputation: 307

Bar plot with two factors with gradient color in R

I have a data frame like this:

df = data.frame(time = rep(0.5:9.5, each = 10), roi = rep(1:10, 10), area = runif(100, 5.0, 7.5))

I would like to draw a plot including 10 bars of 10 values of time and roi. In this case, x-axis will be roi, y-axis will be time, and the values of area will be showed in bars with gradient color (highest = white, lowest = black). The y-axis is reversed from 0.5 top to 9.5 bottom, width of bar is 2.

The expected figure looks like this image.

I am looking forwards to replies and I appreciate your all helps.

Upvotes: 0

Views: 1547

Answers (1)

Shirin Elsinghorst
Shirin Elsinghorst

Reputation: 310

Is this what you had in mind?

df=data.frame(time=as.factor(rep(0.5:9.5,each=10)),roi=rep(1:10,10),area=runif(100, 5.0, 7.5))

df$time <- factor(df$time, levels=rev(levels(df$time)))

ggplot(data=df, aes(y=factor(roi), x=time, fill = area)) +
  theme_minimal()  + coord_flip() +
  geom_tile(colour = "white", width = .9, height = 1) + 
  scale_fill_gradient(low="blue",high="red")

enter image description here

Upvotes: 0

Related Questions