Search code examples
rggplot2axis-labels

scale_x_discrete 5 ticks with 3 labels


I have 5 conditions:

 labels = c("Baseline","Passenger Drive","Passenger Drive","Remote Drive","Remote Drive")

and I would like to put a single label of "Passenger Drive" and "Remote Drive" halfway between the existing points.

Toy dataset:

  df <- data.frame(cbind(cbind(condition = c("Baseline","Passenger Drive",
                        "Passenger Drive","Remote Drive","Remote Drive"),
          rt_type = c("none",rep(c("driver_rt","other_rt"),2))),
  rt = c(.4,.6,.5,.7,.62)))

  ggplot(data = df,aes(x = interaction(rt_type,condition), y = rt)) + 
  theme_classic() + 
  geom_line(group = 1, size = 1) +
  geom_point(size = 3) + 
  scale_x_discrete(labels = c("Baseline",
                              "Passenger Drive",
                              "Remote Drive")) +
  labs(x = "Condition by Speaker", y = "Reaction Time (s)",
       linetype = "Responder", shape = "Speaker")

Interaction

When I try scale_x_continous with breaks I get an error because the data is discrete and categorical. The actual dataset has several more variables represented so I'm not asking for a more efficient way to plot this data. I would just like to turn labels for 5 categorical x-axis positions into 3 x-axis labels. "Passenger Drive" would move between points 2 and 3 and "Remote Drive" would move between points 4 and 5.


Solution

  • You could create dummy numeric variable for the x axis and use scale_x_continuous instead of scale_x_discrete.

    # This replaces interaction(rt_type, condition)
    df$intr <- as.numeric(as.factor(interaction(df$rt_type, df$condition)))
    
    # Creating dummy mid point to place labels in the middle
    ref_avg <- aggregate(intr ~ condition, df, mean)
    df$my_breaks <- ref_avg[match(df$condition, ref_avg$condition), "intr"]
    
    ggplot(data = df,aes(x = intr, y = rt)) + 
      theme_classic() + 
      geom_point(size = 3)  + 
      geom_path(group = 1) + 
      scale_x_continuous(breaks = df$my_breaks, labels = df$condition) + 
      labs(x = "Condition by Speaker", y = "Reaction Time (s)",
           linetype = "Responder", shape = "Speaker")
    

    enter image description here