geom_ribbon cutting off weirdly when intercepting with x-axis

962 views
Skip to first unread message

rajivkk1

unread,
Jun 15, 2015, 6:40:23 AM6/15/15
to ggp...@googlegroups.com
Hello! I just wanted to make a basic plot using geom_ribbon. Here's an example:

Firstly I get my dataset:

x_vec <- 1:100
y_vec
<- seq(1,200,2)
data <- data.frame(x=x_vec,y=y_vec) #y=2x

This is essentially data of the form y=2x.

Then I plot it using geom_ribbon as such:

ggplot(data, aes(x=x,y=y)) +
      geom_ribbon
(aes(ymax=y+5,ymin=y-5), alpha=0.5) + 
#make a semi-transparent ribbon
      scale_y_continuous
(limits=c(10,100),expand=c(0,0)) + #set the limits and remove and margins within the plot for 
y-axis  
      scale_x_continuous(expand=c(0,0)) #remove margins within plot for x-axis

And I obtain this:



As you can see, the ribbon does this ugly thing where it cuts of vertically instead of going all the way to the x-axis. I would like to get something like this (which I did quickly using paint just for demonstration):


Can anyone help me with that? Thank you :)



Richard Zijdeman

unread,
Jun 15, 2015, 7:10:37 AM6/15/15
to rajivkk1, ggp...@googlegroups.com
Hi, thanks for you well setup question. Since you refer to the behaviour as ‘ugly’ I was wondering whether your point is just about aesthetics and if so, if you’re really ready to modify appropriate plotting behaviour. Ribbon adds colour directly above and below the plotted point, not to the side of a plotted point. What you’re asking for is different plotting behaviour at the extremes of your x-variable. Add ‘+ geom_point’ to your plot, and you’ll see that everywhere the grey ribbon is appropriately plotted.

Good luck,

Richard







--
--
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

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brian

unread,
Jun 15, 2015, 7:12:10 AM6/15/15
to rajivkk1, ggp...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

sometimes it's hard to know where to look.
- From ?stat_summary:
# Don't use ylim to zoom into a summary plot - this throws the
# data away
ergo:
ggplot(data, aes(x=x,y=y)) +
geom_ribbon(aes(ymax=y+5,ymin=y-5), alpha=0.5) +
coord_cartesian(ylim = c(0, 100))

will get you a little closer.
Best
Brian


On 06/15/2015 08:08 AM, rajivkk1 wrote:
> ggplot(data, aes(x=x,y=y)) + geom_ribbon(aes(ymax=y+5,ymin=y-5),
> alpha=0.5)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQIcBAEBCAAGBQJVfrL7AAoJEB+kbV9VX51IdO0P/iicHsvL/ysq+bh/L/QBx8pZ
6OMRGQusTj90bosC+ZP+wyE8d3HT7yJmnLTltsWZIfD8KcXpiGUiaQt9lMQZKZkx
Ru20MnpSJh4Q3EZQZnyQ0VKdUHJRb2t8Qc0XVAZshGPQ77NowLtHNXhn8dwNPnJc
sjitrFeaZissP6+YsTzfTYMU7xGEinoAenlsaEHmfKAms4Vxr5kwUpUehJMPJDt/
PN+0iI8wH06AmnMdpzZ7WG3vWbI/m5YJloEtLtUJNDu+9md4wD1LcJKJ138SLqp7
g7exTwnlrQ5JLIMQbsrKXKzDfcOMsOloNho1CA+R9yMw+4mQwZqBOMil2tjt/p9z
xlN7Uhqabv5ymDU1NsebJzl+THuxacOG451aKdQbMZsq90VMRo5D94/PnsZ9X3r+
due/2SX7aXvdTq6C750aD/64X8IIXubyFIzPUFZCHRV+b8VmDhK8g8zAkP0TY9Ux
t884D2dgQaY0DjM9PAJbE0SSFx4+IAdoHMddvAyoZMba0DUJLb0g+yafzCCAbx57
uvSLyy6ykcPK1BjetXGeFji2w7EUr1cmjIwzwuG14gR4QBlA1J+U+91ZM4y6UlXD
vJrtnBWNMaE7aSVCMC+oo+cTeSn6PjRX1h4hA1Fr6BzTbXEz7I3kEKLRJwJkLp45
EMaMSmFkE5ilYNTe33p8
=u50I
-----END PGP SIGNATURE-----

rajivkk1

unread,
Jun 15, 2015, 5:34:03 PM6/15/15
to ggp...@googlegroups.com, raji...@gmail.com
This worked perfectly! Thanks for taking the time to answer my question :)
Reply all
Reply to author
Forward
0 new messages