Hi Karl,
See in line below.
On Sat, Jan 19, 2013 at 12:49 PM, Karl Ove Hufthammer
<
Karl.Hu...@math.uib.no> wrote:
> Dear ggplot2 list members,
>
> I have some data with identical (x,y) values. Is it possible to use
> position_dodge() to avoid the points overlapping? I have tried to do so, but
> only get strange warnings and results.
>
> Example:
>
> d=subset(iris, Petal.Length < 2)
> sunflowerplot(d[, 3:4]) # The ‘petals’ indicate duplicate points
>
> # Ordinary plotting hides these duplicates
> ggplot(d, aes(x=Petal.Length, y=Petal.Width)) + geom_point(size=3)
>
> # One solution is to use jittering, but even then some points
> # may by chance happen to lie directly on top of each other
> ggplot(d, aes(x=Petal.Length, y=Petal.Width)) +
> geom_jitter(size=3, position=position_jitter(w=.01,h=.01))
>
> # So I want to use dodging. But just setting position="dodge"
> # doesn’t work:
> ggplot(d, aes(x=Petal.Length, y=Petal.Width)) +
> geom_point(size=3, position="dodge")
>
> # Setting ‘width’ and ‘height’ only seems to have
> # the effect of compressing the plot horizontally
> # (only ‘width’ seems to matter – ‘height’ has no effect)
> ggplot(d, aes(x=Petal.Length, y=Petal.Width)) +
> geom_point(size=3, position=position_dodge(width=.1, height=.1))
position_dodge is for dodging groups. Since you have no groups defined
dodging has no effect. You can create groups as I've done below (note
that there is probably a better way to do this...)
tmp <- unique(d[c("Petal.Length", "Petal.Width")])
tmp$group <- interaction(tmp)
d <- merge(d, tmp)
d <- ddply(d, "group", transform, gn = factor(1:length(group)))
Now you can dodge the groups
ggplot(d, aes(x=Petal.Length, y=Petal.Width, group = gn)) +
geom_point(size=3, position=position_dodge(width=.08))
Personally I don't like this approach very well. Jiiter works better
in my opinion. Or you could size by the number of points:
d <- ddply(d, "group", transform, gsize = length(gn))
ggplot(d, aes(x=Petal.Length, y=Petal.Width, size = gsize)) +
geom_point()
>
> I also get strange warning, ‘ymax not defined: adjusting position using y
> instead’, but neither ‘position_dodge’ nor ‘geom_point’ takes a ‘ymax’
> argument, AFAICS.
I see that a lot, and just ignore it.
I think that is a documentation error. There is no dodging there for
the same reason there was no dodging in your original effort. The
example should be set up as
df <- data.frame(x=c("a","a","b","b"), y=1:4, g = rep(1:2, 2))
(p <- qplot(x, y, data=df, group=g, position="dodge", geom="bar",
stat="identity"))
then the remaining examples should work.
Best,
Ista
>
> --
> Karl Ove Hufthammer
>
> --
> 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