邮政编码区域在R中绘图 - 如何添加图例

时间:2017-07-18 13:28:58

标签: r

我是R的新手,需要一些帮助。

你可以帮我解决下面的问题吗?我想在0到1的情节旁边添加渐变图例,显示不同的颜色作为值的变化,但这是我能够得到的最好的。还有一些提示如何在地图内添加带有邮政编码的文字?感谢。

Row Labels  Success/Failed ratio    N of coll
LD  1   3
ZE  1   2
WS  0.79    19
ML  0.75    12
HS  0.75    4
TQ  0.74    38
WN  0.73    15
CA  0.71    28
HU  0.7 33
FY  0.69    16
HG  0.69    16
IV  0.68    19
DL  0.68    25
CB  0.68    115
TS  0.67    46
IP  0.67    87
AB  0.67    66
NP  0.67    45
FK  0.67    18
IM  0.67    9
SM  0.66    50
HD  0.66    32
EN  0.66    61
CO  0.65    52
ME  0.65    54
PE  0.64    266
EX  0.64    81
WV  0.63    49
JE  0.63    24
NE  0.62    148
YO  0.62    47
DE  0.62    78
LN  0.61    36
SN  0.61    109
IG  0.6 63
NR  0.6 90
SP  0.59    37
BA  0.59    93
UB  0.59    127
TN  0.59    95
BT  0.59    180
BD  0.59    51
HP  0.59    126
TA  0.59    46
PO  0.58    113
DH  0.58    55
WD  0.58    102
BH  0.57    96
DG  0.57    14
CV  0.57    225
RG  0.57    255
BN  0.56    158
DY  0.56    48
HA  0.56    148
W   0.56    359
WA  0.56    77
DA  0.55    38
CT  0.55    62
GU  0.55    231
RH  0.55    132
BL  0.55    33
HX  0.55    11
BS  0.54    184
SS  0.54    46
EH  0.54    185
DT  0.54    37
G   0.54    137
B   0.54    283
LU  0.54    41
NG  0.54    97
OX  0.53    208
S   0.53    179
CM  0.53    100
DD  0.53    17
GL  0.53    87
AL  0.53    89
HR  0.53    38
LS  0.52    122
TF  0.52    21
RM  0.52    44
SL  0.52    155
MK  0.52    136
SY  0.52    46
DN  0.52    81
N   0.52    191
M   0.52    226
SR  0.52    29
SK  0.52    64
BB  0.51    140
KY  0.51    41
WF  0.51    51
PR  0.51    63
L   0.51    81
KT  0.5 185
CF  0.5 118
ST  0.5 84
TR  0.5 46
CW  0.5 44
TD  0.5 12
P   0.5 2
SW  0.5 317
LL  0.49    49
CH  0.49    43
E   0.49    275
EC  0.48    364
PA  0.48    27
SO  0.48    157
CR  0.48    84
PL  0.48    61
SG  0.47    59
KA  0.47    15
LA  0.47    43
SA  0.46    78
LE  0.46    194
TW  0.45    125
OL  0.44    41
SE  0.44    297
NN  0.43    143
NW  0.42    236
WC  0.41    138
WR  0.38    73
BR  0.37    62
GY  0.26    35
PH  0.23    13

来自csv的原始数据集有3列以下:

fill = color[1]

1 个答案:

答案 0 :(得分:1)

你走了。将sf与新的ggplot或我的misc包中的内容一起用于基本图表。

# collection data set load + post codes lo la - 2016
df2016 <- read.table(stringsAsFactors=FALSE, header=TRUE, text="
name value amount
LD  1   3
ZE  1   2
WS  0.79    19
# YOUR OTHER VALUES FROM ABOVE
PH  0.23    13")

if(FALSE){ # don't run when sourcing file
# Download a shapefile of postal codes into your working directory
download.file(
  "http://www.opendoorlogistics.com/wp-content/uploads/Data/UK-postcode-boundaries-Jan-2015.zip",
  "postal_shapefile.zip"
)
# Unzip and read the shapefile
unzip("postal_shapefile.zip")
}
# install.packages("sf")
postal <- sf::st_read("Distribution/Areas.shp") 

# Join your data to the shapefile
postal2 <- merge(postal, df2016, by="name")

#devtools::install_github("tidyverse/ggplot2") # need newer ggplot2 version for geom_sf
library(ggplot2)
ggplot(postal2) + geom_sf(aes(fill = value)) 


# Want to remain in base graphs?
#install.packages("berryFunctions")
library(berryFunctions)
cols <- seqPal(n=100)
cls <- classify(postal2$value, breaks=100)$index
plot(postal2[,c("value","geometry")], col=cols[cls], graticule=TRUE, axes=TRUE) # ?sf::plot_sf
colPointsLegend(postal2$value, colors=cols, horizontal=FALSE, title="UK value")