0

I’m using ggplot to make a geom_point() graph, and I’m using stat_summary() to show mean values for each category and an interconnecting line. I’ve had to reorganise the categories on the x-axis using scale_x_discrete(), but the line between the mean values is connecting my data points in the wrong order. So, I’m writing my code like this:

Baseline1 <- ggplot(data=smalllongdatabaseline, aes(x=Condition, 
                    y=AVDScore)) +    
    geom_point(aes(colour=as.factor(Condition)), size=1, 
                   show.legend=FALSE) +   
    scale_colour_brewer(palette = "RdBu") +   
    stat_summary(aes(y = AVDScore, group=Condition), fun=mean, 
                    colour="orange", geom="path", group=4) +   
    stat_summary(aes(y = AVDScore,group=Condition), fun=mean, 
                     colour="black", fill="orange", geom="point", size=2, 
                     shape=22, group=4) +   
    theme_classic() +   
    scale_y_continuous(name="Frequency", breaks=seq(0,35,5), limits=c(0,35), 
                       expand=c(0,0)) +   
    scale_x_discrete(limits=c("0.5 cpd + Baseline", "0.5 cpd + 0.5 KHz", 
                              "0.5 cpd + 4 KHz", "0.5 cpd + 8 KHz")) +   
    labs(x = "Stimulus Condition", y = "Mean Score")

And my output looks like this:

ggplot geom_point() graph

Can anyone help me to get the data points to connect in the correct order? I need to keep the categories on the x-axis in that order.

I can't really find any suggestions for this issue online. Someone suggested that the grpup=4 within stat_summary() should be group=Condition, but that returns an error message saying Obkect 'Condition' not found.

kjetil b halvorsen
  • 1,206
  • 2
  • 18
  • 28

1 Answers1

0

To achieve your desired move group=4 inside aes(). All other mappings of the group aes are redundant and could be removed.

Using some fake random example data.

library(ggplot2)

set.seed(123)

smalllongdatabaseline <- data.frame(
  Condition = sample(c("0.5 cpd + Baseline", "0.5 cpd + 0.5 KHz", "0.5 cpd + 4 KHz", "0.5 cpd + 8 KHz"), 100, replace = TRUE),
  AVDScore = runif(100, 5, 30)
)

ggplot(data = smalllongdatabaseline, aes(x = Condition, y = AVDScore)) +
  geom_point(aes(colour = as.factor(Condition)), size = 1, show.legend = FALSE) +
  scale_colour_brewer(palette = "RdBu") +
  stat_summary(aes(y = AVDScore, group = 4),
    fun = mean, colour = "orange", geom = "path"
  ) +
  stat_summary(aes(y = AVDScore),
    fun = mean, colour = "black",
    fill = "orange", geom = "point", size = 2, shape = 22
  ) +
  theme_classic() +
  scale_y_continuous(
    name = "Frequency", breaks = seq(0, 35, 5),
    limits = c(0, 35), expand = c(0, 0)
  ) +
  scale_x_discrete(
    limits = c(
      "0.5 cpd + Baseline", "0.5 cpd + 0.5 KHz",
      "0.5 cpd + 4 KHz", "0.5 cpd + 8 KHz"
    )
  ) +
  labs(x = "Stimulus Condition", y = "Mean Score")

stefan
  • 90,330
  • 6
  • 25
  • 51