0

I want to archieve the following plot type using ggplot:

sketch of a ggplot showing one dot for every 10 observations

using the following data:

t <- read.table(header=T, row.names=NULL, 
    colClasses=c(rep("factor",3),"numeric"), text=
"week   team   level   n.persons
1      A      1       50
1      A      2       20
1      A      3       30
1      B      1       50
1      B      2       20
2      A      2       20
2      A      3       40
2      A      4       20
2      B      3       30
2      B      4       20")

so far, by applying this transformation

t0 <- t[ rep(1:nrow(t), t$n.persons %/% 10 ) , ]

and plotting

ggplot(t0) + aes(x=week, y=level, fill=team) +
geom_dotplot(binaxis="y", stackdir="center",
           position=position_dodge(width=0.2) 

i could generate

my archievement in ggplot so far: dots do not dodge each other vertically

A: How to archieve that dots of different teams dodge each other vertically and do not overlap?

B: Is it possible that the whole pack of dots is always centered, i.e. no dodging occurs if there are only dots of one team in one place?

akraf
  • 2,965
  • 20
  • 44
  • I gave a similar solution to Bubble Charts here: http://stackoverflow.com/questions/26757026/bubble-chart-with-ggplot2/26769996#26769996 geom_jitter is probably what you're looking for here. It will move dots around. In combination with alpha for transparency it could provide a working solution. – Docconcoct Oct 19 '15 at 11:27

1 Answers1

2

The following code stops the overlap:

t0 <- t[ rep(1:nrow(t), t$n.persons %/% 10 ) , ]

t0$level <- as.numeric(t0$level) # This changes the x-axis to numerics

t0$level <- ifelse(t0$team == "B", (t0$level+.1), t0$level) # This adds .1 to the position on the x-axis if the team is 'B'

ggplot(t0) + aes(x=week, y=level, fill=team) + geom_dotplot(binaxis="y", stackdir="center",
           position=position_dodge(width=0.2)) 

Here is the output:

enter image description here

You could also minus a value to move the dot downwards if you would prefer that.

If you want the line exactly between the dots this code should do it:

t0$level <- ifelse(t0$team == "B", (t0$level+.06), t0$level)
t0$level <- ifelse(t0$team == "A", (t0$level-.06), t0$level)

Output:

enter image description here

I'm not sure off the top of my head how to skip the above ifelse when there is only one team at a given coordinate. I'd imagine you'd need to do a count of unique team labels at each coordinate and only if that count was > 1 then run the code above.

Docconcoct
  • 2,040
  • 4
  • 28
  • 52
  • 1
    Thank you, this seems the nearest I can get to it. Seems like I have to dig into the docs how to write my own geoms and aestetics some day :-) – akraf Oct 26 '15 at 13:29