gpt4 book ai didi

r - 使用 ggplot 的调色板

转载 作者:行者123 更新时间:2023-12-04 11:54:34 29 4
gpt4 key购买 nike

为了生成可复制的示例,我将不得不提交 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())

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

Error: Continuous value supplied to discrete scale

编辑:但这有效:

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())

EDIT2:measure 变量是 numeric(),下面是我如何插入度量:

  df$measure <- as.numeric(round(runif(nrow(df), 0, 1), 1))

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 ...

最佳答案

是的。 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 ...

我们将使用蜜蜂,因为它类似于您的度量

# 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" ...

默认情况下,bees 将被视为连续变量,默认填充色标将反射(reflect):

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

enter image description here

您可以让 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

enter image description here

您还可以在 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

enter image description here

一般来说,它更准确,色盲可以准确看到并降级为灰度(并且准确)。

关于r - 使用 ggplot 的调色板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31560916/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com