duttashi / learnr

Exploratory, Inferential and Predictive data analysis. Feel free to show your :heart: by giving a star :star:
MIT License
78 stars 54 forks source link

"warning message: position_dodge requires non-overlapping x intervals", when plotting a boxplot #5

Closed duttashi closed 7 years ago

duttashi commented 7 years ago
> str(data_balanced)
'data.frame':   610 obs. of  10 variables:
 $ VisitRsrc  : int  11 22 91 90 41 64 25 61 25 80 ...
 $ raisedhands: int  2 20 90 80 27 62 8 7 15 20 ...

> ggplot(data = data_balanced, aes(x=VisitRsrc, y=raisedhands, fill=gender)) +
+   geom_boxplot()+
+   coord_flip()+
+   scale_fill_discrete(name="Gender")+
+   facet_grid(~Relation)
Warning messages:
1: position_dodge requires non-overlapping x intervals 
2: position_dodge requires non-overlapping x intervals 

Issue: "warning message: position_dodge requires non-overlapping x intervals", when plotting a box plot is generated. This warning is generated, when plotting continuous variables on both x and y- axis

duttashi commented 7 years ago

Solution

This warning is generated because you are trying to plot continuous variables on x and y-axis. Although, the box plot will be generated but the warning will remain. The resolve this, remember, to plot the categorical on x-axis and continuous on y-axis.

The reason is ggplot2, draws boxplots vertically (categorical along x, continuous along y) and you are trying to draw them horizontally (continuous along x, continuous along y). The correct code will be;

> str(data_balanced)
'data.frame':   610 obs. of  10 variables:
 $ VisitRsrc  : int  11 22 91 90 41 64 25 61 25 80 ...
 $ Relation   : Factor w/ 2 levels "Father","Mother": 1 2 1 1 2 1 1 1 1 1 ...
 $ StudAbsnDay: Factor w/ 2 levels "Above-7","Under-7": 2 1 2 2 1 1 1 1 1 2 ..

>ggplot(data = data_balanced, aes(x=Relation, y=raisedhands, fill=gender)) +
  geom_boxplot()+
  scale_fill_discrete(name="Gender")+
  facet_grid(~StudAbsnDay)

Reference: See this SO answer by user Brian Diggs