3

I want to create in R a graphic similar to the one below to show where a certain person or company ranks relative to its peers. The score will always be between 1 and 100.

rank

Although I am amenable to any ggplot solution it seemed to me that the best way would be to use geom_rect and then to adapt and add the arrowhead described in baptiste's answer to this question. However, I came unstuck on something even simpler - getting the geom_rect to fill properly with a gradient like that shown in the guide to the right of the plot below. This 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

Community
  • 1
  • 1
SlowLearner
  • 7,907
  • 11
  • 49
  • 80

1 Answers1

5

I think that geom_tile() will be better - use sales for y and fill. With geom_tile() you will get separate tile for each sales value and will be able to 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

Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
  • Even you have your solution, maybe this can be of interest, somehow a rectangle is a polygon that can be constructed by segments: http://stackoverflow.com/a/20057229/1450084 – Tom Martens Nov 26 '13 at 21:56