与ggplot的调色板

Max*_*ian 3 plot r ggplot2

为了生成可复制的示例,我将不得不提交shapefile数据等,这对您来说很麻烦(下载数据等),所以这里只是提供最后一部分而且是关于 ggplot

这是示例代码:

cols <- colorRampPalette(c("darkgreen","yellow","red"), space = "rgb")
myPal <- cols(11) 

ggplot(data=df, aes(x=long, y=lat, group=group)) + 
   geom_polygon(aes(fill = measure))+    # draw polygons
   coord_equal() +
   scale_x_continuous(breaks = as.numeric(levels(factor(df$measure))))+
   scale_fill_manual(values = myPal)+
   labs(title="mesure level", x="", y="")+
   theme(axis.text=element_blank(),axis.ticks=element_blank())
Run Code Online (Sandbox Code Playgroud)

基本上,我试图通过定义颜色范围来应用我自己的颜色来填充区域.以上不起作用,因为它产生错误:

Error: Continuous value supplied to discrete scale
Run Code Online (Sandbox Code Playgroud)

编辑:但是这有效:

ggplot(data=df, aes(x=long, y=lat, group=group)) + 
  geom_polygon(aes(fill = measure))+    # draw polygons
  coord_equal() +
  geom_path(color="grey", linestyle=2)+
  scale_fill_gradient(low = "#ffffcc", high = "#ff4444", 
                  space = "Lab", na.value = "grey50",
                  guide = "colourbar")+
  labs(title="measure level", x="", y="")+
  theme(axis.text=element_blank(),axis.ticks=element_blank())
Run Code Online (Sandbox Code Playgroud)

EDIT2:measure变量是numeric(),这是我插入度量的方式:

  df$measure <- as.numeric(round(runif(nrow(df), 0, 1), 1))
Run Code Online (Sandbox Code Playgroud)

dput 很大,所以这里是str()

str(df)
'data.frame':   344858 obs. of  8 variables:
$ long   : num  18 18 18 18 18 ...
$ lat    : num  48.7 48.7 48.7 48.7 48.7 ...
$ order  : int  1 2 3 4 5 6 7 8 9 10 ...
$ hole   : logi  FALSE FALSE FALSE FALSE FALSE FALSE ...
$ piece  : Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...
$ group  : Factor w/ 80 levels "0.1","1.1","2.1",..: 1 1 1 1 1 1 1 1 1 1    ...
$ id     : chr  "0" "0" "0" "0" ...
$ measure: num  0.7 0.4 0.8 0.8 0.8 0.2 0.8 0.5 0.2 0 ...
Run Code Online (Sandbox Code Playgroud)

hrb*_*str 6

是的.scale_fill_gradient是连续的.scale_fill_manual是离散的,measure绝对是数字(而不是一个因素)所以你所看到的是完全预期的行为.这是一个帮助解释的玩具示例:

library(rgdal)
library(curl)
library(ggplot2)
library(ggthemes)

# get a simple shapefile

map_url <- "https://andrew.cartodb.com/api/v2/sql?filename=us_states_hexgrid&q=SELECT+*+FROM+andrew.us_states_hexgrid&format=geojson&api_key="

res <- curl_fetch_disk(map_url, "hexes.json")

hex <- readOGR("hexes.json", "OGRGeoJSON")

## OGR data source with driver: GeoJSON 
## Source: "hexes.json", layer: "OGRGeoJSON"
## with 51 features
## It has 6 fields

str(hex@data)

## 'data.frame':    51 obs. of  6 variables:
##  $ cartodb_id: int  1219 1217 1218 220 215 228 232 227 230 229 ...
##  $ created_at: Factor w/ 4 levels "2015-05-13T22:02:22Z",..: 4 2 3 1 1 1 1 1 1 1 ...
##  $ updated_at: Factor w/ 51 levels "2015-05-14T14:17:56Z",..: 20 40 47 12 44 2 3 11 19 25 ...
##  $ label     : Factor w/ 51 levels "A.K.","Ala.",..: 20 40 47 12 44 2 3 11 19 25 ...
##  $ bees      : num  60.5 47.8 33.9 13.9 46.3 48.1 42.9 34.9 44.3 38.7 ...
##  $ iso3166_2 : Factor w/ 51 levels "AK","AL","AR",..: 22 40 47 12 44 2 4 11 19 26 ...
Run Code Online (Sandbox Code Playgroud)

我们将使用,bees因为它与您的相似measure.

# make it so we can use the polygons in ggplot

hex_map <- fortify(hex, region="iso3166_2")

str(hex_map)

## 'data.frame':    357 obs. of  7 variables:
##  $ long : num  -133 -130 -130 -133 -135 ...
##  $ lat  : num  55.3 54.4 52.5 51.6 52.5 ...
##  $ order: int  1 2 3 4 5 6 7 8 9 10 ...
##  $ hole : logi  FALSE FALSE FALSE FALSE FALSE FALSE ...
##  $ piece: Factor w/ 1 level "1": 1 1 1 1 1 1 1 1 1 1 ...
##  $ group: Factor w/ 51 levels "AK.1","AL.1",..: 1 1 1 1 1 1 1 2 2 2 ...
##  $ id   : chr  "AK" "AK" "AK" "AK" ...
Run Code Online (Sandbox Code Playgroud)

默认情况下,bees将被视为连续变量,默认填充色标将反映出:

gg <- ggplot()
gg <- gg + geom_map(data=hex_map, map=hex_map,
                    aes(x=long, y=lat, map_id=id),
                    fill="#ffffff", color="#7f7f7f", size=0.25)
gg <- gg + geom_map(data=hex@data, map=hex_map, aes(map_id=iso3166_2, fill=bees))
gg <- gg + coord_map()
gg <- gg + theme_map()
gg <- gg + theme(legend.position="right")
gg
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

你可以让ggplot使用自动剪切和离散色图与连续色图,其中scale_fill_distiller:

gg <- ggplot()
gg <- gg + geom_map(data=hex_map, map=hex_map,
                    aes(x=long, y=lat, map_id=id),
                    fill="#ffffff", color="#7f7f7f", size=0.25)
gg <- gg + geom_map(data=hex@data, map=hex_map, aes(map_id=iso3166_2, fill=bees))
gg <- gg + scale_fill_distiller()
gg <- gg + coord_map()
gg <- gg + theme_map()
gg <- gg + theme(legend.position="right")
gg
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

您还可以在ggplot操作之外进行手动剪切并将新列传递到scale_fill_manual.

如果您必须使用连续色标,考虑使用viridis 色彩映射:

devtools::install_github("sjmgarnier/viridis")
library(viridis)

gg <- ggplot()
gg <- gg + geom_map(data=hex_map, map=hex_map,
                    aes(x=long, y=lat, map_id=id),
                    fill="#ffffff", color="#7f7f7f", size=0.25)
gg <- gg + geom_map(data=hex@data, map=hex_map, aes(map_id=iso3166_2, fill=bees))
gg <- gg + coord_map()
gg <- gg + scale_fill_viridis()
gg <- gg + theme_map()
gg <- gg + theme(legend.position="right")
gg
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

它通常更精确,对色盲准确可见并且降级到灰度级(并且准确).