【发布时间】:2021-04-14 00:33:50
【问题描述】:
我在向 tm_map 添加固定中断时遇到了麻烦。我在不同的数据集上尝试了与该论坛(Customize how R tmap legend values are printed)上的另一个主题相同的代码,但原理是相同的。我使用了论坛中的示例,因为在 Map1 行的中断部分中添加所有值也不起作用。但是,这两种情况都给了我错误:
Error in .subset2(x, i, exact = exact) : no such index at level 1
谁能解释一下这个错误的确切含义以及如何克服它?
代码
mybreaks1 = c(0, 5, 15, 25, 35, 70)
mybreaks2 = c()
for (i in 1:length(mybreaks1)-1){
print(i)
if (i == 1){
mybreaks2[i] <- paste0(mybreaks1[i], " - ", mybreaks1[i+1])
}
if (i >1){
mybreaks2[i] <- paste0(prettyNum(mybreaks1[i]+1, big.mark = " "), " - ", prettyNum(mybreaks1[i+1], big.mark = " "))
}
}
Map1 = tm_shape(Week1_PlasticMap) + tm_fill(col = "total.per.segment" , breaks = mybreaks1, labels = mybreaks2, style = "fixed", convert2density = TRUE, area = Week1_PlasticMap$area, title = "Plastics (m^-2)", interactive = TRUE) + tm_borders(col = "darkslategrey") + tm_layout(main.title = "Week 1", legend.outside = TRUE, attr.outside= TRUE, attr.outside.position=c("right", "bottom"), legend.width = 0.2, legend.height = 0.3)```
The dataframe Week1_Plastic can be downloaded via the link below, because the dimensions are to large to upload in this question.
https://www.transfernow.net/7il18A012021
【问题讨论】: