如何创建垃圾箱而不是连续色标?

时间:2014-07-31 21:50:07

标签: r ggplot2

我正在使用此代码绘制下图。基本上,我想绘制一个变量的90-100百分位值。

foo3<-subset(foo1, mnqtp>quantile(mnqtp, 0.90)) #Values greater than 90th quantile
breaks <- quantile(foo1$mnqtp,probs = seq(0.90, 1, by = 0.01))

ggplot() + 
  geom_polygon( data=usamap, aes(x=long, y=lat,group=group),colour="black", fill="white" )+
  geom_point(data=foo3,aes(x=lon,y=lat,group=index,color=mnqtp))+
  scale_colour_gradientn(breaks = as.vector(breaks),labels = names(breaks),
                         values = as.vector(breaks),colours = myPalette(10))+
  xlab('Longitude')+
  ylab('Latitude')+
  coord_map(projection = "mercator")+
  #coord_fixed(ratio = 1.5)+
  theme_bw()+
  theme(legend.position = c(.93,.20),panel.grid.major = element_line(colour = "#808080"))+
  ggsave("x.png",width=10, height=8,dpi=300)

enter image description here

但我不想持续规模。

如何为十个分位数设置分档而不是连续分数,每个分位数都有不同的颜色?

dput(droplevels(头(foo1)))

structure(list(id = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "Csb", class = "factor"), 
fips = c(4011L, 4011L, 4011L, 4011L, 4011L, 4011L), gauge = c(9444500L, 
9444500L, 9444500L, 9444500L, 9442000L, 9442000L), lat = c(33.049444, 
33.049444, 33.049444, 33.049444, 32.965833, 32.965833), lon = c(-109.295278, 
-109.295278, -109.295278, -109.295278, -109.309722, -109.309722
), start = structure(c(5L, 2L, 4L, 1L, 3L, 6L), .Label = c("1/12/1993 7:00", 
"1/19/1993 2:15", "1/30/2008 9:15", "11/12/1994 13:30", "3/2/1991 7:30", 
"9/22/1997 20:45"), class = "factor"), end = structure(c(5L, 
2L, 4L, 1L, 3L, 6L), .Label = c("1/12/1993 13:30", "1/19/1993 6:30", 
"1/30/2008 9:30", "11/13/1994 11:00", "3/2/1991 20:30", "9/23/1997 17:00"
), class = "factor"), peakq = c(438.9111, 1248.7729, 600.3171, 
404.9309, 267.0279, 478.5547), peakt = structure(c(5L, 2L, 
4L, 1L, 3L, 6L), .Label = c("1/12/1993 8:45", "1/19/1993 6:30", 
"1/30/2008 9:23", "11/12/1994 21:15", "3/2/1991 13:15", "9/23/1997 8:15"
), class = "factor"), dt = c(5.75, 4.25, 7.75, 1.75, 0.1333, 
11.5), HUC = c(15040004L, 15040004L, 15040004L, 15040004L, 
15040002L, 15040002L), agency = structure(c(1L, 1L, 1L, 1L, 
1L, 1L), .Label = "USGS", class = "factor"), regulation = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = "Undefined", class = "factor"), 
gname = structure(c(2L, 2L, 2L, 2L, 1L, 1L), .Label = c("GILA RIVER NEAR CLIFTON, AZ.", 
"SAN FRANCISCO RIVER AT CLIFTON, AZ."), class = "factor"), 
area = c(7163.9071, 7163.9071, 7163.9071, 7163.9071, 10385.8523, 
10385.8523), carea = c(7156.1371, 7156.1371, 7156.1371, 7156.1371, 
0, 0), q2 = c(196.2357, 196.2357, 196.2357, 196.2357, 168.2021, 
168.2021), q5 = c(512.5349, 512.5349, 512.5349, 512.5349, 
325.6437, 325.6437), q10 = c(855.1688, 855.1688, 855.1688, 
855.1688, 475.723, 475.723), q25 = c(1500.7929, 1500.7929, 
1500.7929, 1500.7929, 739.0697, 739.0697), q50 = c(2171.9021, 
2171.9021, 2171.9021, 2171.9021, 1002.4164, 1002.4164), q100 = c(3029.9026, 
3029.9026, 3029.9026, 3029.9026, 1330.8918, 1330.8918), q200 = c(4134.2596, 
4134.2596, 4134.2596, 4134.2596, 1741.4861, 1741.4861), q500 = c(6059.8052, 
6059.8052, 6059.8052, 6059.8052, 2449.4072, 2449.4072), action = c(297.3269, 
297.3269, 297.3269, 297.3269, 263.6298, 263.6298), minor = c(509.7032, 
509.7032, 509.7032, 509.7032, -0.0283, -0.0283), moderate = c(736.238, 
736.238, 736.238, 736.238, -0.0283, -0.0283), major = c(2035.9813, 
2035.9813, 2035.9813, 2035.9813, -0.0283, -0.0283), usgs_area = c(7163.91, 
7163.91, 7163.91, 7163.91, 10385.9, 10385.9), est_area = c(7255, 
7255, 7255, 7255, 10272, 10272), error = c(1.27155, 1.27155, 
1.27155, 1.27155, 1.09622, 1.09622), el = c(0.521385, 0.521385, 
0.521385, 0.521385, 0.434142, 0.434142), k = c(0.213505, 
0.213505, 0.213505, 0.213505, 0.148031, 0.148031), bl = c(184338, 
184338, 184338, 184338, 263421, 263421), r = c(2054.38, 2054.38, 
2054.38, 2054.38, 2066.19, 2066.19), rr = c(0.0111447, 0.0111447, 
0.0111447, 0.0111447, 0.00784367, 0.00784367), si = c(0.00757568, 
0.00757568, 0.00757568, 0.00757568, 0.00513591, 0.00513591
), rdd = c(0.816995, 0.816995, 0.816995, 0.816995, 0.928348, 
0.928348), rbm = c(5460, 5460, 5460, 5460, 8809, 8809), rfocf = c(0.762154, 
0.762154, 0.762154, 0.762154, 0.848173, 0.848173), rrg = c(1.67842, 
1.67842, 1.67842, 1.67842, 1.91814, 1.91814), precip = c(529.942, 
529.942, 529.942, 529.942, 465.025, 465.025), temp = c(10.4292, 
10.4292, 10.4292, 10.4292, 12.1328, 12.1328), fd = c(13, 
4.25, 21.5, 6.5, 0.25, 20.25), tp = c(5.75, 4.25, 7.75, 1.75, 
0.133333333333333, 11.5), rt = c(7.25, 0, 13.75, 4.75, 0.116666666666667, 
8.75), nfd = c(0.00181465223076385, 0.000593251690826644, 
0.00300115561241714, 0.000907326115381926, 2.40712069436997e-05, 
0.00194976776243968), ntp = c(0.000802634640530165, 0.000593251690826644, 
0.00108181190680153, 0.000244280107987442, 1.28379770366399e-05, 
0.00110727551941019), nrt = c(0.00101201759023369, 0, 0.00191934370561561, 
0.000663046007394484, 1.12332299070599e-05, 0.000842492243029491
), nq = c(0.0612670005170782, 0.17431450220788, 0.0837974434369759, 
0.0565237508454011, 0.0257107353625662, 0.0460775568703206
), Group.1 = c(4011L, 4011L, 4011L, 4011L, 4011L, 4011L), 
county = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "Greenlee", class = "factor"), 
class = c("Csb", "Csb", "Csb", "Csb", "Csb", "Csb"), prop = c(0.742, 
0.742, 0.742, 0.742, 0.742, 0.742), state = c("arizona", 
"arizona", "arizona", "arizona", "arizona", "arizona"), tpwt = c(7, 
8, 6, 9, 10, 5), nqwt = c(3L, 7L, 4L, 3L, 2L, 3L), index = c(10, 
15, 10, 12, 12, 8), nqtp = c(0.0106551305247092, 0.0410151769900895, 
0.0108125733467066, 0.0322992861973721, 0.192830515219247, 
0.00400674407568005), mIndex = c(11.2222222222222, 11.2222222222222, 
11.2222222222222, 11.2222222222222, 10, 10), mnqtp = c(0.0190053539269603, 
0.0190053539269603, 0.0190053539269603, 0.0190053539269603, 
0.0685493678257457, 0.0685493678257457)), .Names = c("id", 
"fips", "gauge", "lat", "lon", "start", "end", "peakq", "peakt", 
"dt", "HUC", "agency", "regulation", "gname", "area", "carea", 
"q2", "q5", "q10", "q25", "q50", "q100", "q200", "q500", "action", 
"minor", "moderate", "major", "usgs_area", "est_area", "error", 
"el", "k", "bl", "r", "rr", "si", "rdd", "rbm", "rfocf", "rrg", 
"precip", "temp", "fd", "tp", "rt", "nfd", "ntp", "nrt", "nq", 
"Group.1", "county", "class", "prop", "state", "tpwt", "nqwt", 
"index", "nqtp", "mIndex", "mnqtp"), row.names = 99096:99101, class = "data.frame")

1 个答案:

答案 0 :(得分:0)

makeQuantiles <- function(x, probs = seq(0.9, 1, by = 0.02)) {
  cut(x, breaks = qu <- quantile(x, probs = probs),
      labels = names(qu)[-1], include.lowest = TRUE)
}
foo1$quantile<-makeQuantiles(foo1$mnqtp)
ggplot() + 
  geom_polygon( data=usamap, aes(x=long, y=lat,group=group),colour="black", fill="white" )+
  geom_point(data=na.omit(foo1),aes(x=lon,y=lat,color=quantile))+
  coord_map(projection = "mercator")+
  theme_bw()+
  theme(legend.position = c(.93,.20),panel.grid.major = element_line(colour = "#808080"))+
  ggsave("useventsmap_mnqtp_90.png",width=10, height=8,dpi=300)