Home > Back-end >  Bar graph groups on top of each other rather than superimposing each other
Bar graph groups on top of each other rather than superimposing each other

Time:02-26

Here is a reproducible sample:

set.seed(42)
n <- 1000
dat <- data.frame(Participant=1:20, 
                  Environment=rep(LETTERS[1:2], n/2),
                  Condition=rep(LETTERS[25:26], n/2),
                  Gate= sample(1:5, n, replace=TRUE),
                  Block = sample(1:2, n, replace=TRUE),
                  Sound=rep(LETTERS[3:4], n/2),
                  Correct=sample(0:1, n, replace=TRUE),
                  Target= sample(1:5, n, replace=TRUE)
)

When I go to graph this, the two groups show as above/below one another, rather than superimposing each other, giving me averages that exceed 100%. How do I get these groups to show either side-by-side or on top of one another?

Here is the code for my graph:

dat %>%
  group_by(Target, Sound) %>%
  dplyr::summarize(Accuracy = mean(Correct), 
                   se = sd(Correct)/sqrt(n())) %>%    
  ggplot(aes(x = Target, y = Accuracy, color = Sound, group = Sound))   
  geom_bar(stat = "identity")   
  scale_y_continuous(labels = scales::percent) 
  theme(axis.text.x = element_text(angle = 45)) 
  scale_color_brewer(palette = "Set1")

And a picture of the graph I am getting: enter image description here

CodePudding user response:

You can adjust the position of groups in geom_bar with the argument position:

geom_bar(stat = "identity", position = position_dodge())

I believe there is no need to use the argument group in ggplot(aes())

If you want to change the color of all bar instead of the outline, change color by fill

  • Related