r - 在 geom_sf (R ggplot) 的颜色条图例中添加逗号

标签 r ggplot2 sf

我正在使用 ggplot 和 geom_sf 制作 map ,我想为图例中的值添加逗号。我发现编辑颜色条的唯一方法是通过“指南”,但是 + guides(fill = guide_colorbar(labels="comma")) 似乎没有做任何事情(可能是因为“标签”不是“指南”中包含的规范?)如何获得相同的图例,其中包含数字超过 1,000 的逗号?

获取所有数据的代码有点长,所以我希望有人不用 reprex 就知道答案,但如果需要我可以编辑它。谢谢!

landingsmap <- ggplot() + 
  scale_x_continuous(limits=c(-126, -116), expand=c(0,0)) + 
  scale_y_continuous(limits=c(32, 42), expand=c(0,0)) + 
  geom_sf(data=simpleblocks, aes(colour=number_fish, fill=number_fish)) + 
  scale_colour_gradient(low="lightcoral", high="darkred", name="Number of Fish") + 
  scale_fill_gradient(low="lightcoral", high="darkred", name="Number of Fish") +
  geom_sf(data=camap, colour="black") + 
  theme(
    panel.background = element_rect(fill="skyblue4", size=0.5, linetype="solid"),
    legend.position = c(0.78, 0.5)
    ) + 
  NULL
landingsmap 

enter image description here

最佳答案

试试这个。我将使用 sf 包中的示例。

library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3
library(ggplot2)
fname <-  system.file("shape/nc.shp", package = "sf")
nc <-  read_sf(fname)

ggplot(nc)+
  geom_sf(aes(fill = BIR79))+
  scale_fill_continuous(labels=function(x) format(x, big.mark = ",", scientific = FALSE))

reprex package 创建于 2018-09-19 (v0.2.0).

所以,对于你的例子,你会写:

scale_fill_gradient(low="lightcoral", high="darkred", name="Number of Fish", labels=function(x) format(x, big.mark = ",", scientific = FALSE))

关于r - 在 geom_sf (R ggplot) 的颜色条图例中添加逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52413452/

相关文章:

r - 如何在仍然限制 x 轴范围的同时更改 x 轴刻度?

r - 如何加速 `dplyr::mutate()` 中的空间操作?

r - 如何使用 sf 库查找多边形的质心

r - 如何在sf R包中按组测量空间点之间的距离

带置信条的 R survplot 函数无法正常工作

r - 在ggplot中具有不同宽度的堆叠条形图

r - 将 position_dodge 与 geom_pointrange 一起使用

标签旋转以跟随ggplot2中的x轴,极坐标投影?

将线旋转到真北或垂直

r - ggplot 中的空等高线图