Offsetting objects on the x-axis

677 views
Skip to first unread message

Felix Schönbrodt

unread,
Feb 5, 2010, 4:34:00 AM2/5/10
to ggp...@googlegroups.com
Hi all,

I tried to offset an object, so that it doesn't overlap others - here's a simple example:

ggplot(diamonds[1:1000,], aes(cut, carat)) + geom_point(alpha=0.3) + stat_summary(fun.data="mean_cl_normal", color="red")


I'd like to move the pointrange a bit to the right, so that the points are not overlapped - I tried position_dodge, etc. but it didn't work ...
Any ideas?

Thanks,
Felix

smu

unread,
Feb 5, 2010, 4:42:14 AM2/5/10
to Felix Schönbrodt, ggp...@googlegroups.com
Hi,

maybe like this:

ggplot(diamonds[1:1000,], aes(cut, carat)) +

geom_point(alpha=0.3,position=position_dodge(width=.2)) +
stat_summary(fun.data="mean_cl_normal",color="red")

regards,
stefan

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> To post to this group, send email to ggp...@googlegroups.com
> To unsubscribe from this group, send email to
> ggplot2+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/ggplot2
>

Felix Schönbrodt

unread,
Feb 5, 2010, 4:52:22 AM2/5/10
to smu, ggp...@googlegroups.com
Hi Stefan,

that works for the points - however, I want to move the pointrange-object and unfortunately it does not work for that (maybe because it is only one object and the dodge algorithm distributes the objects based on their count?).

Best,
Felix

Am 05.02.2010 um 10:42 schrieb smu:

> Hi,
>
> maybe like this:
>
> ggplot(diamonds[1:1000,], aes(cut, carat)) +
> geom_point(alpha=0.3,position=position_dodge(width=.2)) +
> stat_summary(fun.data="mean_cl_normal",color="red")
>
> regards,
> stefan
>

smu

unread,
Feb 5, 2010, 5:11:13 AM2/5/10
to Felix Schönbrodt, ggp...@googlegroups.com
ah sorry, I should have read you message more attentive.

maybe something like
stat_summary(fun.color="red",position="jitter",heigth=0)

But it is not perfect, the different spaces between poitnrange and
points doesn't look so nice. Maybe someone else has a better idea.

regards,
stefan

Felix Schönbrodt

unread,
Feb 5, 2010, 5:39:16 AM2/5/10
to smu, ggp...@googlegroups.com
Yes, I tried the same, but with jitter it is jittered ;-)
My goal is to have the range bars always on the right side, with the same distance ...

Best,
Felix

Xie Chao

unread,
Feb 5, 2010, 5:41:22 AM2/5/10
to Felix Schönbrodt, smu, ggp...@googlegroups.com
How about this:

df <- diamonds[1:1000,]
ggplot(df, aes(y = carat)) +
geom_point(aes(as.numeric(cut)), alpha=0.3) +
stat_summary(aes(as.numeric(cut)+0.2), fun.data="mean_cl_normal",
color="red") +
scale_x_continuous('cut', breaks = 1:nlevels(df$cut), labels =
levels(df$cut), minor_breaks = NA)

Xie Chao

Felix Schönbrodt

unread,
Feb 5, 2010, 5:55:55 AM2/5/10
to Xie Chao, smu, ggp...@googlegroups.com
Thanks for your help - that's exactly what I wanted!

Best,
Felix

hadley wickham

unread,
Feb 5, 2010, 9:17:28 AM2/5/10
to Xie Chao, Felix Schönbrodt, smu, ggp...@googlegroups.com
> df <- diamonds[1:1000,]
> ggplot(df, aes(y = carat)) +
>    geom_point(aes(as.numeric(cut)), alpha=0.3) +
>    stat_summary(aes(as.numeric(cut)+0.2), fun.data="mean_cl_normal",
> color="red") +
>    scale_x_continuous('cut', breaks = 1:nlevels(df$cut), labels =
> levels(df$cut), minor_breaks = NA)

You can simplify the code a little bit:

df <- diamonds[1:1000,]

ggplot(df, aes(cut, carat)) +
geom_point(alpha=0.3) +


stat_summary(aes(as.numeric(cut)+0.2), fun.data="mean_cl_normal",
color="red")

Hadley

--
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages