R 如何根据geom_line ggplot中的值绘制不同的颜色

R 如何根据geom_line ggplot中的值绘制不同的颜色,r,ggplot2,R,Ggplot2,我有这样一个情节: 我想要一条红色的线,如果它在最顶端的几何线之上,最好只有当它在线之上时。未能通过此geom_点颜色更改将执行以下操作: 最小数据集 structure(list(chr = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1

我有这样一个情节:

我想要一条红色的线,如果它在最顶端的几何线之上,最好只有当它在线之上时。未能通过此geom_点颜色更改将执行以下操作:

最小数据集

structure(list(chr = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1), leftPos = c(480000, 600000, 2520000, 2760000, 2880000, 
3000000, 3120000, 3480000, 3600000, 4440000, 5880000, 6120000, 
7680000, 8400000, 8520000, 8640000, 8760000, 8880000, 9000000, 
9120000, 10320000, 11280000, 12240000, 12360000, 12840000, 13080000, 
14760000, 17520000, 17880000, 18120000, 18240000, 18480000, 18600000, 
18720000, 19200000, 19320000, 20640000, 21000000, 21120000, 21840000, 
22560000, 22680000, 24360000, 24600000, 24720000, 25080000, 25200000, 
25440000, 25560000, 25680000, 25920000, 26160000, 26280000, 27000000, 
27360000, 27480000, 27840000, 27960000, 28320000, 28440000, 28560000, 
28680000, 29160000, 29280000, 29520000, 29760000, 29880000, 30000000, 
30120000, 30360000, 30840000, 31200000, 31680000, 32760000, 33000000, 
33360000, 33480000, 33600000, 33720000, 33840000, 34080000, 34200000, 
34320000, 34440000, 34920000, 35040000, 35160000, 35400000, 35520000, 
35640000, 35760000, 35880000, 36000000, 36120000, 36480000, 36720000, 
36840000, 37440000, 37680000, 37800000), Means = c(45.1721105399911, 
58.3236234466928, 10.701781072969, 81.1844097193854, 15.9764258576336, 
72.9857717254786, 100.909692015799, 42.3842652589944, 48.5758167032384, 
60.5307788344342, 9.72605226749078, 30.952302909114, 17.860141968134, 
30.8530526895484, 20.4628162381967, 116.623581135536, 38.6678878887186, 
78.9150157908195, 247.360337239763, 24.8838914877946, 23.429988222356, 
53.2615600034024, 52.3424770503346, 170.222454449925, 50.5781138394727, 
68.3087962976007, 89.4230029840393, 330.313625666045, 5.03584032143116, 
89.2643864875047, 27.8653665885641, 201.299758574135, -5.42494623204948, 
46.35601686698, 5.79103997302425, 112.341455197127, -4.28592603543802, 
7.41453012965141, 54.7629963052909, 23.3995967033728, 38.50598295151, 
51.1117491264575, 9.70064959823299, 145.915122377063, -1.93394721482472, 
223.668891284723, 0.967813870514552, 3.98951268889225, 103.991761780532, 
25.360339056567, 110.629872488342, 98.3711372064266, 4.29902758331359, 
-6.49705043591403, 119.885301893303, 11.7856758287972, 2.14257993846943, 
0.895276964913781, -4.13978377824383, 32.8265982947408, 102.894448568246, 
16.4150366530933, 3.50639248897849, 96.4740592727108, 163.538533768379, 
11.7417544456533, 271.336168593183, 21.9737935918722, 69.3505139183734, 
39.0812235546367, -0.0979741416490998, 11.2593492499262, 1.09352485411191, 
125.197079877443, 3.72187246405846, 60.0443035930762, 64.269810330157, 
16.8509153029321, -1.53165469412626, 17.8174419767041, 12.4924416240186, 
77.1154197241883, 50.0459666078323, 44.1948097160243, 7.87958496186204, 
24.2661434245405, 36.080204093108, 57.8551517801946, 130.247092097128, 
56.2802152403655, 24.1732000185415, 0.805669460114936, 20.805136831573, 
89.3895887677943, 12.0889852368913, 189.261728063453, 18.3254402417783, 
0.914727515857528, 26.4441526771417, 60.3908804957335)), .Names = c("chr", 
"leftPos", "Means"), row.names = c(NA, 100L), class = "data.frame")
我在其中一个图中使用的代码(facet\u wrap稍后提供)


你检查过你的例子了吗?它在新会话中给出了太多错误(如标题“Means”不在数据中,UL1也不在数据中)。更改为更合理地复制链接问题中的内容,这可能会为您提供另一个前进方向的提示<代码>f您是否检查了示例?它在新会话中给出了太多错误(如标题“Means”不在数据中,UL1也不在数据中)。更改为更合理地复制链接问题中的内容,这可能会为您提供另一个前进方向的提示<代码>f
UL1=400
LL1=0
Median1=200
ggplot(Zoutliers1,aes(x = leftPos,
                         y = as.numeric(Means),
                         group = chr,
                         xend = leftPos,
                         yend=0))+
  #geom_bar(stat="identity",fill = "red", size = 1, colour = "red")+
  geom_line()+
  #geom_segment(linetype= 1, colour = "#919191")+

  #ggtitle(TBBName)+
  ylim(-100,480)+
  facet_grid(.~chr, space="free_x", scales="free_x")+
  geom_hline(yintercept = UL1,size=0.5)+
  geom_hline(yintercept = Median1,size=0.5,colour='orange')+
  geom_hline(yintercept = LL1,size=0.5)+
  theme(panel.margin = unit(0.1, "lines"))+
  theme(axis.text.x = element_blank())+
  theme(panel.border = element_rect(fill=NA,color="darkred", size=0.5, 
                                    linetype="dashed"))+
  theme(axis.title = element_blank())