scale_x_discrete 5 тиков с 3 метками - PullRequest
2 голосов
/ 24 мая 2019

У меня 5 условий:

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

и я хотел бы поставить одну метку «Пассажирский диск» и «Удаленный диск» на полпути между существующими точками.

Набор данных игрушек:

  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

Когда я пытаюсь использовать scale_x_continous с перерывами, я получаю сообщение об ошибке, потому что данные являются дискретными и категоричными. В фактическом наборе данных представлено еще несколько переменных, поэтому я не требую более эффективного способа построения этих данных. Я просто хотел бы превратить метки для 5 категориальных позиций по оси X в 3 метки по оси X. «Пассажирский диск» будет перемещаться между точками 2 и 3, а «Удаленный диск» будет перемещаться между точками 4 и 5.

Ответы [ 2 ]

1 голос
/ 24 мая 2019

Вы можете создать фиктивную числовую переменную для оси x и использовать scale_x_continuous вместо 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

1 голос
/ 24 мая 2019

Обходной путь

Просто измените

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

на

  scale_x_discrete(labels = df$condition) +

Желательно

Я знаю, что вы не просите более эффективного способано я думаю, что одна переменная (например, rt_type) должна быть сопоставлена ​​с точечной формой.

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