2

I am plotting a some data using ggplot2's geom_bar. The data represents a ratio that should center around 1 and not 0. This would allow me to highlight which categories go below or above this central ratio number. I've tried playing with set_y_continuous() and ylim(), neither of which allow me to sent a central axis value.

Basically: how to I make Y center around 1 and not 0.

sorry if i am asking a question that's been answered... maybe I just don't know the right key words?

ggplot(data = plotdata) +
  geom_col(aes(x = stressclass, y= meanexpress, color = stressclass, fill = stressclass)) +
  labs(x = "Stress Response Category", y = "Average Response Normalized to Control") +
  facet_grid(exposure_cond ~ .)

As of now my plots look like this:

enter image description here

d.b
  • 32,245
  • 6
  • 36
  • 77
  • Can you elaborate what you mean by "center around 1" since plotting a barplot means you'll fill space from 0 to whatever value it is you're plotting? Maybe using `geom_point()` and limiting the y axis would be more suitable? you could have a single plot with a legend specifying your groups. – Croote Mar 08 '19 at 02:12
  • By "center around 1", I mean that, because a ratio of 1 means that bot the numerator and the denominator are equal, the plot should then use the value 1 as a base line and not the value zero. All values above a ratio of 1 are indicative of the experimental class being greater and all values less than 1 are indicative of the control being greater. Having the "center of the plot be 0 would illustrate this point more easily. Hope that helps!! – Bryant Chambers Mar 10 '19 at 00:20

1 Answers1

1

You can pre-process your y-values so that the plot actually starts at 0, then change the scale labels to reflect the original values (demonstrating with a built-in dataset):

library(dplyr)
library(ggplot2)

cut.off = 500                                            # (= 1 in your use case)

diamonds %>%
  filter(clarity %in% c("SI1", "VS2")) %>%
  count(cut, clarity) %>%
  mutate(n = n - cut.off) %>%                            # subtract cut.off from y values
  ggplot(aes(x = cut, y = n, fill = cut)) +
  geom_col() +
  geom_text(aes(label = n + cut.off,                     # label original values (optional)
                vjust = ifelse(n > 0, 0, 1))) +
  geom_hline(yintercept = 0) +
  scale_y_continuous(labels = function(x) x + cut.off) + # add cut.off to label values
  facet_grid(clarity ~ .)

plot

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • I guess it is easier to just modify the data, I thought there was some way of moving the y axis but this makes sense. Thanks, Z. – Bryant Chambers Mar 10 '19 at 00:23