Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

x-axis starting value for diverging plot

Tags:

r

ggplot2

How can I change the "x-axis starting value" from the diverging bar chart below (extracted from here), so that the vertical axis is set at 25 instead of 0. And therefore the bars are drawn from 25 and not 0.

For instance, I want this chart:

enter image description here

To look like this:

enter image description here


EDIT It it not the label I want to change, it is how the data is plotted. My apologies if I wasn't clear. See example below:

Another example to make it clear:

enter image description here

like image 586
mat Avatar asked Nov 27 '25 12:11

mat


1 Answers

You can provide computed labels to an (x-)scale via scale_x_continuous(labels = function (x) x + 25).

If you also want to change the data, you’ll first need to offset the x-values by the equivalent amount (in the opposite direction):

Example:

df = tibble(Color = c('red', 'green', 'blue'), Divergence = c(5, 10, -5))
offset = 2

df %>%
    mutate(Divergence = Divergence - offset) %>% 
    ggplot() +
    aes(x = Divergence, y = Color) +
    geom_col() +
    scale_x_continuous(labels = function (x) x + offset)

enter image description here

like image 120
Konrad Rudolph Avatar answered Nov 30 '25 15:11

Konrad Rudolph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!