我找不到一种方法来为同一个gggraph中的两个不同的geome实现两种不同的 colored颜色 比例.我try 了三种方法:手动定义两个色标,但这会导致第二个色标超过第一个色标;将第二个色标定义为变量,但这不起作用;将参数fill与第二个色标一起使用,这也不起作用.那么,正确的方式是什么呢?如果剧情能显示两个传说,我将不胜感激:一个是第一个geom_segment,一个是第二个geom_segment

ggplot(data = df2, #%>% 
       #filter(MM == "aoi"), 
       aes(x = start_pm, xend = end_pm,
           y = MM_by, yend = MM_by,               
           color = MM_annotation)) +
  geom_segment(size = 3) +
  geom_point(aes(x = max_x, y = MM_by), alpha = 0) +  #plot invisible dummy end points
  scale_y_discrete(name = NULL, limits = rev) +         #rev to get A at the top
  facet_wrap(~ Utterance, scales = "free_x", ncol = 1) +
  scale_colour_manual(values = c("*" = "lemonchiffon",
                                 "A" = "darkorange",
                                 "B" = "lawngreen",
                                 "C" = "slateblue1")) +
  
  # add vertical lines demarcating word end points:
  geom_vline(data = df2 %>% 
               filter(MM == "word"),
             aes(xintercept = end_pm), lty = 3)+
  
  # add words:
  geom_text(data = df2 %>% 
              filter(MM == "word"),
            aes(x = end_pm -(end_pm - start_pm)/2, label = MM_annotation), size = 3, colour = "white")+
  
  # gesture:
  geom_segment(data = df2 %>%
                 filter(MM == "gesture") #%>% 
               #mutate(col = ifelse(MM_annotation == "prep", "skyblue",
               #                    ifelse(MM_annotation == "stroke", "royalblue",
               #                           ifelse(MM_annotation == "hold", "blue", "navyblue")))
               ,
               aes(x = start_pm, xend = end_pm,
                   y = MM_by, yend = MM_by,             
                   color = MM_annotation)) +
  scale_colour_manual(values = c("prep" = "lightblue",
                                 "stroke" = "royalblue",
                                 "hold" = "blue",
                                 "retract" = "navyblue"))

生效日期:

df2 <- structure(list(Utterance = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
                                              1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
                                              2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
                                              4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), levels = c("C: [what's] a !mountain! for you: (#1)", 
                                                                                              "NA: (0.554) (#1)", "C: ((v: laughs))= (#1)", "A: =I don't know the (Schauinsland) is a small mount[ain] (#1)"
                                              ), class = "factor"), MM_by = c("Gaze_by_A", "Gaze_by_A", "Gaze_by_A", 
                                                                              "Gaze_by_A", "Gaze_by_B", "Gaze_by_B", "Gaze_by_B", "Gaze_by_B", 
                                                                              "Gaze_by_B", "Gaze_by_C", "Words_by_C", "Words_by_C", "Words_by_C", 
                                                                              "Words_by_C", "Words_by_C", "Words_by_C", "Gesture_by_C", "Gesture_by_C", 
                                                                              "Gesture_by_C", "Gaze_by_A", "Gaze_by_A", "Gaze_by_B", "Gaze_by_C", 
                                                                              "Gesture_by_C", "Gesture_by_C", "Gaze_by_A", "Gaze_by_B", "Gaze_by_C", 
                                                                              "Gesture_by_C", "Gaze_by_A", "Gaze_by_A", "Gaze_by_B", "Gaze_by_B", 
                                                                              "Gaze_by_C", "Gaze_by_C", "Words_by_A", "Words_by_A", "Words_by_A", 
                                                                              "Words_by_A", "Words_by_A", "Words_by_A", "Words_by_A", "Words_by_A", 
                                                                              "Words_by_A", "Words_by_A", "Gesture_by_A", "Gesture_by_A"), 
                      MM = c("aoi", "aoi", "aoi", "aoi", "aoi", "aoi", "aoi", "aoi", 
                             "aoi", "aoi", "word", "word", "word", "word", "word", "word", 
                             "gesture", "gesture", "gesture", "aoi", "aoi", "aoi", "aoi", 
                             "gesture", "gesture", "aoi", "aoi", "aoi", "gesture", "aoi", 
                             "aoi", "aoi", "aoi", "aoi", "aoi", "word", "word", "word", 
                             "word", "word", "word", "word", "word", "word", "word", "gesture", 
                             "gesture"), MM_annotation = c("*", "B", "*", "C", "A", "*", 
                                                           "C", "*", "A", "A", "what", "'s", "a", "mountain", "for", 
                                                           "you", "prep", "stroke", "hold", "C", "*", "A", "A", "hold", 
                                                           "retract", "*", "A", "A", "retract", "*", "C", "A", "*", 
                                                           "A", "*", "I", "do", "n't", "know", "the", "Schauinsland", 
                                                           "is", "a", "small", "mountain", "stroke", "retract"), start_pm = c(0, 
                                                                                                                              400, 867, 933, 0, 693, 940, 1413, 1560, 0, 0, 333, 447, 600, 
                                                                                                                              1113, 1333, 0, 547, 934, 1813, 2166, 1813, 1813, 1813, 2179, 
                                                                                                                              2367, 2367, 2367, 2367, 3437, 4437, 3437, 3743, 3437, 5737, 
                                                                                                                              3437, 3548, 3660, 3833, 4046, 4200, 4740, 4880, 5000, 5273, 
                                                                                                                              3437, 3950), end_pm = c(400, 867, 933, 1820, 693, 940, 1413, 
                                                                                                                                                      1560, 1820, 1820, 333, 447, 600, 1113, 1333, 1820, 547, 934, 
                                                                                                                                                      1813, 2166, 2373, 2380, 2380, 2179, 2366, 3427, 3427, 3427, 
                                                                                                                                                      3437, 4437, 6057, 3743, 6057, 5737, 6057, 3548, 3660, 3833, 
                                                                                                                                                      4046, 4200, 4740, 4880, 5000, 5273, 6055, 3950, 4104), max_x = c(2700, 
                                                                                                                                                                                                                       2700, 2700, 2700, 2700, 2700, 2700, 2700, 2700, 2700, 2700, 
                                                                                                                                                                                                                       2700, 2700, 2700, 2700, 2700, 2700, 2700, 2700, 4513, 4513, 
                                                                                                                                                                                                                       4513, 4513, 4513, 4513, 5067, 5067, 5067, 5067, 6137, 6137, 
                                                                                                                                                                                                                       6137, 6137, 6137, 6137, 6137, 6137, 6137, 6137, 6137, 6137, 
                                                                                                                                                                                                                       6137, 6137, 6137, 6137, 6137, 6137)), row.names = c(NA, -47L
                                                                                                                                                                                                                       ), class = c("tbl_df", "tbl", "data.frame"))
  

推荐答案

正如已经在 comments 中提到的,一个简单的方法是使用ggnewscale包,它允许多个比例和相同的美学图例:

library(ggplot2)
library(dplyr, warn=FALSE)
library(ggnewscale)

ggplot(
  data = df2,
  aes(
    x = start_pm, xend = end_pm,
    y = MM_by, yend = MM_by,
    color = MM_annotation
  )
) +
  geom_segment(size = 3) +
  geom_point(aes(x = max_x, y = MM_by), alpha = 0) + # plot invisible dummy end points
  scale_y_discrete(name = NULL, limits = rev) + # rev to get A at the top
  facet_wrap(~Utterance, scales = "free_x", ncol = 1) +
  scale_colour_manual(values = c(
    "*" = "lemonchiffon",
    "A" = "darkorange",
    "B" = "lawngreen",
    "C" = "slateblue1"
  )) +
  # add vertical lines demarcating word end points:
  geom_vline(
    data = df2 %>%
      filter(MM == "word"),
    aes(xintercept = end_pm), lty = 3
  ) +
  # add words:
  geom_text(
    data = df2 %>%
      filter(MM == "word"),
    aes(x = end_pm - (end_pm - start_pm) / 2, label = MM_annotation), size = 3, colour = "white"
  ) +
  ggnewscale::new_scale_color() +
  geom_segment(
    data = df2 %>%
      filter(MM == "gesture"),
    aes(
      x = start_pm, xend = end_pm,
      y = MM_by, yend = MM_by,
      color = MM_annotation
    )
  ) +
  scale_colour_manual(values = c(
    "prep" = "lightblue",
    "stroke" = "royalblue",
    "hold" = "blue",
    "retract" = "navyblue"
  ))
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.

R相关问答推荐

根据R中两个变量的两个条件删除带有dspirr的行

terra nearest()仅为所有`to_id`列返回NA

pickerInput用于显示一条或多条geom_hline,这些线在图中具有不同 colored颜色

将年度数据插入月度数据

在另一个函数中调用ggplot2美学

将数据集中的值增加到当前包含的最大值

使用strsplit()将向量操作为数据框

使用R闪光显示所有数据点作为默认设置

ComplexHEAT:使用COLUMN_SPLIT时忽略COLUMN_ORDER

如何将SAS数据集的列名和列标签同时包含在r中GT表的表首?

如何在PackageStatus()中列出&q;不可用的包&q;?

如何识别倒排的行并在R中删除它们?

使用RSelenium在R中抓取Reddit时捕获多个标签

查找所有站点的最小值

WRS2包中带有bwtrim的简单ANOVA抛出错误

删除在R中的write.table()函数期间创建的附加行

用多边形替换地块点

如何在AER::ivreg中指定仪器?

具有由向量定义的可变步长的序列

为什么在POSIXct-times的向量上循环会改变R中的类型?