我正在尝试创建一系列等值线图,但是等值线上的标签精确到小数点后 10 位。我希望标签只有 2 位小数。以下是原始代码和结果图:
plot <-
ggplot(data, aes(x = X, y = Y, z = pred, fill = pred)) +
geom_tile() +
geom_contour(colour = "gray",
size = 0.15) +
geom_label_contour(aes(z = pred), label.placer = label_placer_flattest()) +
scale_fill_gradientn(colours = rev(brewer.pal(10, "RdYlBu")),
limits= c(0, 10)) +
xlab("Easting") +
ylab("Northing") +
coord_cartesian(expand = FALSE) +
theme(legend.position = "none")
geom_label_contour(breaks = pretty(round(data, digits = 2)
中的分隔符进行四舍五入,但绘图在标签中仍显示为小数点后 10 位。我尝试过类似地舍入数据 geom_contour(breaks = round(pretty(x = data,n = 10), digits = 2)
哪个参数/函数控制轮廓的计算方式以及如何将此值舍入到小数点后两位?
所以解决方案似乎在breaks参数之内。下面是有效的代码。
breaks <- seq(min(data$pred), max(data$pred), length.out = 10) %>% round(digits = 2)
plot <-
ggplot(data, aes(x = X, y = Y, z = pred, fill = pred)) +
geom_tile() +
geom_contour(colour = "gray",
size = 0.15) +
geom_label_contour(aes(z = pred), label.placer = label_placer_flattest()) +
scale_fill_gradientn(colours = rev(brewer.pal(10, "RdYlBu")),
**breaks = breaks,**
limits= c(0, 10)) +
xlab("Easting") +
ylab("Northing") +
coord_cartesian(expand = FALSE) +
theme(legend.position = "none")