Assuming I have a data frame as such:
Date <- seq(as.Date("1990-03-01"), by = 'quarters', length.out=100)
Date<- as.yearqtr(Date, format="%Y-%m-%d")
A <- runif(100, 500, 2000)
B <- runif(100, 200, 1000)
C <- runif(100, 1000, 5000)
df <- data.frame(Date, A, B, C)
I would like to create a heatmap using ggplot2 but apply a different conditional discrete colour scale to each variable A and B based on the percentile of each of its value. i.e. For values below the 25th percentile > red, for values between 25th to 50th percentile > orange, for values between 50th to 75th percentile > yellow, for values between 75th to 100th percentile > green.
I could create a heatmap on ggplot geom_tiles by using the melt function and making x=Date and y=variable, with fill=value. However, they will take the value of all the variables as one vector and give the 25th percentile based on all the combined values. Is there a way to separately condition the scale_fill_manual based on the percentiles of individual variables A, B, C. In that case I would probably need to apply multiple scale_fill_manual functions.
Alternatively is there any way which I can stack the ggplot on top of each other since I have been able to create individual heatmaps for each variable. I have been struggling with this for a while and any help is appreciated, thank you very much.
This should help with the color coding:
Date <- seq(as.Date("1990-03-01"), by = 'quarters', length.out=100)
Date<- as.yearqtr(Date, format="%Y-%m-%d")
A <- runif(100, 500, 2000)
B <- runif(100, 200, 1000)
C <- runif(100, 1000, 5000)
df <- data.frame(Date, A, B, C)
df
quantA <- quantile(df$A, c(0.25, 0.75))
quantB <- quantile(df$B, c(0.25, 0.75))
quantC <- quantile(df$C, c(0.25, 0.75))
df$quantA <- with(df, factor(ifelse(A < quantA[1], 0,
ifelse(A < quantA[2], 1, 2))))
df$quantB <- with(df, factor(ifelse(B < quantB[1], 0,
ifelse(B < quantB[2], 1, 2))))
df$quantC <- with(df, factor(ifelse(C < quantC[1], 0,
ifelse(C < quantC[2], 1, 2))))
Please see this post for more details on color coding based on percentiles:
Color code points based on percentile in ggplot
and this one for color coding based on multiple variables:
R ggplot barplot; Fill based on two separate variables
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With