buhtz
buhtz

Reputation: 12152

Group points in a stacked stripchart (1-D scatter plot) in R

This is hard to describe in words. First the picture. enter image description here

It was created with that code

df <- data.frame(a=sample(1:2, 60, replace=TRUE), b=c(1:3))
stripchart(b~a, data=df, method="stack", offset=0.5, pch=20)

Please look at the value bottom left (1x1.0). There are 13 points. I want to group them by (e.g.) 5 points. It means I want columns of 5 points. The result should be like that.

enter image description here

Upvotes: 0

Views: 212

Answers (1)

G5W
G5W

Reputation: 37641

Using your data generation function, but with different results because of different random selections.

df <- data.frame(a=sample(1:2, 60, replace=TRUE), b=c(1:3))
stripchart(b~a, data=df, method="stack", offset=0.5, pch=20)
df2 = df                ## So that df is not changed

for(A in unique(df$a)) {
    for(B in unique(df$b)) {
        S = which(df$a==A & df$b==B)
        while(length(S) > 5) {
            S = S[-(1:5)]
            df2$b[S] = df2$b[S]+0.05
        }
    }
}
stripchart(b~a, data=df2, method="stack", offset=0.5, pch=20)

Grouped stripchart

Upvotes: 2

Related Questions