Re: Must x axis be numeric in geom_ribbon?

1,031 views
Skip to first unread message

Dennis Murphy

unread,
Mar 22, 2013, 8:11:53 PM3/22/13
to Stephanie May, ggp...@googlegroups.com
Hi:

On Fri, Mar 22, 2013 at 3:59 PM, Stephanie May <miz...@gmail.com> wrote:
> geom_ribbon() works when the x axis is numeric:
>
> data <- data.frame(TMAX = rnorm(10)+10,TMIN = rnorm(10) - 10, MDAY =
> rep(1:10))
> ggplot(data,aes(x=MDAY)) + geom_ribbon(aes(ymin=TMIN,ymax=TMAX))
>
> but not when the x axis is character:
> data <- data.frame(TMAX = rnorm(10)+10,TMIN = rnorm(10) - 10, MDAY =
> letters[1:10])
> ggplot(data,aes(x=MDAY)) + geom_ribbon(aes(ymin=TMIN,ymax=TMAX))

Firstly, by the way you defined your data frame, MDAY is not character:
> str(data)
'data.frame': 10 obs. of 3 variables:
$ TMAX: num 9.73 10.18 8.82 9.85 9.8 ...
$ TMIN: num -9.51 -9.75 -11.37 -8.7 -9.73 ...
$ MDAY: Factor w/ 10 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10
>
> Why???

geom_ribbon() is intended for continuous x. If you use geom_ribbon()
with a discrete x, are you not misleading the consumer of your graphic
into believing that the x-variable is continuous?
>
> Can anyone suggest a workaround?

Here are a couple, but they may not be what you want:

ggplot(data, aes(x = MDAY)) +
geom_segment(aes(xend = MDAY, y = TMIN, yend = TMAX), size = 1)
ggplot(data, aes(x = MDAY)) +
geom_pointrange(aes(y = (TMIN + TMAX)/2, ymin = TMIN, ymax =
TMAX), size = 1)

geom_pointrange() expects a 'middle point' - I chose the midrange of
TMIN and TMAX, but if you decide to use this and want a different
value, you'll need to include another variable in your data frame.

Dennis
>
> Thanks much!
> Stephanie
>
> --
> --
> 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/groups/opt_out.
>
>

Stephanie May

unread,
Mar 23, 2013, 3:34:59 PM3/23/13
to Dennis Murphy, ggp...@googlegroups.com
Hi Dennis,

Thanks for taking the time to respond.

> Firstly, by the way you defined your data frame, MDAY is not character:

Factor. You are absolutely right. Good job.

> geom_ribbon() is intended for continuous x. If you use geom_ribbon()
> with a discrete x, are you not misleading the consumer of your graphic
> into believing that the x-variable is continuous?

No. And I could have been more explicit about this in my initial post,
since I realize that all of my use cases and indeed some of the
examples in the documentation are dates or times. This works great:

data <- data.frame(TMAX = rnorm(10)+10,TMIN = rnorm(10) - 10, MDAY =
seq(as.Date("2013-03-01"), by = "3 days", length.out = 10 )
ggplot(data,aes(x=MDAY)) + geom_ribbon(aes(ymin=TMIN,ymax=TMAX))

And I see there are more options available using scale_x_date()

The documentation for geom_ribbon() says right in the header that it
is intended for continuous x, and I didn't notice that before I
posted, so I take full responsibility for the time I spent yesterday
staring at blank plots. NEVERTHELESS I will suggest that an error
message would gratify people like me who miss things even when they
are right in the page header.

Thanks! Back to ggplotting!
:) Stephanie
Reply all
Reply to author
Forward
0 new messages