在ggplot上用曲线连接点,以获取“ x”轴上的分类变量

时间:2019-02-27 05:36:48

标签: r ggplot2

请考虑以下data.frame

oi2 <- structure(list(trial = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("ES1-7", 
"ES8-13", "ES14-25", "ES26-38", "SA1-13", "SA14-25"), class = "factor"), 
    marker = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
    ), rs. = c("S1A_499864157", "S1B_566171302", "S1B_642616640", 
    "S2B_75832544", "S2B_784544719", "S5B_24292046", "S5B_47584429", 
    "S7A_12011058", "S7B_124548883", "S7B_576927863", "S1A_494392059", 
    "S1A_497201550", "S2B_24883552", "S4B_644330895", "S5A_548234618", 
    "S5B_513712393", "S5D_550192169", "S6B_17686703", "S6B_459374225", 
    "S7A_7938818", "S7B_605313385", "S7B_733461150", "S1A_499864157", 
    "S1B_566171302", "S1B_642616640", "S2B_75832544", "S2B_784544719", 
    "S5B_24292046", "S5B_47584429", "S7A_12011058", "S7B_124548883", 
    "S7B_576927863", "S1A_494392059", "S1A_497201550", "S2B_24883552", 
    "S4B_644330895", "S5A_548234618", "S5B_513712393", "S5D_550192169", 
    "S6B_17686703", "S6B_459374225", "S7A_7938818", "S7B_605313385", 
    "S7B_733461150", "S1A_499864157", "S1B_566171302", "S1B_642616640", 
    "S2B_75832544", "S2B_784544719", "S5B_24292046", "S5B_47584429", 
    "S7A_12011058", "S7B_124548883", "S7B_576927863", "S1A_494392059", 
    "S1A_497201550", "S2B_24883552", "S4B_644330895", "S5A_548234618", 
    "S5B_513712393", "S5D_550192169", "S6B_17686703", "S6B_459374225", 
    "S7A_7938818", "S7B_605313385", "S7B_733461150", "S1A_499864157", 
    "S1B_566171302", "S1B_642616640", "S2B_75832544", "S2B_784544719", 
    "S5B_24292046", "S5B_47584429", "S7A_12011058", "S7B_124548883", 
    "S7B_576927863", "S1A_494392059", "S1A_497201550", "S2B_24883552", 
    "S4B_644330895", "S5A_548234618", "S5B_513712393", "S5D_550192169", 
    "S6B_17686703", "S6B_459374225", "S7A_7938818", "S7B_605313385", 
    "S7B_733461150", "S1A_499864157", "S1B_566171302", "S1B_642616640", 
    "S2B_75832544", "S2B_784544719", "S5B_24292046", "S5B_47584429", 
    "S7A_12011058", "S7B_124548883", "S7B_576927863", "S1A_494392059", 
    "S1A_497201550", "S2B_24883552", "S4B_644330895", "S5A_548234618", 
    "S5B_513712393", "S5D_550192169", "S6B_17686703", "S6B_459374225", 
    "S7A_7938818", "S7B_605313385", "S7B_733461150", "S1A_499864157", 
    "S1B_566171302", "S1B_642616640", "S2B_75832544", "S2B_784544719", 
    "S5B_24292046", "S5B_47584429", "S7A_12011058", "S7B_124548883", 
    "S7B_576927863", "S1A_494392059", "S1A_497201550", "S2B_24883552", 
    "S4B_644330895", "S5A_548234618", "S5B_513712393", "S5D_550192169", 
    "S6B_17686703", "S6B_459374225", "S7A_7938818", "S7B_605313385", 
    "S7B_733461150"), n = c(41L, 44L, 5L, 51L, 25L, 49L, 61L, 
    50L, 7L, 28L, 96L, 63L, 136L, 92L, 75L, 75L, 85L, 117L, 111L, 
    96L, 89L, 127L, 115L, 95L, 12L, 155L, 50L, 140L, 81L, 203L, 
    4L, 107L, 282L, 160L, 350L, 198L, 273L, 262L, 302L, 331L, 
    335L, 328L, 174L, 341L, 320L, 200L, 25L, 214L, 142L, 147L, 
    128L, 211L, 57L, 145L, 402L, 320L, 492L, 303L, 325L, 365L, 
    488L, 512L, 403L, 368L, 321L, 328L, 42L, 32L, 2L, 40L, 10L, 
    36L, 28L, 41L, 3L, 25L, 82L, 64L, 84L, 60L, 66L, 62L, 74L, 
    83L, 81L, 66L, 53L, 88L, 191L, 151L, 56L, 149L, 70L, 173L, 
    151L, 173L, 26L, 183L, 291L, 222L, 431L, 322L, 245L, 345L, 
    353L, 400L, 359L, 317L, 262L, 361L, 251L, 186L, 48L, 176L, 
    105L, 149L, 170L, 116L, 66L, 252L, 310L, 281L, 445L, 351L, 
    192L, 353L, 405L, 451L, 347L, 309L, 316L, 276L), prop = c(0.304054054054054, 
    0.320945945945946, 0.287162162162162, 0.371621621621622, 
    0.192567567567568, 0.358108108108108, 0.452702702702703, 
    0.375, 0.0506756756756757, 0.290540540540541, 0.665540540540541, 
    0.452702702702703, 0.925675675675676, 0.648648648648649, 
    0.537162162162162, 0.564189189189189, 0.611486486486487, 
    0.810810810810811, 0.780405405405405, 0.685810810810811, 
    0.628378378378378, 0.885135135135135, 0.311576354679803, 
    0.257389162561576, 0.261083743842365, 0.417487684729064, 
    0.130541871921182, 0.374384236453202, 0.224137931034483, 
    0.527093596059113, 0.0147783251231527, 0.435960591133005, 
    0.706896551724138, 0.415024630541872, 0.868226600985222, 
    0.519704433497537, 0.699507389162562, 0.683497536945813, 
    0.768472906403941, 0.830049261083744, 0.846059113300493, 
    0.826354679802956, 0.445812807881773, 0.850985221674877, 
    0.594736842105263, 0.394736842105263, 0.32719298245614, 0.409649122807018, 
    0.276315789473684, 0.283333333333333, 0.256140350877193, 
    0.404385964912281, 0.11140350877193, 0.43859649122807, 0.72719298245614, 
    0.597368421052632, 0.876315789473684, 0.569298245614035, 
    0.61140350877193, 0.678947368421053, 0.878070175438596, 0.912280701754386, 
    0.740350877192982, 0.67719298245614, 0.592982456140351, 0.6, 
    0.438095238095238, 0.328571428571429, 0.276190476190476, 
    0.4, 0.114285714285714, 0.357142857142857, 0.314285714285714, 
    0.428571428571429, NA, 0.333333333333333, NA, 0.638095238095238, 
    0.80952380952381, 0.595238095238095, 0.652380952380952, 0.619047619047619, 
    0.728571428571429, 0.823809523809524, 0.785714285714286, 
    0.666666666666667, 0.533333333333333, 0.857142857142857, 
    0.427350427350427, 0.351495726495727, 0.245726495726496, 
    0.355769230769231, 0.161324786324786, 0.396367521367521, 
    0.344017094017094, 0.382478632478633, 0.0598290598290598, 
    0.435897435897436, 0.636752136752137, 0.512820512820513, 
    0.927350427350427, 0.707264957264957, 0.544871794871795, 
    0.757478632478633, 0.772435897435897, 0.860042735042735, 
    0.777777777777778, 0.695512820512821, 0.585470085470085, 
    0.783119658119658, 0.54517453798768, 0.415811088295688, 0.322381930184805, 
    0.392197125256674, 0.24435318275154, 0.326488706365503, 0.385010266940452, 
    0.25564681724846, 0.15605749486653, 0.601642710472279, 0.650924024640657, 
    0.600616016427105, 0.921971252566735, 0.757700205338809, 
    0.428131416837782, 0.749486652977413, 0.848049281314168, 
    0.935318275154004, 0.725872689938398, 0.659137577002053, 
    0.664271047227926, 0.597535934291581), BASE = c("T", "A", 
    "G", "G", "A", "A", "C", "A", "A", "T", "C", "C", "A", "G", 
    "A", "A", "T", "G", "C", "G", "A", "T", "T", "A", "G", "G", 
    "A", "A", "C", "A", "A", "T", "C", "C", "A", "G", "A", "A", 
    "T", "G", "C", "G", "A", "T", "T", "A", "G", "G", "A", "A", 
    "C", "A", "A", "T", "C", "C", "A", "G", "A", "A", "T", "G", 
    "C", "G", "A", "T", "T", "A", "G", "G", "A", "A", "C", "A", 
    "A", "T", "C", "C", "A", "G", "A", "A", "T", "G", "C", "G", 
    "A", "T", "T", "A", "G", "G", "A", "A", "C", "A", "A", "T", 
    "C", "C", "A", "G", "A", "A", "T", "G", "C", "G", "A", "T", 
    "T", "A", "G", "G", "A", "A", "C", "A", "A", "T", "C", "C", 
    "A", "G", "A", "A", "T", "G", "C", "G", "A", "T"), alleles = c("C/T", 
    "G/A", "A/G", "A/G", "G/A", "C/A", "T/C", "G/A", "G/A", "C/T", 
    "C/G", "C/T", "A/G", "G/C", "A/G", "A/G", "T/C", "G/A", "C/T", 
    "G/C", "A/G", "T/C", "C/T", "G/A", "A/G", "A/G", "G/A", "C/A", 
    "T/C", "G/A", "G/A", "C/T", "C/G", "C/T", "A/G", "G/C", "A/G", 
    "A/G", "T/C", "G/A", "C/T", "G/C", "A/G", "T/C", "C/T", "G/A", 
    "A/G", "A/G", "G/A", "C/A", "T/C", "G/A", "G/A", "C/T", "C/G", 
    "C/T", "A/G", "G/C", "A/G", "A/G", "T/C", "G/A", "C/T", "G/C", 
    "A/G", "T/C", "C/T", "G/A", "A/G", "A/G", "G/A", "C/A", "T/C", 
    "G/A", "G/A", "C/T", "C/G", "C/T", "A/G", "G/C", "A/G", "A/G", 
    "T/C", "G/A", "C/T", "G/C", "A/G", "T/C", "C/T", "G/A", "A/G", 
    "A/G", "G/A", "C/A", "T/C", "G/A", "G/A", "C/T", "C/G", "C/T", 
    "A/G", "G/C", "A/G", "A/G", "T/C", "G/A", "C/T", "G/C", "A/G", 
    "T/C", "C/T", "G/A", "A/G", "A/G", "G/A", "C/A", "T/C", "G/A", 
    "G/A", "C/T", "C/G", "C/T", "A/G", "G/C", "A/G", "A/G", "T/C", 
    "G/A", "C/T", "G/C", "A/G", "T/C")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -132L))

我使用以下代码在ggplot中进行绘图:

gg <- ggplot(oi2, aes(x=trial, y=prop)) + 
      geom_point(aes(col=rs.))+
      labs(subtitle="Favorable allele over time", 
      y="allele frequency", 
      x="Groups", 
      title="Yield QTL markers",
      col='markers')

我得到以下图片:enter image description here

我真的很想一条曲线,将各组中的每个唯一标记连接起来。请选择一条曲线而不是一条直线。

下图是我想要的示例,尽管在这种情况下x轴是连续变量。我在上面的代码中尝试过类似geom_line(aes(group=1))之类的方法,但是它没有按预期工作。

enter image description here

1 个答案:

答案 0 :(得分:1)

像这样吗?

library(dplyr)

ggplot(oi2, aes(x = trial, y = prop, color = rs.)) + 
  geom_point() +
  geom_line(data = . %>%
              mutate(trial = as.numeric(trial)) %>%
              group_by(rs.) %>%

              # increase n for smoother line; can also try the other methods listed under
              # ?spline, though I find "natural" looks better than some of the rest: "fmm"'s 
              # curves are rather drastic, & "periodic" doesn't touch all the points
              summarise(x1 = list(spline(trial, prop, n = 50, method = "natural")[["x"]]),
                        y1 = list(spline(trial, prop, n = 50, method = "natural")[["y"]])) %>%

              tidyr::unnest(),
            aes(x = x1, y = y1)) +
  labs(subtitle="Favorable allele over time", 
       y="allele frequency", 
       x="Groups", 
       title="Yield QTL markers",
       col='markers')

plot

非主题:图表中可能有太多独特的颜色标记,无法用作区分它们的有效方法。您最好将其拆分为多个图表,每个图表中的标记较少。或只为要突出显示的颜色赋予独特的颜色,其余的保持灰色。