geom_contour 和 geom_label_contour:更改等高线上显示的小数位

问题描述 投票:0回答:1

我正在尝试创建一系列等值线图,但是等值线上的标签精确到小数点后 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)
哪个参数/函数控制轮廓的计算方式以及如何将此值舍入到小数点后两位?

r ggplot2 label contour
1个回答
0
投票

所以解决方案似乎在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")
© www.soinside.com 2019 - 2024. All rights reserved.