gpt4 book ai didi

r - 在 R ggplot : manually assigning colors and values polygons should take 中制作特定图例

转载 作者:行者123 更新时间:2023-12-04 03:14:38 26 4
gpt4 key购买 nike

我目前正在尝试制作一张 map ,该 map 1) 用各自的名称标记每个多边形,以及 2) 根据每个多边形的计数为每个多边形赋予特定的颜色。

阅读了几篇 Stack overflow 帖子,我发现其中一篇对我帮助很大(Labeling center of map polygons in R ggplot),但我遇到了两个主要问题:1) 我似乎无法指定我希望 map 采用的具体颜色, 和 2) 我似乎无法按照我想要的方式获得图例。

我将使用之前提到的帖子 ( Labeling center of map polygons in R ggplot) 中用户 Silverfish 提供的代码:

library(rgdal) # used to read world map data
library(rgeos) # to fortify without needing gpclib
library(maptools)
library(ggplot2)
library(scales) # for formatting ggplot scales with commas

#Data from http://thematicmapping.org/downloads/world_borders.php.
#Direct link: http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip
#Unpack and put the files in a dir 'data'

worldMap <- readOGR(dsn="data", layer="TM_WORLD_BORDERS_SIMPL-0.3")
# Change "data" to your path in the above!
worldMap.fort <- fortify(worldMap, region = "ISO3")
# Fortifying a map makes the data frame ggplot uses to draw the map outlines.
# "region" or "id" identifies those polygons, and links them to your data.
# Look at head(worldMap@data) to see other choices for id.
# Your data frame needs a column with matching ids to set as the map_id aesthetic in ggplot.
idList <- worldMap@data$ISO3
# "coordinates" extracts centroids of the polygons, in the order listed at worldMap@data
centroids.df <- as.data.frame(coordinates(worldMap))
names(centroids.df) <- c("Longitude", "Latitude") #more sensible column names
# This shapefile contained population data, let's plot it.
popList <- worldMap@data$POP2005

pop.df <- data.frame(id = idList, population = popList, centroids.df)

ggplot(pop.df, aes(map_id = id)) + #"id" is col in your df, not in the map object
geom_map(aes(fill = population), colour= "grey", map = worldMap.fort) +
expand_limits(x = worldMap.fort$long, y = worldMap.fort$lat) +
scale_fill_gradient(high = "red", low = "white", guide = "colorbar", labels = comma) +
geom_text(aes(label = id, x = Longitude, y = Latitude)) + #add labels at centroids
coord_equal(xlim = c(-90,-30), ylim = c(-60, 20)) + #let's view South America
labs(x = "Longitude", y = "Latitude", title = "World Population") +
theme_bw()

This is what the code currently outputs

这个例子让我学会了如何在 map 的多边形上放置标签,但我需要做几处更改才能到达我需要的位置:1)我需要比例不是渐变,而是以图例的形式为特定范围的值提供颜色。我想要的颜色和范围如下(颜色以十六进制形式指定):- 范围:0 颜色:“白色”- 范围:1-99 颜色:“#d3c874”- 范围:100-249 颜色:“#d69b26”- 范围:250-499 颜色:“#89280d”- 范围:500+ 颜色:“#411614”

2)我需要图例出现在底部并且从左到右

3) 我需要多边形根据它们的数量选择正确的颜色

在“pop.df”数据框中,我创建了另一列 (Categ),根据我之前提到的范围分配值“1”、“2”、“3”、“4”或“5” :- 范围:0 值:“1”- 范围:1-99 值:“2”- 范围:100-249 值:“3”- 范围:250-499 值:“4”- 范围:500+ 值:“5”(注意:看到此示例中使用的计数非常大,我知道每个多边形很可能会超过 500+,这些只是我出于自己的目的使用的范围)。

我这样做是为了在绘制 map 时更容易为多边形指定颜色。基本上,我认为这种方式比创建首先检查计数、将其与范围进行比较、然后给出颜色的代码更容易。我会澄清它不需要以这种方式完成,我只是说我已经接近它的方式。

我使用这段代码实现了这一点:

Pop.df$Categ <- ifelse(Pop.df$population < 1, "1",
ifelse(Pop.df$population >= 1 & Pop.df$population < 100, "2",
ifelse(Pop.df$population >= 100 & Pop.df$population < 250, "3",
ifelse(Pop.df$population >= 250 & Pop.df$population < 500, "4", "5"))))

看到我需要的颜色不是特定调色板的一部分,并且我需要 map 中的那些特定颜色,我开始使用“scale_colour_manual”而不是“scale_fill_gradient”方法根据需要调整图例,但它似乎没有用。我面临的具体障碍是:a) 我似乎无法让图例从梯度到离散值b) map 上的某些颜色与我需要的颜色完全不同。我不知道是因为十六进制代码还是其他原因。

我已经通过多种方式解决了这个问题。我最近使用的代码是这个:

ggplot(pop.df, aes(map_id = id)) +
geom_map(aes(fill = population), colour= "black", map
= worldMap.fort) +
expand_limits(x = worldMap.fort$long, y = worldMap.fort$lat) +
scale_colour_manual(values = c("white", "#d3c874", "#d69b26", "#89280d", "#411614"),
limits = c("1", "2", "3", "4", "5"), breaks = c("1", "2", "3", "4", "5")) + #This is my attempts to getting the legend to work
geom_text(aes(label = id, x = Longitude, y = Latitude)) + #add labels at centroids
coord_equal(xlim = c(-90,-30), ylim = c(-60, 20)) + #let's view South America
labs(x = "Longitude", y = "Latitude", title = "World Population") +
theme_bw()

关于图例的位置,我什至还没有开始解决这个问题,因为我首先试图让它正确显示。

任何见解都会很棒!谢谢

最佳答案

最终,您需要为 fill 美学提供离散的 Categ 变量,而不是连续的人口变量。然后使用 scale_fill_manual(),因为您想要用一种颜色填充国家(而不是为其边界着色),并且您已经在 geom_map() 中将边界着色为黑色。请参阅下面带有注释的代码。

# same code as in your question, 
# but (1) changed population categories (added four zeroes) so that
# more countries fall in different bins,
# (just so I could see if my subsequent solutions worked)
# and (2) used the hex colors you supplied as category values, rather than 1-5
pop.df$Categ <- ifelse(pop.df$population < 1, "#ffffff",
ifelse(pop.df$population >= 1 & pop.df$population < 1000000, "#d3c874",
ifelse(pop.df$population >= 1000000 & pop.df$population < 2500000, "#d69b26",
ifelse(pop.df$population >= 2500000 & pop.df$population < 5000000, "#89280d", "#411614"))))
# convert Categ to factor
pop.df$Categ <- factor(pop.df$Categ, levels = c("#ffffff", "#d3c874", "#d69b26", "#89280d", "#411614"))
# plot
ggplot(pop.df, aes(map_id = id)) +
geom_map(aes(fill=Categ), colour= "black", map = worldMap.fort) +
expand_limits(x = worldMap.fort$long, y = worldMap.fort$lat) +
scale_fill_manual(values = c("#ffffff", "#d3c874", "#d69b26", "#89280d", "#411614"),
breaks = c("#ffffff", "#d3c874", "#d69b26", "#89280d", "#411614"),
labels = c("zero", "< 1 million", "1 to 2.5 million", "2.5 to 5 million", "> 5 million"))
geom_text(aes(label = id, x = Longitude, y = Latitude)) + #add labels at centroids
coord_equal(xlim = c(-90,-30), ylim = c(-60, 20)) + #let's view South America
labs(x = "Longitude", y = "Latitude", title = "World Population") +
theme_bw()

(我知道您指定人口类别是有原因的。我只是更改了它们,以便我可以更轻松地查看我所做的是否有效。)

该代码应返回以下图:

enter image description here

然后您可以通过添加 + theme(legend.position = ...) 来更改图例的位置,其中可接受的值为“top”、“bottom”、“left”、 “右”、“无”或零到一的坐标系(例如,c(0.5, 0.5) 会将您的图例放在绘图的正中间)。

因此,将 + theme(legend.position = c(0.83, 0.857), legend.background = element_rect(fill="transparent",colour=NA)) 添加到您的 ggplot 对象将产生:

enter image description here

关于r - 在 R ggplot : manually assigning colors and values polygons should take 中制作特定图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42165578/

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