[英]Color coded world map with ggplot2 and geom_map
我試圖繪制一張世界地圖,其中每個國家/地區的顏色取決於數據框中變量的值。
> affiliation
# A tibble: 252 × 2
region value
<chr> <dbl>
1 Aruba 0
2 Afghanistan 0
3 Angola 0
4 Anguilla 0
5 Albania 0
6 Finland 1
7 Andorra 0
8 United Arab Emirates 0
9 Argentina 2
10 Armenia 0
# ... with 242 more rows
我的代碼工作正常:
library(ggplot2)
library(ggthemes)
ggplot(affiliation, aes(map_id = region)) +
geom_map(aes(fill = value), map = worldMap, color='grey', size=0.3) +
expand_limits(x = worldMap$long, y = worldMap$lat) +
theme_few()+
theme(legend.position = "bottom",
axis.ticks = element_blank(),
axis.title = element_blank(),
axis.text = element_blank()) +
scale_fill_gradient(low="white", high="darkblue", name="Number of Affiliates") +
guides(fill = guide_colorbar(barwidth = 10, barheight = .5))
我的問題是,大多數值是0、1或2,而20-30中的值很少,但是1個離群的國家/地區中的值為330。這使色標無用,因為該國家/地區顯示為飽和色,而所有國家/地區值較低的國家顯示為白色。
我嘗試將geom_map(aes(fill = value),…
替換為geom_map(aes(fill = log(value)),…
但是在這種情況下,圖例中的比例顯示為0到5+(而不是0到330)。
為了更好地控制許多值較低的國家/地區的色彩飽和度,以便使其在世界地圖上更清晰可見,我該怎么做? 謝謝
為什么不嘗試調用scale_fill_gradientn
而不是scale_fill_gradient
,而是根據需要停止設置顏色。 例如:
scale_fill_gradientn(colours = c('#ff0000', '#cccccc', '#0000ff'), values = c(0, 30, 331))
ggplot2中的大多數連續scale
函數都接受一個trans
參數,該參數指定要用於該美學的變換。 這將自動處理轉換和標記。
在你的榜樣,加入trans = "log1p"
(不只是"log"
,因為你的數據有幾個0
值)的參數scale_gradient_n
可能會有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.