我想在ggplot中绘制一些地图数据,其中根据连续变量的值填充多边形。我遇到的一个问题是,数据中只有极少数极端异常值(大约3000个),可以防止大多数其他观察结果显示出足够有意义的颜色变化。
基本上我希望能够为我的大部分数据指定颜色方案,然后将所有正异常值亮蓝色和所有负异常值着红色。
示例代码 - 例如,假设大多数数据介于-0.01和0.01之间,但有一些值低于/高于这些级别......
library(ggplot2)
DAT <- structure(list(long = c(848025.138769486, 827715.400155344, 819783.06692123,
811273.46685221, 807112.24435793, 825218.389316822, 824653.263676394,
851268.022958262, 854876.20206792, 848025.138769486, 824710.033713034,
808060.98832434, 791388.306466439, 791011.02400712, 790256.857765555,
790077.133379578, 792915.8857683, 803799.11479841, 812042.67207171,
811086.452951819, 818801.811870496, 824623.483334553, 829923.458416495,
844732.373710393, 843509.962977133, 840793.468791028, 844661.128104187,
841054.158477243, 824318.536510595, 824710.033713034, 957744.153625377,
964378.49410997, 963723.372709918, 963536.949007208, 950656.36657876,
923479.701493794, 918733.432704199, 950416.579446804, 951806.914646236,
952020.610154412, 957744.153625377, 838474.166485858, 839637.34900613,
846161.553188089, 854105.657535852, 850749.837928768, 851268.022958262,
824653.263676394, 825218.389316822, 807112.24435793), lat = c(-755063.209671518,
-751075.227428769, -758486.024742793, -742209.818502709, -725584.096412241,
-723927.958082718, -719335.285579824, -716264.904081879, -747370.371610989,
-755063.209671518, -607299.311749675, -601135.889250199, -605563.568360578,
-602482.006717006, -602560.163831817, -601169.088819494, -595490.118653201,
-589823.673980013, -577642.515107293, -574207.831414412, -570499.93978401,
-555676.769699399, -554574.027849141, -569849.400696679, -581914.546950335,
-587110.213020993, -586640.550459734, -595326.416960721, -603469.770252399,
-607299.311749675, -686468.891810573, -696059.793425604, -696994.182842627,
-698894.537935268, -702169.738050773, -705710.496557967, -666147.504830981,
-662203.856856129, -666852.009638632, -667571.668902733, -686468.891810573,
-684524.444323129, -694047.01193517, -705551.617269712, -711060.296586236,
-711747.897688833, -716264.904081879, -719335.285579824, -723927.958082718,
-725584.096412241), group = c("0.1", "0.1", "0.1", "0.1", "0.1",
"0.1", "0.1", "0.1", "0.1", "0.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", "2.1", "2.1",
"2.1", "2.1", "2.1", "2.1", "2.1", "2.1", "2.1", "2.1", "2.1",
"3.1", "3.1", "3.1", "3.1", "3.1", "3.1", "3.1", "3.1", "3.1"
), VALUE = c(0.0197178619295337, 0.0197178619295337, 0.0197178619295337,
0.0197178619295337, 0.0197178619295337, 0.0197178619295337, 0.0197178619295337,
0.0197178619295337, 0.0197178619295337, 0.0197178619295337, 0.0144402919365254,
0.0144402919365254, 0.0144402919365254, 0.0144402919365254, 0.0144402919365254,
0.0144402919365254, 0.0144402919365254, 0.0144402919365254, 0.0144402919365254,
0.0144402919365254, 0.0144402919365254, 0.0144402919365254, 0.0144402919365254,
0.0144402919365254, 0.0144402919365254, 0.0144402919365254, 0.0144402919365254,
0.0144402919365254, 0.0144402919365254, 0.0144402919365254, -0.00812118892018265,
-0.00812118892018265, -0.00812118892018265, -0.00812118892018265,
-0.00812118892018265, -0.00812118892018265, -0.00812118892018265,
-0.00812118892018265, -0.00812118892018265, -0.00812118892018265,
-0.00812118892018265, 0.00751936235807205, 0.00751936235807205,
0.00751936235807205, 0.00751936235807205, 0.00751936235807205,
0.00751936235807205, 0.00751936235807205, 0.00751936235807205,
0.00751936235807205)), .Names = c("long", "lat", "group", "VALUE"
), row.names = c(6L, 5L, 4L, 10L, 7L, 8L, 9L, 2L, 3L, 1L, 151L,
150L, 156L, 162L, 168L, 163L, 159L, 154L, 160L, 149L, 161L, 158L,
164L, 152L, 153L, 155L, 165L, 167L, 166L, 157L, 226L, 223L, 233L,
232L, 225L, 227L, 224L, 230L, 228L, 229L, 231L, 258L, 253L, 257L,
262L, 254L, 256L, 261L, 260L, 269L), class = "data.frame")
#Construct plot, placing limits on color scale. How to make outliers bright red or blue?
gmp <- ggplot(data=DAT, aes(x=long,y=lat,group=group)) +
scale_fill_gradient2(low=rgb(0.8,0.2,0.2),high=rgb(0.2,0.3,0.8),mid=rgb(0.9,0.9,0.9),limits=c(-0.01,0.01)) +
geom_polygon(aes(fill=VALUE,group=group),colour=NA, size = .3) +
coord_fixed()
plot(gmp)
答案 0 :(得分:4)
这是一个选项:
为每个条件创建子集(最小极值,最大极值和中间范围)。我为您的数据添加了最小极限以供说明。
DAT$VALUE[DAT$group == 0.1] <- -0.019
DAT.mid <- DAT[abs(DAT$VALUE) < 0.01,]
DAT.max <- DAT[DAT$VALUE >= 0.01,]
DAT.min <- DAT[DAT$VALUE <= -0.01,]
然后用中间数据构建基础图:
base <- ggplot(DAT.mid, aes(x=long,y=lat,group=group)) +
geom_polygon(aes(fill=VALUE,group=group),colour=NA, size = .3) +
scale_fill_gradient2(low=rgb(0.8,0.2,0.2),high=rgb(0.2,0.3,0.8),
mid=rgb(0.9,0.9,0.9),limits=c(-0.01,0.01)) +
coord_fixed()
base
然后,您可以使用您想要的任何颜色添加极端的图层:
ext <- base +
geom_polygon(data = DAT.max, fill = "blue") +
geom_polygon(data = DAT.min, fill = "red")
ext
棘手的一点是得到代表你极端的传奇。从here开始,您可以添加映射到不存在的aes
的不可见的geoms,然后使用图例来获取它想要的结果。在这里,我习惯geom_point
。对于第一个,我将大小映射到“&gt; = 0.01”,这将为我们提供该文本的图例,并且类似于第二个,除了我使用alpha来获取另一个图例条目。然后,您可以使用guides()
覆盖图例属性,并获得具有正确填充的正方形。它并不完美,但在大多数情况下都有效。
gmp <- ext +
geom_point(data = DAT.max, aes(size = ">= 0.01", shape = NA), colour = "blue") +
geom_point(data = DAT.min, aes(alpha = "<= -0.01", shape = NA), colour = "red") +
guides(size = guide_legend(title = "", override.aes = list(shape = 15, size = 10)),
alpha = guide_legend(title = "", override.aes = list(shape = 15, size = 10, alpha = 1)))
gmp