changing y-axis value to opposite order

219 views
Skip to first unread message

Morioka MS

unread,
May 2, 2012, 6:42:11 AM5/2/12
to ggplot2
Hi everyone,

I want to ask you how to change the y-axis value to opposite order.
On plot function, it can easily to change by exchanging ylim value as
from plot(x,y,ylim=c(1,50))
to plot(x,y,ylim=c(50,1))
I searched this one in previous posts.
But I could not find out.
Could you tell me how to change it.

Thanks,
Masaki MS.

Dennis Murphy

unread,
May 2, 2012, 9:34:17 AM5/2/12
to Morioka MS, ggplot2
Hi:

The function to use is scale_y_reverse(), but it uses the 'actual' y
limits (i.e., those for which the scale is trained) rather than the
defined y limits. Here's a toy example:

DF <- data.frame(x = rnorm(50), y = rnorm(50, mean = 20, sd = 5))
library('ggplot2')
ggplot(DF, aes(x, y)) + geom_point() + scale_y_reverse()

However, if you try to set an explicit y range and then apply
scale_y_reverse(), it ignores the defined range:

(b <- ggplot(DF, aes(x, y)) + geom_point() + ylim(0, 50))
b + scale_y_reverse()

Perhaps someone can chime in with a way to use scale_y_reverse() on a
scale with a user-defined y-range.

Dennis
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

Masaki MS

unread,
May 3, 2012, 2:09:39 AM5/3/12
to ggplot2
Hi Dennis,

Thank you for your rapidly replying.
I success to change y-axis to opposite order! :-)

Masaki MS.

2012/5/2 Dennis Murphy <djm...@gmail.com>



--
---------------------------------------------------------------------------------------
森岡 勝樹
理学博士
東京大学医学部附属病院
循環器内科 特任助教
東京都 文京区 本郷7-3-1
東京大学医学部附属病院
旧中央診療棟 2F 307研 第三研究室
Masaki Suimye Morioka
Ph. D.
Department of Cardiovascular Medicine
The University of Tokyo Graduate School of Medicine
7-3-1 Hongo, Bunkyo
Tokyo 113-8655, Japan

Phone:+81-3-3815-5411 (内)33127
email:msmori...@umin.ac.jp 
---------------------------------------------------------------------------------------

Andy

unread,
Jan 28, 2014, 8:57:15 AM1/28/14
to ggp...@googlegroups.com
To set user defined limits with scale_y_reverse using your example you can do the following:

b <- ggplot (DF, aes (x, y, ymin = 0, ymax = 50)) + geom_point() + scale_y_reverse() 
Reply all
Reply to author
Forward
0 new messages