Ignacio
Ignacio

Reputation: 7948

wrong number of dots with geom_dotplot

My data looks like this:

df.plot <-structure(list(color = structure(c(2L, 2L, 3L, 1L, 3L, 4L, 3L, 
                                             1L, 4L, 1L, 2L, 4L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 
                                             3L, 3L, 3L, 3L), .Label = c("54", "55", "61", "69"), class = "factor"), 
                         date = structure(c(16687, 16687, 16687, 16687, 16687, 16687, 
                                            16688, 16688, 16688, 16689, 16689, 16690, 16693, 16693, 16693, 
                                            16694, 16694, 16695, 16695, 16695, 16695, 16696, 16696, 16696, 
                                            16696, 16696, 16696), class = "Date"), facet = c("A", 
                                                                                             "A", "A", "A", "A", "B", 
                                                                                             "B", "A", "B", "B", "B", "B", 
                                                                                             "B", "B", "B", "B", "A", "B", 
                                                                                             "A", "B", "A", "C", "B", "C", 
                                                                                             "C", "B", "C")), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                  -27L), .Names = c("color", "date", "facet"))

The following table summarizes the number of observations for each facet/color

df.plot %>% group_by(facet, color) %>% summarise(n())

---------------------
  facet   color   n() 
------- ------- -----
A      54      5  
A      55      2  
A      61      2  
B      54      6  
B      55      3  
B      61      2  
B      69      3  
C      61      4  
---------------------    

ggplot generates this graphs:

ggplot(df.plot, aes(x=date, fill=color)) + 
  geom_dotplot(binwidth=1) +
  coord_fixed(ratio=1) + 
  ylim(0,7) +
  facet_grid(facet ~ .) 

enter image description here

Why are the number of points wrong? For example, I expect to have 5 points for A 54 and the plot only shows 4.

Upvotes: 3

Views: 622

Answers (1)

erc
erc

Reputation: 10131

The dots of different colours but same dates overlap. Adding stackgroups=TRUE should give the expected result:

ggplot(df.plot, aes(x=date, fill=color)) + 
  geom_dotplot(binwidth=1, stackgroups=TRUE) +
  coord_fixed(ratio=1) + 
  ylim(0,7) +
  facet_grid(facet ~ .) 

enter image description here

Upvotes: 9

Related Questions