R: gradient fill for geom_rect in ggplot2

I want to create a graph in R similar to the one below to show which place belongs to a particular person or company compared to my peers. The score will always be from 1 to 100.

rank

Although I succumb to any ggplot solution, it seemed to me that the best way is to use geom_rect , and then adapt and add the arrow described in the baptiste answer to this question . However, I found fault with something even simpler - getting geom_rect in order to fill the gradient correctly, as shown in the manual to the right of the graph below. It should be easy. What am I doing wrong?

 library(ggplot2) library(scales) mydf <- data.frame(id = rep(1, 100), sales = 1:100) ggplot(mydf) + geom_rect(aes(xmin = 1, xmax = 1.5, ymin = 0, ymax = 100, fill = sales)) + scale_x_discrete(breaks = 0:2, labels = 0:2) + scale_fill_gradient2(low = 'blue', mid = 'white', high = 'red', midpoint = 50) + theme_minimal() 

geom_rect

+3
r ggplot2
source share
1 answer

I think geom_tile() would be better - use sales for y and fill . Using geom_tile() you get a separate tile for each sales value and you can see the gradient.

 ggplot(mydf) + geom_tile(aes(x = 1, y=sales, fill = sales)) + scale_x_continuous(limits=c(0,2),breaks=1)+ scale_fill_gradient2(low = 'blue', mid = 'white', high = 'red', midpoint = 50) + theme_minimal() 

enter image description here

+3
source share

All Articles