ggplot2 中每个面有不同的 x 刻度

问题描述 投票:0回答:1

我正在尝试可视化不同实验条件和参与者组的自定进度阅读数据。每个条件和组都涉及读取不同的句子,因此我希望每个方面的 x 轴标签显示它们读取的句子中的相应单词,而不是像 Position 这样的通用变量。 注意,我不想只使用数据框中数据的某些子集作为 x 刻度,因为我希望标签来说明实际数据中不存在的一些内容,所以我想手动指定这些标签独立于数据集

这是我的数据和当前绘图设置的最小工作示例 (MWE)。在这个例子中:

  • Position 列确保数据的线性顺序。
  • 我想用自定义标签中的单词替换 x 轴上的位置,特定于每个方面。

库(ggplot2) 图书馆(dplyr)

# Simulated dataset
reading_data_clean <- tibble::tibble(
  Position = rep(-5:3, times = 6),
  Read_time = rnorm(54, mean = 500, sd = 50),
  Subcondition = rep(c("ANE", "AR", "ENE", "ER", "agreement", "non-agreement"), each = 9),
  Language = rep(c("Danish", "German"), each = 27),
  Condition = rep(c("a-verb", "definite", "num.agreement"), each = 9, times = 2),
  Facet = rep(c("Danish-a-verb", "Danish-definite", "Danish-num.agreement", 
                "German-a-verb", "German-definite", "German-num.agreement"), each = 9)
)

# Custom x-tick labels for each facet
custom_labels <- list(
  "Danish-a-verb" = c("hunden", "bjeffer", "hele", "natten", "fordi", "katten", "løb", "på", "..."),
  "Danish-definite" = c("stenen", "lå", "ved", "floden", "og", "var", "markant", "stor", "..."),
  "Danish-num.agreement" = c("de", "fleste", "bøger", "er", "ikke", "tilgængelige", "for", "alle", "..."),
  "German-a-verb" = c("der", "Hund", "bellt", "die", "ganze", "Nacht", "weil", "die", "Katze"),
  "German-definite" = c("der", "Stein", "lag", "am", "Fluss", "und", "war", "sehr", "groß"),
  "German-num.agreement" = c("die", "meisten", "Bücher", "sind", "nicht", "für", "alle", "verfügbar", "...")
)

# Simplified plot
plot <- ggplot(reading_data_clean, 
               aes(x = Position, y = Read_time, color = Subcondition, group = Subcondition)) +
  geom_line(size = 1) +
  geom_point(size = 2) +
  # Use the dynamic Word column for x-axis labels
  
  facet_wrap(~ Facet, scales = "free_x") +  # Allow free x-axis labels for each facet
  labs(
    title = "Facet-Specific X-Ticks with Custom Labels",
    x = "Sentence Position",
    y = "Read Time (msec)",
    color = "Subcondition"
  ) +
  theme_minimal() +
  theme(
    axis.text.x = element_text(size = 10, angle = 45, hjust = 1),
    strip.text = element_text(size = 14, face = "bold"),
    legend.position = "bottom"
  )

# Print the plot
print(plot)
r ggplot2 visualization
1个回答
0
投票

我们可以直接将标签添加到绘制的线上:

library(ggplot2)
library(dplyr)

# Simulated dataset
reading_data_clean <- tibble::tibble(
  Position = rep(-5:3, times = 6),
  Read_time = rnorm(54, mean = 500, sd = 50),
  Subcondition = rep(c("ANE", "AR", "ENE", "ER", "agreement", "non-agreement"), each = 9),
  Language = rep(c("Danish", "German"), each = 27),
  Condition = rep(c("a-verb", "definite", "num.agreement"), each = 9, times = 2),
  Facet = rep(c("Danish-a-verb", "Danish-definite", "Danish-num.agreement", 
                "German-a-verb", "German-definite", "German-num.agreement"), each = 9)
)

# Custom x-tick labels for each facet
custom_labels <- list(
  "Danish-a-verb" = c("hunden", "bjeffer", "hele", "natten", "fordi", "katten", "løb", "på", "..."),
  "Danish-definite" = c("stenen", "lå", "ved", "floden", "og", "var", "markant", "stor", "..."),
  "Danish-num.agreement" = c("de", "fleste", "bøger", "er", "ikke", "tilgængelige", "for", "alle", "..."),
  "German-a-verb" = c("der", "Hund", "bellt", "die", "ganze", "Nacht", "weil", "die", "Katze"),
  "German-definite" = c("der", "Stein", "lag", "am", "Fluss", "und", "war", "sehr", "groß"),
  "German-num.agreement" = c("die", "meisten", "Bücher", "sind", "nicht", "für", "alle", "verfügbar", "...")
)

# Simple solution

reading_data_clean <- reading_data_clean %>%
  mutate(
    Word = purrr::map2_chr(
      Facet,
      Position + 6,  # Adjust Position to align with indexing in `custom_labels`
      ~ custom_labels[[.x]][.y]
    )
  )

ggplot(reading_data_clean, 
       aes(x = Position, y = Read_time, color = Subcondition, group = Subcondition)) +
  geom_line(size = 1) +
  geom_point(size = 2) +
  facet_wrap(~ Facet, scales = "free_x") +  # Allow free x-axis labels for each facet
  scale_x_continuous(
    breaks = unique(reading_data_clean$Position)
  ) +
  labs(
    title = "Facet-Specific X-Ticks with Custom Labels",
    x = "Sentence Position",
    y = "Read Time (msec)",
    color = "Subcondition"
  ) +
  theme_minimal() +
  theme(
    axis.text.x = element_text(size = 0, angle = 45, hjust = 1),
    strip.text = element_text(size = 14, face = "bold"),
    legend.position = "bottom",
    plot.margin = margin(1, 1, 2, 1)  # Add extra space around the plot
  ) +
  geom_text(aes(label = Word), angle = 45, vjust = 1.5, size = 3) + 
  coord_cartesian(clip = "off")  # Ensure text is not clipped

out2


或者我们使用 Word 作为 x 位置,这将使绘图看起来有点不同:

# Solution B


ggplot(reading_data_clean, 
       aes(x = Word, y = Read_time, color = Subcondition, group = Subcondition)) +
  geom_line(size = 1) +
  geom_point(size = 2) +
  facet_wrap(~ Facet, scales = "free_x")  +
  labs(
    title = "Facet-Specific X-Ticks with Custom Labels",
    x = "Sentence Position",
    y = "Read Time (msec)",
    color = "Subcondition"
  ) +
  theme_minimal() +
  theme(
    axis.text.x = element_text(size = 10, angle = 45, hjust = 1),
    strip.text = element_text(size = 14, face = "bold"),
    legend.position = "bottom"
  ) 

out2

© www.soinside.com 2019 - 2024. All rights reserved.