Error: `position_jitterdodge()` requires at least one aesthetic to dodge by

1.5k views Asked by At

I try to draw a barplot grouped figure, but I can't, my error is, "position_jitterdodge() requires at least one aesthetic to dodge by". Can anyone help me? Here is the sample of my data and my try to draw barplot code. My expected output is sth like the graph below.enter image description here

library(ggpubr)

d<-data.frame(Average=c(3,1.5,4, 6,
                    2,5.4,3.1,0.9,
                    4.6,3.3,2.8,1.9,
                    2.1,3.7,4.4,5.2),
          Size=c("1750","1750","1750","1750",
                 "2000","2000","2000","2000",
                 "2500","2500","2500","2500",
                 "3000","3000","3000","3000"),
          Group=c("P1","P1","P1","P1",
                  "S1","S1","S1","S1",
                  "P2","P2","P2","P2",
                  "S2","S2","S2","S2"))


ggbarplot(
  d, x = "Size", y = "Average", 
  add = c("mean_sd", "jitter"), 
  add.params = list(shape = "supp"),
  fill= "Group", palette = c("#807F7F", "#BF504D"),
  position = position_dodge(0.8)
)
1

There are 1 answers

0
Zhiqiang Wang On

There are several issues with your code:

  1. group has 4 values with only 2 colors assigned.
  2. The variable 'supp' does not exist in your data.

Otherwise your code should work:

library(ggpubr)
#> Loading required package: ggplot2

d<-data.frame(Average=c(3,1.5,4, 6,
                        2,5.4,3.1,0.9,
                        4.6,3.3,2.8,1.9,
                        2.1,3.7,4.4,5.2),
              Size=c("1750","1750","1750","1750",
                     "2000","2000","2000","2000",
                     "2500","2500","2500","2500",
                     "3000","3000","3000","3000"),
              Group=c("P","P","S","S", "P","P","S","S", "P","P","S","S","P","P","S","S")
              )


ggbarplot(
  d, x = "Size", y = "Average", 
  add = c("mean_sd", "jitter"), 
  add.params = list(shape = "Group"),
  fill= "Group", palette = c("#807F7F", "#BF504D"),
  position = position_dodge(0.8)
)

Created on 2020-10-02 by the reprex package (v0.3.0)