为了生成可复制的示例,我将不得不提交shapefile数据等,这对您来说很麻烦(下载数据等),所以这里只是提供最后一部分,而不是{{1} }
以下是示例代码:
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
EDIT2: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())
变量是数字(),这是我插入度量的方式:
measure
df$measure <- as.numeric(round(runif(nrow(df), 0, 1), 1))
很大,所以这里是str()
dput
答案 0 :(得分: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 ...
我们将使用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" ...
默认情况下,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
你可以让ggplot使用自动剪切&amp;离散色图与带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
您还可以在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
一般来说,它更准确,对于色盲和准确可见。将(并准确地)降级为灰度级。
答案 1 :(得分:1)
使用scale_manual
,您可以创建自己的离散量表&#34; (?scale_fill_manual
)。因此,错误&#34;错误:连续值[即&#34;衡量&#34;]提供给离散比例[scale_fill_manual
]&#34;。
您需要一个连续的比例并尝试scale_fill_gradient
。精细。但是,使用scale_fill_gradientn
更容易实现所需的调色板,这会在n种颜色之间创建一个&#34;平滑颜色渐变&#34;。
一个更简单的例子:
# some data
df <- data.frame(x = 1:11, y = 1)
# an analogue to your failed attempt
ggplot(data = df, aes(x = x, y = y, fill = x)) +
geom_point(pch = 21, size = 20) +
scale_fill_manual(values = myPal)
# Error: Continuous value supplied to discrete scale
# using the continuous scale_fill_gradientn instead, with the desired color vector and space
ggplot(data = df, aes(x = x, y = y, fill = x)) +
geom_point(pch = 21, size = 20) +
scale_fill_gradientn(colours = c("darkgreen", "yellow", "red"), space = "rgb")