Search code examples
rggplot2jitteraesthetics

Boxplot Not Showing Correctly


Attempting to run this command:

data %>%
+     ggplot( aes(x=Seconds, y=Rate)) +
+     geom_boxplot( fill="skyblue", notch=FALSE) +
+     geom_jitter( size=1, color="orange", width=0.2)

On this data.frame (each column has been transformed to a factor) (simplified):

Seconds  |  Rate
0.512849 |  0
0.227982 |  0
1.287198 |  1
1.278101 |  1
2.218711 |  2
3.124897 |  2
5.128947 |  3
6.189710 |  3

Output is this, which is showing the scatter plot part correctly but not the box-plot part:

Plot with error described in post.


Solution

  • Other answers were all partly correct, but the full answer is below. In particular the data needed grouped by Rate (JMilner suggested the grouping but by Seconds, so was on the right track!), and Rate needed to be made a factor, not seconds (as Brian and Camille suggested)

    ggplot(data, aes(x=Rate, y=Seconds, group=Rate)) +
    +     geom_boxplot( fill="skyblue", notch=FALSE) +
    +     geom_jitter( size=1, color="orange", width=0.3)
    

    Final output looks like this (changed Seconds to Minutes, label is now correct:)enter image description here