5

I'm trying to get ggplot2 plot with reversed y-axis and x-axis on top. I used scale_y_reverse() to get reversed y-axis but could not figured out how to put x-axis on the top rather than at bottom.

dfn <- read.table(header=T, text='
supp dose length
  OJ  0.5  13.23
  OJ  1.0  22.70
  OJ  2.0  26.06
  VC  0.5   7.98
  VC  1.0  16.77
  VC  2.0  26.14
')

library(ggplot2)
p1 <- ggplot(data=dfn, aes(x=dose, y=length, group=supp, colour=supp)) + geom_line() + geom_point()
p1 <- p1 + scale_y_reverse()
print(p1)

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
MYaseen208
  • 22,666
  • 37
  • 165
  • 309
  • Unfortunately, this feature is unlikely to be implemented. See [related question](http://stackoverflow.com/questions/21035101/ggplot2-move-x-axis-to-top-intersect-with-reversed-y-axis-at-0). – tonytonov Nov 25 '14 at 10:35
  • 1
    Though AFAIK this is supported in ggvis. – tonytonov Nov 25 '14 at 10:37
  • Thanks @tonytonov for your interest in my problem. Is there any workaround other than using `Inkscape`? Thanks – MYaseen208 Nov 25 '14 at 10:38
  • A combination of grid/gridExtra may be helpful, though this is a complicated low-level approach. Also related: [one](http://stackoverflow.com/questions/26838005/putting-x-axis-at-top-of-ggplot2-chart), [two](http://stackoverflow.com/questions/21026598/ggplot2-adding-secondary-transformed-x-axis-on-top-of-plot). – tonytonov Nov 25 '14 at 10:42
  • [this](http://stackoverflow.com/questions/22318186/add-ticks-without-labels-on-the-top-of-a-bar-plot-in-ggplot2/22335684#22335684) might also help – user20650 Nov 25 '14 at 13:34

4 Answers4

7

Now even easier with ggplot v2.2.0:

p1 <- ggplot(data=dfn, aes(x=dose, y=length, group=supp, colour=supp)) + geom_line() + geom_point()
p1 <- p1 + scale_y_reverse() + scale_x_continuous(position = 'top')
print(p1)
CephBirk
  • 6,422
  • 5
  • 56
  • 74
4

If you don't want to switch to ggvis just yet, the ggdraw(switch_axis_position(p1 , axis = 'x')) function of the cowplot package works very well.

https://cran.r-project.org/web/packages/cowplot/vignettes/axis_position.html

Clay
  • 2,584
  • 1
  • 28
  • 63
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/11167218) – Ronak Shah Feb 06 '16 at 06:31
  • 1
    the link is only for reference and is not absolutely necesary for the answer. The code provided is the essential part of the answer. – Clay Feb 07 '16 at 11:23
1

You need ggvis to do that:

library(ggvis)
dfn %>% ggvis(~dose, ~length, fill= ~supp, stroke=~supp) %>% layer_lines(fillOpacity=0) %>%
  scale_numeric('y', reverse=T) %>% add_axis('x',orient='top')

enter image description here

LyzandeR
  • 37,047
  • 12
  • 77
  • 87
0

You should use either scale_y_continuous () or scale_y_reverse (), but not both at the same time. So:

scale_y_reverse (limits = c(0, -0), breaks = seq (from = 0, to = -0, by = -0), expand = c(0,0), position = "right")

P.S.: substitute 0 for your values

ginn
  • 87
  • 5