我有一个 SpatialPolygonsDataFrame,其中包含包含十六进制颜色值的列。我想用 tmap 包来绘制这样的地图:
tm_shape(full.shp) + tm_fill(col="konf1900")
Run Code Online (Sandbox Code Playgroud)
但随后它被视为类别变量,结果是:

我不知道如何告诉 tmap 它应该直接在地图上绘制颜色值......
有人可以帮忙吗?
编辑:
请参阅下面的答案 - 问题是数据帧列未编码as.character。我想这有时可能会对某人有所帮助......
我正在尝试开发一个简单的地图,其中气泡代表大小。
library(tmap)
data(World, metro)
example.map <- tm_shape(World)+
tm_borders()+
tm_shape(metro)+
tm_bubbles(size = "pop2010",
col = "pop2010",
palette = "-RdYlBu")
example.map
Run Code Online (Sandbox Code Playgroud)
实际的地图很好,是我想要的。我的问题是传说。上面的代码中有两个,我可以使用以下方法将其设为一个:
legend.col.show = FALSE
Run Code Online (Sandbox Code Playgroud)
或者
legend.size.show = FALSE
Run Code Online (Sandbox Code Playgroud)
但后来我有一个显示尺寸或颜色的图例。是否可以使单个图例同时显示大小和颜色?
我正在使用tmap软件包在R中绘制一些空间数据。我定义中断并在tm_dots函数中绘制颜色。我希望能够定义类别的打印顺序,以便按类别定义(最高的类别位于顶部,第二高的类别位于其下,等等)。我需要能够清楚地看到最高类别点在哪里。我知道这可以通过多个空间点数据帧来实现,但是还有另一种不太笨拙的方法吗?以下是使用缪斯数据的示例。我提出的要点很重要,所以它们可以重叠。因此,理想情况下,绘图中的绘图顺序为:蓝色,绿色,橙色,红色。
libary(tmap)
library(sp)
data("meuse")
coordinates(meuse) <- c("x","y")
tm_layout() +
tm_shape(meuse) + tm_dots("cadmium", breaks = c(1,2,3,4,Inf), palette = "-Spectral", auto.palette.mapping = FALSE,
size = 1) +
tm_legend(legend.outside = TRUE)
Run Code Online (Sandbox Code Playgroud)
tmap从一个玩具示例开始,我可以使用以下代码快速获取交互式地图:
library(tmap)
tmap_mode("view")
data("World", "metro")
tm_shape(World) +
tm_polygons() +
tm_shape(metro) +
tm_dots("pop2010",
col = "red") +
tm_format("World")
Run Code Online (Sandbox Code Playgroud)
我希望地图最初仅显示图层World并隐藏地铁图层。仅当用户勾选图层选择中的框时它才会出现。
我浏览了tm_shape文档tm_dots,但没有发现任何似乎可以控制这种行为的东西。那可能吗?
我正在尝试创建一个 tmap 气泡图,其中大小和颜色美观具有相同的固定中断。设置style = "fixed"和breaks = c(-Inf, seq(-10, 10, by = 2.5), Inf)将为颜色变量生成所需的结果,但大小变量保持不变。我正在处理具有正值和负值的数据,因此我使用具有色彩美感的发散调色板。不过,在绘制其大小时,我使用了该变量的绝对值。我想创建 0 到 10 之间的固定大小分隔符,以匹配我的调色板的绝对值范围。
这是我当前地图脚本的简单版本
# dput(head(df))
points_layer <- structure(list(date = structure(c(17449, 17449, 17449, 17449,
17449, 17449), class = "Date"), node = c("Irvington", "Marion Court Apartments",
"Portsmouth Portland", "PSU STAR Lab Roof North", "PSU STAR Lab Roof South",
"PSU STAR Lab Rose City Park"), pm2_5_cf_x = c(9.25546767003776,
9.45130634372901, 12.6012155894551, 8.74508087500526, 8.42118421108649,
9.22030608712564), pm2.5.predicted = c(9.20049509485525, 8.7580813489583,
11.0618606546844, 7.93370690912048, 7.91343513801979, 9.40375047003665
), geometry = …Run Code Online (Sandbox Code Playgroud) 我是编程新手,目前正在参加使用 R 的介绍性空间分析课程。以下代码生成下面包含的 tmap。如何将每个 tmap 的标题居中,并将图例放置在右上角而不将地图本身放在上面?
非常感谢你的协助。
ga1 = tm_shape(a2georgia) +
tm_polygons('PctBlack', style='quantile', breaks=c(4.98,11.75, 22.35,27.64, 32.55, 40.06, 48.18, 79.64),
n=8, palette=c('lightblue','khaki1', 'red3'), title='Quantiles(8)',
border.col='grey27', alpha=.9) +
tm_layout(legend.position = c("right", "top"), title= '% of Population of Black Race', title.position = c('right', 'top'))
ga_cartogram <- cartogram_cont(a2georgia, "PctBlack", itermax=5)
ga2 = tm_shape(ga_cartogram) +
tm_polygons("PctBlack", style='quantile', breaks=c(4.98,11.75, 22.35,27.64, 32.55, 40.06, 48.18, 79.64),
n=8, palette=c('lightblue','khaki1', 'red3'), title='Quantiles(8)',
border.col='grey27', alpha=.9) +
tm_layout(legend.position = c("right", "top"), title= '% of Population of Black Race', title.position = c('right', 'top')) …Run Code Online (Sandbox Code Playgroud) 我想用 tmap 包在密度图中绘制 1950 年的世界人口。
我手动将人口数据分成 22 个类别,并为每个类别填充不同的颜色。
我的代码是:
library(tmap)
data(World)
map <- tm_shape(World)+
tm_fill("1950",
title = "Population class",
breaks = c(0, 100 ,200, 300, 400, 500, 600, 700, 800, 900,
1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800,
1900, 2000, 3000, 4000),
textNA = "No data",
colorNA = "white",
palette = "topo")+
tm_borders()+
tm_layout("World Density Population Map")
tm_style_classic()
current.mode <- tmap_mode("plot")
map
Run Code Online (Sandbox Code Playgroud)
最后一步“地图”发生错误:
错误:调色板无效
我有一个来自 wpp2015 的公共数据集,它与我的数据集不完全相同,但会提供我的数据框结构的直觉。
dput(df) <- structure(list(name = structure(c(1L, 3L, 4L, 5L, 6L, 14L, 7L, …Run Code Online (Sandbox Code Playgroud) 这是一个可重现的示例
library(tmap)
data("World")
tm_shape(World) +
tm_polygons("pop_est", style = "cont", legend.is.portrait=FALSE) +
tm_facets("continent") +
tm_layout(legend.position = c("left", "bottom"),
main.title = "Population",
main.title.position = "centre")
Run Code Online (Sandbox Code Playgroud)
我希望将图例放置在面下方,以更好地利用空间并使所有内容更加居中。我有什么想法可以做到这一点吗?
我正在尝试将test1(用 tmap 制作的地图)与test2(用 ggplot2 制作的图)结合起来,使用cowplot::plot_grid。
library(ggplot2)
library(tmap)
library(cowplot)
library(ggplotify)
test2 <-ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + geom_point()
data("World")
test1 <- tm_shape(World) + tm_polygons("HPI")
plot_grid(test1,test2)
Run Code Online (Sandbox Code Playgroud)
tmap 似乎与cowplot 不兼容:
“在 as_grob.default(plot) 中:无法将 tmap 类的对象转换为 grob”
使用时同样的问题
tmap::tmap_arrange(test1,test2)
Run Code Online (Sandbox Code Playgroud)
“tmap_arrange(test1, test2) 中的错误:并非所有参数都是 tmap 对象”
我还尝试使用 ggplotify 中的“as.grob”等函数来转换 tmap,
test3 <- as.grob(test1)
Run Code Online (Sandbox Code Playgroud)
但运气不佳“UseMethod(“as.grob”) 中的错误:没有适用于“as.grob”的方法应用于类“tmap”的对象”
有什么建议么?
谢谢!
我正在使用该tmap包在地图上绘制一些数据。由于数据集的大小,您可以在此处下载所需的数据集(它们是公共数据集)。此代码使用驱动器中的.csv和.shp文件。
我需要将指南针和比例尺从图像上移到图例下方。我怎样才能做到这一点?我还没有看到该tm_compass命令有一个选项可以将其移到图像之外,那么还有其他方法可以做到这一点吗?
代码:
#Load packages
library(pacman)
p_load(sf, tidyverse, ggspatial, tmap, ggplot2)
#Read data
boundary <- st_read('US_tract_2010.shp')
food <- read.csv('FoodAtlas.csv')
#Join data together
boundary$GEOID10 <- as.numeric(boundary$GEOID10)
foodleft <- left_join(boundary, food, by = c('GEOID10' = 'CensusTract'))
#Construct the graph
ks <- foodleft %>%
select(State, County, TractLOWI, LAhalfand10) %>%
filter(State %in% c('Kansas'))
ks$LAhalfand10 <- as.factor(ks$LAhalfand10)
tmap_mode("plot")
qtm(ks,
fill = "LAhalfand10",
fill.title = "LAhalfand10") +
tm_bubbles("TractLOWI", col = "steelblue", title.size = "TractLOWI (Pop)") +
tm_layout(legend.outside …Run Code Online (Sandbox Code Playgroud)