- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在尝试制作一张 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()
(我知道您指定人口类别是有原因的。我只是更改了它们,以便我可以更轻松地查看我所做的是否有效。)
该代码应返回以下图:
然后您可以通过添加 + 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 对象将产生:
关于r - 在 R ggplot : manually assigning colors and values polygons should take 中制作特定图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42165578/
我在 visual studio 中使用它,但编译器声称 h1 不能嵌套在 legend 元素中,但浏览器无论如何都可以呈现它,所以我很困惑这是有效的还是不是? Caption
Matplotlib 中的图例是帮助观察者理解图像数据的重要工具。 图例通常包含在图像中,用于解释不同的颜色、形状、标签和其他元素. 1. 主要参数 当不设置图例的参数时,默认的图例是这样的.
mongoDB分页的两种方法 mongoDB的分页查询是通过limit(),skip(),sort()这三个函数组合进行分页查询的 下面这个是我的测试数据 第一种方法 查询第一页的数据:db.te
我的 grafana 仪表板图例中有这些名称: stats.gauges.all.cloudwatch.communicationapi.dynamodb.ca_communicationapi_re
鉴于以下情况: import pandas as pd import matplotlib.pyplot as plt d=pd.DataFrame({'category':['a','a','a',
我收到此错误,请问出了什么问题? plt.legend(handles=[d1, d2]) File "/usr/lib/python3/dist-packages/matplotlib/pypl
这个问题已经有答案了: python matplotlib: label in histogram (1 个回答) 已关闭 5 年前。 我无法向使用 matplotlib.pyplot 创建的直方图添
我想更改我的图例,我必须删除分页并将所有标题放在一两行中。我想更改图标。 现在是这个样子 我想要这样的东西 我输入了 scrollArrows: 'none' 但它不起作用。这是我在 Controll
我正在创建一个文件来读取一定数量的 .wav 文件,每个文件对应一个音符。我正在对它们中的每一个执行 FFT,并将它们全部绘制在同一个图形上。但是,我在正确打印图例时遇到了问题,它将我想要使用的名称分
我正在 ggplot 中处理多个 sf 几何图形,并希望以点、线和正方形(对于多边形)的形式显示图例。然而,geom_sf图例结合了我的几何特征(即结合线和点)如下所示: library(ggplot
我想要 geom_text()标签根据变量采用字体系列。 根据 ggplot2 docs 上的示例(向下滚动到底部),我已经这样做了(与 ggplot 文档示例中的相同): library(ggplo
我正在绘制一个 geom_tile 图,其中每个点都有一个特定的 p-value。 图的绘制方式很好,p-value 限制在图例上。 我想添加到这个图例的是具有这个 p-value 的数据点的数量。
我有以下 data.frame : my.df = data.frame(mean = c(0.045729661,0.030416531,0.043202944,0.025600973,0.0405
我正在绘图中打印 Case-Shiller 索引: structure(list(Date = structure(c(10957, 11048, 11139, 11231, 11323, 1141
我想绘制一个包含 4 个不同值( 1 )的栅格,并带有描述类别的分类文本图例,例如 2但有彩盒: 我试过使用图例,例如: legend( 1,-20,legend = c("land","ocean/
我在 R 中用 ggplot2 制作了这个图 由以下代码绘制: ggplot(mtcars) + geom_smooth(fill='grey', alpha=0.3, span=0.1, aes
我有来自两个不同数据框的数据。我正在尝试为每个数据框创建图例。我知道我可以组合数据框并进行操作,但是由于我的数据源,从两个不同的数据框进行绘图最有意义。 请在下面找到简化的示例。我已经接近了,但传说中
我将 highchart 的图形保存为 svg。 var chart = $('#graphicName').highcharts(); EXPORT_WIDTH = 1000; render_wid
我想将自定义符号设置为图例项。目前我有矩形。我需要带有勾号的矩形(如彩色复选框)。根据文档,我可以自定义元素,例如符号附近的文本。 但如果我将绘图上的所有元素都设置为自定义图像 ( Example )
我有下面的代码,可以生成饼图和图例。有没有办法让表情符号和饼图具有相同的对应颜色,因为目前表情符号是黑色的。另外,我希望删除图例中的方形色 block ,并且术语“男性”和“女性”出现在正确的表情符号
我是一名优秀的程序员,十分优秀!