Home > Enterprise >  How to overlay dot plots on the boxplots with groups
How to overlay dot plots on the boxplots with groups

Time:10-03

I'm trying to create a simple graph to compare test scores by both the classes and the first or second test.

a <- data.frame(Name = rep(c("A", "B", "C", "D", "E", "F", "G", "H", "I"),2),
                Class = rep(c(rep("class1",3), rep("class2", 3), rep("class3", 3)),2),
                Test = c(rep("first",9), rep("second", 9)),
                Score = c(80, 70, 50, 60, 80, 90,
                          75, 85, 50, 75, 85, 98,
                          85, 76, 50, 75, 68, 72)
)
g <- ggplot(a, aes(x = Class, y = Score, fill = Test)) 
geom_boxplot()

This created expected boxplots categorized by class and the first/second test. Then, I tried to overlay the dotplots on the boxplots.

g <- g   geom_point()

But this created dot plots for each class with the first and second test scores on the same linesenter image description here

edit: grouping the dot and box:

g   geom_dotplot(binaxis='y', stackdir='center', dotsize=0.5, binwidth=5, position = position_dodge(width = 0.9))

enter image description here

CodePudding user response:

Is this the sort of layout you're looking for??

library(ggplot2)

a <- data.frame(Name = rep(c("A", "B", "C", "D", "E", "F", "G", "H", "I"),2),
                Class = rep(c(rep("class1",3), rep("class2", 3), rep("class3", 3)),2),
                Test = c(rep("first",9), rep("second", 9)),
                Score = c(80, 70, 50, 60, 80, 90,
                          75, 85, 50, 75, 85, 98,
                          85, 76, 50, 75, 68, 72)
)

g <- ggplot(a, aes(x = Class, y = Score, fill = Test)) 
  geom_boxplot()

g   geom_point(position = position_dodge(width = 0.75))

  • Related