0

I have a bar chart which was based on code provided here:

enter image description here

library(tidyverse)
dat <- data.frame(x = rep(3 - 0.4, 301),
                  xend = rep(3 + 0.4, 301),
                  y = seq(from = 0, to = 3, by = 0.01),
                  yend = seq(from = 0, to = 3, by = 0.01))

dat %>% 
  ggplot(aes(x = x, xend = xend, y = y, yend = yend, color = y)) +
  geom_segment(size = 2) +
  scale_color_gradient2(low = "green", mid = "yellow", high = "red", 
                        midpoint = max(dat$y)/2) +
  coord_flip() +
  theme(legend.position = "blank",
        axis.title = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank())

Can anybody help me modify this chart so that the x-axis scale (which is really a y-axis scale but which has been flipped), goes from -100% to 100%, ideally with breaks at 25% intervals, with the percentages shown in absolute values? My desired end result is the following:

enter image description here

Even if the underlying data is not modified that is fine - I just need to modify the axis labels. I'll eventually plot an hline on top of it, which is based on a -1 to 1 scale, but I can do the math to get the right placement on the 0 to 3 scale,

Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
DJC
  • 1,491
  • 6
  • 19

1 Answers1

1

The scales package can be convenient here. You can rescale the input data, put percentage labels in there and set breaks to occur every 0.25.

library(tidyverse)
library(scales)
dat <- data.frame(x = rep(3 - 0.4, 301),
                  xend = rep(3 + 0.4, 301),
                  y = seq(from = 0, to = 3, by = 0.01),
                  yend = seq(from = 0, to = 3, by = 0.01))

dat %>% 
  ggplot(aes(x = x, 
             xend = xend, 
             y = rescale(y, to = c(-1, 1), 
                         from = range(c(y, yend))), 
             yend = rescale(yend, to = c(-1, 1), 
                            from = range(c(y, yend))), 
             color = y)) +
  geom_segment(size = 2) +
  scale_color_gradient2(low = "green", mid = "yellow", high = "red", 
                        midpoint = max(dat$y)/2) +
  scale_y_continuous(labels = function(x){percent(abs(x))},
                     breaks = breaks_width(0.25)) +
  coord_flip() +
  theme(legend.position = "blank",
        axis.title = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank())

teunbrand
  • 33,645
  • 4
  • 37
  • 63
  • Thank you so much. One quick follow-up. I'm trying to add my segment line but for some reason I can't change the color to black. Any idea what's wrong with my code: +geom_segment(aes(x = 3.40, xend = 2.6, y = 0.25, yend = 0.25)) – DJC Feb 10 '21 at 20:19
  • 1
    Have you tried `geom_segment(aes(x = 3.40, xend = 2.6, y = 0.25, yend = 0.25), colour = "black") ` ? – teunbrand Feb 10 '21 at 20:23
  • I'm an idiot. I was trying to do it inside of the aesthetic, rather than in the geom_segment. Thank you again for the help. Hope my boss likes the end result :) – DJC Feb 10 '21 at 20:25