gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:05:10 25 4
gpt4 key购买 nike

我正在使用 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))

reprexpackage 创建于 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com