1

When I make my bar graph using this code, it becomes unclear which bar the points are supposed to be above. This seems to happen when I add the col function.

Any help would be great!

ggplot(Data_Task1, aes(Type, Percentage_Correct_WND, fill = Condition, col = Animal)) + 
  geom_bar(stat = "summary", col = "black", position = "dodge") + 
  geom_point(position = position_dodge(0.9)) + 
  labs(x = "", y = "% Correct Without No Digs") + 
  scale_fill_brewer(palette = "Blues") + 
  scale_colour_manual(values = c("#000000", "#FF9933", "#00FF33", "#FF0000", "#FFFF00", "#FF00FF")) +
  theme(
    panel.grid.major.x = element_blank(), 
    panel.grid.minor.x = element_blank(), 
    axis.line.x = element_line(colour = "black", size = 0.5), 
    axis.line.y = element_line(colour = "black", size = 0.5)
  )

enter image description here

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • 1
    Welcome to SO! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data to reproduce your issue. – stefan Mar 29 '22 at 14:57
  • ... this said: The issue is most likely the grouping which you implicitly define via the `fill` and `color` aes. You could try to explicitly set the grouping for the points using the `group` aes, ie try `geom_point(aes(group = Condition), ....)` so that the points get dodged by Condition. – stefan Mar 29 '22 at 15:00

1 Answers1

1

The issue is that ggplot2 does not know the column in your dataset it should use for the dodging when it comes to the point geom. For the bar geom, it's obvious because different bars are drawn with different fill colors at the same x axis value. For your point geom, ggplot2 is dodging based on the color aesthetic. If you want to force grouping or dodging based on a specific column, you should assign that column to the group= aesthetic.

Here's an example using mtcars. I'm forcing continuous factors to be discrete via as.factor() here.

library(ggplot2)

# plot with incorrect dodging
ggplot(mtcars, aes(x=as.factor(carb), y=mpg, fill=as.factor(cyl), color=as.factor(gear))) +
  geom_bar(position="dodge", stat="summary", col='black') +
  geom_point(position=position_dodge(0.9), size=3) +
  theme_classic()

enter image description here

The bars are dodging based on as.factor(cyl), assigned to the fill aesthetic, but the points are dodging based on as.factor(gear), assigned to the color aesthetic. We override the color aesthetic in the geom_bar() command (as OP did) by defining col='black'.

The solution is to force the points to be grouped (and therefore dodged) based on the same column used for the fill aesthetic, so mapping is group=as.factor(cyl).

ggplot(mtcars, aes(x=as.factor(carb), y=mpg, fill=as.factor(cyl), color=as.factor(gear))) +
  geom_bar(position="dodge", stat="summary", col='black') +
  geom_point(aes(group=as.factor(cyl)), position=position_dodge(0.9), size=3) +
  theme_classic()

enter image description here

Applied to OP's case, the dodging should work with this code:

ggplot(Data_Task1, aes(Type, Percentage_Correct_WND, fill = Condition, col = Animal)) + 
  geom_bar(stat = "summary", col = "black", position = "dodge") + 
  
  # adjust group here to Condition (same as fill)
  geom_point(aes(group = Condition)), position = position_dodge(0.9)) + 
  labs(x = "", y = "% Correct Without No Digs") + 
  scale_fill_brewer(palette = "Blues") + 
  scale_colour_manual(values = c("#000000", "#FF9933", "#00FF33", "#FF0000", "#FFFF00", "#FF00FF")) +
  theme(
    panel.grid.major.x = element_blank(), 
    panel.grid.minor.x = element_blank(), 
    axis.line.x = element_line(colour = "black", size = 0.5), 
    axis.line.y = element_line(colour = "black", size = 0.5)
  )
chemdork123
  • 12,369
  • 2
  • 16
  • 32
  • This has worked thank you so much! – Kayleigh Jade Barnes Mar 29 '22 at 16:01
  • Having looked back at the graph, some of the points are now overlapping so they cant all be seen, is there any way to have the points which have the same x value next to eachother? – Kayleigh Jade Barnes Mar 30 '22 at 14:18
  • By the same "x value" I'm guessing you mean same y value? In this case, you are looking to [dodge and jitter at the same time](https://ggplot2.tidyverse.org/reference/position_jitterdodge.html). This can conveniently be achieved by using `position_jitterdodge()` instead of `position_dodge()` within `geom_point()`. Check the documentation for arguments, but `dodge.width=0.9` and the default `jitter.width=` worked in the example here. You may have to play with values to get something which looks right for your case. – chemdork123 Mar 30 '22 at 19:43