代码之家  ›  专栏  ›  技术社区  ›  MAPK

如何更改特定线条的厚度和/或在多线条打印中添加形状?

  •  1
  • MAPK  · 技术社区  · 7 年前

    我有个数据叫 molten.data 下面。我有这段代码可以绘制我想要的线条,但是我需要改变一条特定线条的厚度( G11F:G11M )使其看起来比其他行更厚,或者最好在该行的数据点上添加形状。我们怎么做?

    我拥有的代码:

    ggplot(molten.data, aes(variable, value,group= key.related,colour=key.related)) + 
        geom_line() + 
        geom_point()
    

    数据:

    molten.data<- structure(list(key.related = c("G11F:G11F", "G11F:G11F", "G11F:G11F", 
    "G11F:G11M", "G11F:G11F", "G11F:G11M", "G11F:AOGC-02-0079", "G11F:G11F", 
    "G11F:G11M"), variable = structure(c(1L, 2L, 3L, 3L, 4L, 4L, 
    4L, 5L, 5L), .Label = c("IBS_2_samples", "IBS_4_samples", "IBS_8_samples", 
    "IBS_16_samples", "IBS_32_samples"), class = "factor"), value = c(0.533, 
    1.01, 1.11, 0.132, 1.22, 0.353, 0.0658, 1.33, 0.534)), .Names = c("key.related", 
    "variable", "value"), row.names = c(1L, 82L, 163L, 168L, 244L, 
    249L, 260L, 325L, 330L), class = "data.frame")
    
    2 回复  |  直到 7 年前
        1
  •  3
  •   camille    7 年前

    您可以设置一个列来记录是否应突出显示某个观测,并将其映射到大小、形状、线型等。

    你可以用一个 ifelse 以下内容:

    library(tidyverse)
    
    molten.data %>%
      mutate(hilite = ifelse(key.related == "G11F:G11M", 2, 1))
    #>         key.related       variable  value hilite
    #> 1         G11F:G11F  IBS_2_samples 0.5330      1
    #> 2         G11F:G11F  IBS_4_samples 1.0100      1
    #> 3         G11F:G11F  IBS_8_samples 1.1100      1
    #> 4         G11F:G11M  IBS_8_samples 0.1320      2
    #> 5         G11F:G11F IBS_16_samples 1.2200      1
    #> 6         G11F:G11M IBS_16_samples 0.3530      2
    #> 7 G11F:AOGC-02-0079 IBS_16_samples 0.0658      1
    #> 8         G11F:G11F IBS_32_samples 1.3300      1
    #> 9         G11F:G11M IBS_32_samples 0.5340      2
    

    我倾向于考虑各种因素。 forcats::fct_other 允许您选择要保留的级别,并将其他内容标记为“其他”。假设您有几个密钥要与它们的原始名称保持一致,并且随着数据变得更加复杂,您希望将许多密钥集中到一个“其他”类别中变得非常有用。

    然后我把它插进去 ggplot ,使用 hilite 指定突出显示。一种方法是使用大小;您可以调整大小以使其更具戏剧性,或者获取任何合适的大小:

    molten.data %>%
      mutate(hilite = as.factor(key.related) %>% fct_other(keep = c("G11F:G11M"), other_level = "Other keys")) %>%
      ggplot(aes(x = variable, y = value, color = key.related, group = key.related, size = hilite)) +
        geom_line() +
        geom_point(size = 2) +
        scale_size_manual(values = c("G11F:G11M" = 1.5, "Other keys" = 0.5), guide = F)
    

    或者保持大小一致但形状变化。在这里,我设置了形状值,使非高亮点得到一个正常的圆点,高亮点得到三角形。

    molten.data %>%
      mutate(hilite = as.factor(key.related) %>% fct_other(keep = c("G11F:G11M"), other_level = "Other keys")) %>%
      ggplot(aes(x = variable, y = value, color = key.related, group = key.related)) +
        geom_line() +
        geom_point(aes(shape = hilite), size = 2) +
        scale_shape_manual(values = c("G11F:G11M" = 17, "Other keys" = 16), guide = F)
    

    于2018年7月1日由 reprex package (第0.2.0版)。

        2
  •  1
  •   Vikash Kumar    7 年前

    根据需要,可以保留或删除尺寸或线型,如下所示-

    l <- ifelse(molten.data$key.related == 'G11F:G11M', 1, 2)
    ltyp <- ifelse(molten.data$key.related == 'G11F:G11M', 'longdash', '“dotted”')
    
    ggplot(data=molten.data, aes(x=variable, y=value, group= key.related, colour=key.related)) +
      geom_line(aes( linetype = ltyp , size = l )) +
      geom_point(aes( size = l ))
    

    enter image description here