散点线组合图

scatter line combination plot

使用 ggplot2,我想创建一个图表,例如:

在我的数据中,x 指的是 x 轴,line 是实线(蓝色表示所有负 x 值,红色表示所有正 x 值)。 dot 指的是实心点(蓝色表示负 x 值,红色表示正 x 值),而 lower 表示相应垂直虚线的最低点, upper 表示垂直虚线的最高点。

如何创建这样的情节?

structure(list(x = c(-2, -1.98749995231628, -1.96249997615814, 
-1.9375, -1.91250002384186, -1.88750004768372, -1.86249995231628, 
-1.83749997615814, -1.8125, -1.78750002384186, -1.76250004768372, 
-1.75, -1.73749995231628, -1.71249997615814, -1.6875, -1.66250002384186, 
-1.63750004768372, -1.61249995231628, -1.58749997615814, -1.5625, 
-1.53750002384186, -1.51250004768372, -1.5, -1.48749995231628, 
-1.46249997615814, -1.4375, -1.41250002384186, -1.38750004768372, 
-1.36249995231628, -1.33749997615814, -1.3125, -1.28750002384186, 
-1.26250004768372, -1.25, -1.23749995231628, -1.21249997615814, 
-1.1875, -1.16250002384186, -1.13750004768372, -1.11249995231628, 
-1.08749997615814, -1.0625, -1.03750002384186, -1.01250004768372, 
-1, -0.987500011920929, -0.962499976158142, -0.9375, -0.912500023841858, 
-0.887499988079071, -0.862500011920929, -0.837499976158142, -0.8125, 
-0.787500023841858, -0.762499988079071, -0.75, -0.737500011920929, 
-0.712499976158142, -0.6875, -0.662500023841858, -0.637499988079071, 
-0.612500011920929, -0.587499976158142, -0.5625, -0.537500023841858, 
-0.512499988079071, -0.5, -0.487500011920929, -0.462500005960464, 
-0.4375, -0.412499994039536, -0.387499988079071, -0.362500011920929, 
-0.337500005960464, -0.3125, -0.287499994039536, -0.262499988079071, 
-0.25, -0.237499997019768, -0.212500005960464, -0.1875, -0.162499994039536, 
-0.137500002980232, -0.112499997019768, -0.0874999985098839, 
-0.0625, -0.0375000014901161, -0.0125000001862645, 0, 0.0125000001862645, 
0.0375000014901161, 0.0625, 0.0874999985098839, 0.112499997019768, 
0.137500002980232, 0.162499994039536, 0.1875, 0.212500005960464, 
0.237499997019768, 0.25, 0.262499988079071, 0.287499994039536, 
0.3125, 0.337500005960464, 0.362500011920929, 0.387499988079071, 
0.412499994039536, 0.4375, 0.462500005960464, 0.487500011920929, 
0.5, 0.512499988079071, 0.537500023841858, 0.5625, 0.587499976158142, 
0.612500011920929, 0.637499988079071, 0.662500023841858, 0.6875, 
0.712499976158142, 0.737500011920929, 0.75, 0.762499988079071, 
0.787500023841858, 0.8125, 0.837499976158142, 0.862500011920929, 
0.887499988079071, 0.912500023841858, 0.9375, 0.962499976158142, 
0.987500011920929, 1, 1.01250004768372, 1.03750002384186, 1.0625, 
1.08749997615814, 1.11249995231628, 1.13750004768372, 1.16250002384186, 
1.1875, 1.21249997615814, 1.23749995231628, 1.25, 1.26250004768372, 
1.28750002384186, 1.3125, 1.33749997615814, 1.36249995231628, 
1.38750004768372, 1.41250002384186, 1.4375, 1.46249997615814, 
1.48749995231628, 1.5, 1.51250004768372, 1.53750002384186, 1.5625, 
1.58749997615814, 1.61249995231628, 1.63750004768372, 1.66250002384186, 
1.6875, 1.71249997615814, 1.73749995231628, 1.75, 1.76250004768372, 
1.78750002384186, 1.8125, 1.83749997615814, 1.86249995231628, 
1.88750004768372, 1.91250002384186, 1.9375, 1.96249997615814, 
1.98749995231628, 2, 2.01250004768372, 2.03749990463257, 2.0625, 
2.08750009536743, 2.11249995231628, 2.13750004768372, 2.25), 
    line = c(6.19665648828238, 6.19651712178167, 6.19621877565744, 
    6.19589779355107, 6.19555822802314, 6.19520396126029, 6.19483876255868, 
    6.19446621215612, 6.19408971880521, 6.19371256255139, 6.1933379140131, 
    6.19315246872322, 6.19296873929168, 6.19260780157523, 6.19225782536747, 
    6.19192132530987, 6.19160058433265, 6.19129786824334, 6.19101523747572, 
    6.19075458730955, 6.1905176208875, 6.19030599406495, 6.19021009971068, 
    6.19012107982284, 6.18996416537474, 6.18983643304462, 6.18973882360151, 
    6.18967214587648, 6.18963708868792, 6.18963417652327, 6.18966374532725, 
    6.18972601430549, 6.18982103866934, 6.18988080854996, 6.18994871096957, 
    6.19010879603549, 6.19030086125351, 6.19052435835488, 6.19077858289285, 
    6.19106264908957, 6.1913755315152, 6.19171607226086, 6.19208294321403, 
    6.19247463280608, 6.19267929200941, 6.19288953990191, 6.19332584515705, 
    6.19378162412825, 6.19425477444115, 6.1947430330201, 6.1952440133489, 
    6.19575514867015, 6.19627371697606, 6.19679686478826, 6.19732156685725, 
    6.19758351632048, 6.19784465173068, 6.19836279145431, 6.19887250526611, 
    6.19937015747258, 6.1998519635449, 6.20031397851226, 6.20075210570791, 
    6.20116209780167, 6.20153955250402, 6.20187991327731, 6.20203471717777, 
    6.20217846844031, 6.20243035630681, 6.20263055772745, 6.20277390270312, 
    6.20285506299053, 6.20286856418025, 6.20280877262717, 6.20266990252962, 
    6.20244601307972, 6.20213101335409, 6.20193739973902, 6.20171865390325, 
    6.20120253597035, 6.20057610403579, 6.19983265098427, 6.19896531525375, 
    6.19796708084205, 6.19683077952508, 6.19554908841501, 6.19411453122193, 
    6.19251947775124, NA, 6.19669552219094, 6.19692838534326, 
    6.19716536482625, 6.19740646066496, 6.19765167285938, 6.19790100148535, 
    6.19815444631536, 6.19841200765889, 6.19867368535506, 6.19893947925528, 
    6.19907391991806, 6.19920938949587, 6.19948341628678, 6.19976155940581, 
    6.20004381890202, 6.20033019475087, 6.20062068660608, 6.20091529513569, 
    6.20121402006702, 6.20151686135099, 6.20182381898761, 6.20197884124048, 
    6.20213489260605, 6.20245008331879, 6.20276938964251, 6.20309281231889, 
    6.20342035208956, 6.20375200747123, 6.20408778004534, 6.20442766813229, 
    6.2047716725719, 6.20511979420394, 6.20529539822004, 6.20547203134883, 
    6.20582838568617, 6.20618885553636, 6.20655344164105, 6.20692214513446, 
    6.20729496414073, 6.20767190014315, 6.20805295185471, 6.20843811982077, 
    6.20882740497927, 6.20902359085674, 6.20922080678485, 6.20961832351442, 
    6.21001995708735, 6.21042570671849, 6.21083557280043, 6.21124955720903, 
    6.21166765619254, 6.21208987123427, 6.21251620272678, 6.21294665067009, 
    6.21316341915059, 6.21338121694006, 6.21381989739237, 6.21426269429548, 
    6.21470960764936, 6.21516063706147, 6.21561578519281, 6.2160750475065, 
    6.21653842627097, 6.21700592148623, 6.21747753236714, 6.21771488310157, 
    6.21795326235985, 6.21843310692747, 6.21891706716075, 6.21940514384481, 
    6.21989733697966, 6.22039364922631, 6.2208940744776, 6.22139861696481, 
    6.22190727511768, 6.22242005050646, 6.2226779827097, 6.22293694422192, 
    6.22345795172714, 6.22398307568316, 6.22451231608996, 6.22504567294755, 
    6.22558314813181, 6.22612473789098, 6.2266704433158, 6.2272202651914, 
    6.2277742035178, 6.22805271797497, 6.22833226174112, 6.22889442952294, 
    6.22946071986272, 6.23003112586814, 6.23060564378746, 6.23118428269446, 
    6.23383908889738), lower = c(6.17785787582397, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, 6.17318391799927, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, 6.17153835296631, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, 6.17408847808838, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, 6.17824029922485, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, 6.18390989303589, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, 6.18862676620483, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, 6.19003582000732, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, 6.18658685684204, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, 6.190993309021, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, 6.19340229034424, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, 6.19517421722412, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, 6.19699621200562, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, 6.19884967803955, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, 6.20038080215454, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, 6.20116233825684, NA, NA, NA, NA, NA, NA, 6.20085954666138
    ), dot = c(6.21545505523682, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, 6.21312093734741, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, 6.20888185501099, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, 6.20567321777344, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, 6.20711851119995, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, 6.21125745773315, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, 6.2154426574707, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, 6.21383905410767, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 6.21156120300293, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 6.21296405792236, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 6.21718883514404, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 6.22287273406982, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 6.22933053970337, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 6.23657989501953, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 6.24497509002686, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 6.25494289398193, 
    NA, NA, NA, NA, NA, NA, 6.26681852340698), upper = c(6.20049048162888, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 6.20049048162888, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 6.20049048162888, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 6.21183848407275, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 6.20049048162888, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 6.20049048162888, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 6.20049048162888, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 6.22168206950706, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, 6.20049048162888, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, 6.20049048162888, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, 6.20049048162888, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, 6.20049048162888, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, 6.20049048162888, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, 6.20049048162888, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, 6.20049048162888, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, 6.22271156090545, NA, NA, NA, NA, 
    NA, NA, 6.20049048162888)), row.names = c(NA, -184L), class = c("tbl_df", 
"tbl", "data.frame"))

你会像这样创建情节,但你的数据似乎出了问题,因为你的 dot 值通常高于 upper 值,而你的line 值与您的 dot 值非常不同:

library(tidyverse)

df %>% 
  mutate(color = ifelse(x < 0, 'deepskyblue4', 'orangered')) %>%
  ggplot(aes(x, line, color = color)) +
  geom_line(na.rm = TRUE) +
  geom_point(aes(y = dot), na.rm = TRUE) +
  geom_linerange(aes(ymin = lower, ymax = upper), linetype = 2) +
  scale_color_identity()