0

I have log transformed my data and plan to plot this as a bar chart in ggplot2. The data looks as follows:

Log Transformed data

I would like to work out how to change where the X axis is positioned along the y axis at -4... I can do this in excel, however I would like to do this in ggplot2. The graphs would look as follows:

X axis at -4 intercept

Does anyone know how to do this?

rawr
  • 20,481
  • 4
  • 44
  • 78
Script_badger
  • 11
  • 1
  • 3
  • 1
    Where's a reproducible example? Where's your `ggplot` code? [See here for how-to on data sharing and making a good reproducible example](http://stackoverflow.com/q/5963269/903061) – Gregor Thomas Aug 17 '16 at 17:36
  • ggplot really doesn't accommodate this type of axis positioning. One solution might be to add a `geom_hline(yintercept = 0)` to your plot instead. – jdobres Aug 17 '16 at 17:40
  • those two charts aren't equal. you've modified the data in the bottom chart. – hrbrmstr Aug 17 '16 at 17:42

2 Answers2

0

You modified the data between charts in your example. While geom_bar() or it's new cousin geom_col() require y to be 0 (rightfully so and you really shouldn't be doing what you're doing anyway), you can do something similar with geom_segment():

library(ggplot2) # devtools::install_github("hadley/ggplot2")

# your original data
df <- data.frame(x=1:4, y=c(6, 4, -2, -3))
gg <- ggplot(df, aes(x, y))
gg <- gg + geom_col(width=0.5)
gg <- gg + theme_bw()
gg

# your modified data
df <- data.frame(x=1:4, y=c(10, 8, 2, 1))
gg <- ggplot(df, aes(x=x, xend=x, y=-4, yend=y-4))
gg <- gg + geom_segment(size=10)
gg <- gg + theme_bw()
gg
hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
0

You can transform your y-value to center around the new axis you wish to create. Then in scale_y_continuous() you can specify breaks =, and labels= to rename the value of the y-axis (which you transform)

In the example below. I center the y-value around .8 by first mutate() my y-value. Then I plot the geom_col() and modify the scale_y_continuous with label that represent the untransformed y-value.... and voila ;)

`df %>% 
  mutate(yvalue = yvalue - .8) %>% 
  ggplot(aes(x = FACTOR, y = yvalue)
  geom_col(position = "dodge", color = "black")+
  scale_y_continuous(limits = c(-.2,.4),
                     breaks = seq(-.2,.4,.1),
                     labels = seq(.6,1.2,.1))`