【问题标题】:ggplot2: "Error: Must request at least one colour from a hue palette." for plotting spatial problem solutionggplot2:“错误:必须从色调调色板中请求至少一种颜色。”用于绘制空间问题解决方案
【发布时间】:2021-07-02 15:42:58
【问题描述】:

我有一个空间问题列表,我可以使用 Prioritizr 解决,但我无法绘制解决方案。

这是问题列表:

p <- readRDS("Input/Problems/01b_ProblemsIUCNLong.rds")

这是我用来解决它的代码:

s2 <- lapply(readRDS("Input/Problems/01b_ProblemsIUCNLong.rds"), function(x) {
    f <- solve(x, force= TRUE) %>% 
      st_as_sf(sf_column_name = "geometry")
    
    ff <- f %>% mutate(Selected_Area = round(nrow(f[f$solution_1 == 1,])/nrow(f), 1)*100)
  } )

但是,我似乎无法绘制它并继续收到相同的错误:

IUCNonlyTarget10_withLP_plot <- ggplot() + 
+     geom_sf(data = s2[[1]], aes(fill = as.factor(solution_1), color = NA, size = 0.02, show.legend = FALSE)) +
+     scale_fill_manual(name = "Solution",
+                       values = pal_rich,
+                       labels = solution) +
+     geom_sf(data = world_sf, size = 0.05, fill = "grey20") +
+     coord_sf(xlim = c(st_bbox(Bndry)$xmin, st_bbox(Bndry)$xmax), 
+              ylim = c(st_bbox(Bndry)$ymin, st_bbox(Bndry)$ymax),
+              expand = TRUE) +
+     ggtitle("Target 10%") +
+     theme_bw() +
+     ggsave("Output/Figs/IUCNonlyTarget10_withLP_06.pdf", width = 20, height = 10, dpi = 300) +
+     ggsave("Output/Figs/IUCNonlyTarget10_withLP_06.png", width = 20, height = 10, dpi = 300)

Error: Must request at least one colour from a hue palette.
In addition: Warning message:
Ignoring unknown aesthetics: show.legend

知道我可以解决这个问题吗?

我尝试删除 color = NA 并尝试将其放在 aes() 之外,但我的绘图中出现了一个空层...即使我检查它不是一个空层...

这就是我得到的

虽然它应该是这样的

有什么建议吗?

【问题讨论】:

  • 尝试从 aes() 中删除 color = NA 或将其移出 aes()
  • 我试过了,我没有错误了,但我得到了一个空的情节......虽然我检查了我的图层不是空的......

标签: r ggplot2


【解决方案1】:

是括号里的错位

IUCNonlyTarget10_withLP_plot <- ggplot() + 
+     geom_sf(data = s2[[1]], aes(fill = as.factor(solution_1)), color = NA, size = 0.02, show.legend = FALSE) +
+     scale_fill_manual(name = "Solution",
+                       values = pal_rich,
+                       labels = solution) +
+     geom_sf(data = world_sf, size = 0.05, fill = "grey20") +
+     coord_sf(xlim = c(st_bbox(Bndry)$xmin, st_bbox(Bndry)$xmax), 
+              ylim = c(st_bbox(Bndry)$ymin, st_bbox(Bndry)$ymax),
+              expand = TRUE) +
+     ggtitle("Target 10%") +
+     theme_bw() +
+     ggsave("Output/Figs/IUCNonlyTarget10_withLP_06.pdf", width = 20, height = 10, dpi = 300) +
+     ggsave("Output/Figs/IUCNonlyTarget10_withLP_06.png", width = 20, height = 10, dpi = 300)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 2019-03-10
    • 2020-07-07
    相关资源
    最近更新 更多