如何在ggplot2中反转一半的色阶?

时间:2016-08-01 10:38:01

标签: r ggplot2

usamap <- map_data("state")
myPalette <- colorRampPalette(rev(brewer.pal(10, "Spectral")))
ggplot() + 
  geom_polygon( data=usamap, aes(x=long, y=lat,group=group),colour="black", fill="white" )+
  geom_point(data=allgauges,aes(x=lon,y=lat,color=mf.gauge))+ #0-1

  scale_colour_gradientn(colours = c("red", "white" ,"blue"),
                         values=c(1.0,0.75,0))+
  xlab('Longitude')+
  ylab('Latitude')+
  coord_map(projection = "mercator")+
  theme_bw()+
  theme(legend.position = c(.93,.20),panel.grid.major = element_line(colour = "#808080"))

我想让乐队的蓝色部分颠倒过来。这个想法是用两个增加的色标来显示小于0.75和0.75-1的任何东西。

enter image description here

head(allgauges,4)
    fips   gauge      lat       lon            start              end    peakq           peakt    dt
1: 33011 1094000 42.85750 -71.50667   4/2/2004 12:45   4/3/2004 18:15 172.7328   4/3/2004 0:15 11.50
2: 25017 1100600 42.56806 -71.21528   4/24/2012 9:15  4/24/2012 19:00   7.1925 4/24/2012 14:15  5.00
3: 25017 1096000 42.63417 -71.65833  3/23/2001 10:30  3/23/2001 22:15  45.0238 3/23/2001 15:00  4.50
4: 25021 1105600 42.19028 -70.94528 10/25/2005 17:15 10/26/2005 22:00   4.3042 10/26/2005 5:30 12.25
       HUC agency                                   gname     area    carea      q2       q5      q10
1: 1070002   USGS         SOUHEGAN RIVER AT MERRIMACK, NH 442.8880   0.0000 90.8971 136.7704 173.5823
2: 1070002   USGS     SHAWSHEEN RIVER NEAR WILMINGTON, MA  94.5346  94.5346 13.5355  20.3598  30.2990
3: 1070004   USGS  SQUANNACOOK RIVER NEAR WEST GROTON, MA 170.6802 164.9822 36.5287  54.0852  77.3050
4: 1090001   USGS OLD SWAMP RIVER NEAR SOUTH WEYMOUTH, MA  11.6549  11.6549  0.0000   0.0000  12.4311
        q25      q50     q100     q200     q500   action    minor moderate    major  regulation   alpha
1: 228.5170 276.0893 328.4754 390.7725 481.3864 101.3743 134.7882 212.0932 302.9903 unregulated 6.26885
2:  39.9268  48.1386  57.2000  60.3149  81.8357   6.8810  17.5564  25.3436  46.1565 unregulated 2.82139
3: 102.2238 123.1783 146.3981 148.9466 209.8278  40.7763  70.2258 136.4872 158.5743 unregulated 4.56452
4:  18.3493  23.8711  30.2990   0.0000  50.6872   2.4636   4.3042  13.5921  13.5921 unregulated 2.11838
       beta       cc usgs_area est_area     error       el        k       rl       rr       si      rdd rbm
1: 0.493860 0.749571  442.8880      102 76.969300 0.748627 0.249608 20313.70 0.013788 0.007105 0.243173  95
2: 0.480760 0.920398   94.5346       98  3.665750 0.741916 0.297481 18242.60 0.003716 0.002121 0.734508  73
3: 0.445343 0.896841  170.6800      171  0.187368 0.762164 0.243889 26556.30 0.016847 0.013121 0.967351 170
4: 0.437781 0.900491   11.6549       14 20.121200 0.977205 0.441559  5828.43 0.006621      NaN 0.933290  15
      rfocf slopeoutlet  precip     temp cnbasin cncell coemcell imperviousbasin imperviouscell kfact
1: 0.214501    0.008768 1143.90  8.47924 61.9223     39    20.00        0.015515        5.85470  0.17
2: 0.772204    0.004678 1228.70  9.49196 70.9394     80    66.67        0.766365       41.35030  0.18
3: 0.996015    0.009182 1218.29  8.26621 64.0291     36    10.00        0.048869        2.37394  0.25
4: 1.287010    0.008404 1269.49 10.24680 68.8667     42    10.00        0.191742       15.13900  0.16
   rockdepth rockvolume bpartexture       dc ldd lbm lfocf ruggedness    fd    tp    rt        nfd
1:       144         14        4.26 11451.80   0   0     0   0.051290 29.50 11.50 18.00 0.06660826
2:       146          8        4.74 11227.20   0   0     0   0.041299  9.75  5.00  4.75 0.10313684
3:       102         18        5.33 14798.60   0   2     2   0.316433 11.75  4.50  7.25 0.06884220
4:       128         31        4.74  4370.19   0   1     1   0.027636 28.75 12.25 16.50 2.46677363
          ntp        nrt         nq           f Group.1       county  class  prop         state month year
1: 0.02596593 0.04064233 0.39001463 0.014010510   33011 Hillsborough Dfb(E) 1.000 new hampshire   Apr 2004
2: 0.05289069 0.05024615 0.07608325 0.000659018   25017    Middlesex Dfb(E) 0.554 massachusetts   Apr 2012
3: 0.02636510 0.04247710 0.26379041 0.005530160   25017    Middlesex Dfb(E) 0.554 massachusetts   Mar 2001
4: 1.05106007 1.41571356 0.36930390 0.012891836   25021      Norfolk    Dfa 0.898 massachusetts   Oct 2005
   season maxseason          mf  mf.gauge fness.gauge
1:    MAM       MAM 0.014010510 0.6482717  Non-flashy
2:    MAM       MAM 0.002092467 0.3438448  Non-flashy
3:    MAM       MAM 0.016664304 0.6731352  Non-flashy
4:    SON       MAM 0.020501880 0.7101273  Non-flashy

1 个答案:

答案 0 :(得分:0)

这应该有效:

old_pal<-brewer.pal(10, "RdYlBu")
new_pal<-c(old_pal[1:5],rev(old_pal[6:10]))
myPalette <- colorRampPalette(new_pal)

然后当您使用时:

  scale_colour_gradientn(colours =myPalette(100),
                         values=c(1.0,0.75,0))+

不确定这样的黑色调色板是不是一个好主意。