在 ggplot 中绘制等高线 - geom_raster、geom_contour、geom_point

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

我有一个想要绘制的插值数据集。当我将其绘制为 geom_point 时,我发现使用 geom_raster 或 geom_contour 时缺少的许多功能。我猜它已经平滑了?有没有办法可以在不丢失 geom_point 中拾取的更细粒度轮廓的情况下绘制此图?第二个数字看起来很糟糕!有什么办法可以平滑这个吗?

ggplot() + geom_raster(data = df, 
                             aes(x=X, y = Y, fill=Z)) + scale_y_reverse() +
scale_fill_distiller(palette = "RdYlBu") +
theme_bw() + theme(panel.grid = element_blank()))


 ggplot(data=df, aes(x = X, y = Y)) +
  geom_point(aes(colour = Z)) + scale_y_reverse() +
scale_colour_gradientn(colours = rev(ODV_colours)) +
theme_bw() + theme(panel.grid = element_blank()))
r ggplot2 contour contourf geom-raster
1个回答
1
投票

您似乎有一个低分辨率栅格,并且您想要进行插值以使其不那么块状。在这种情况下,您可以使用

interp
进行二维插值:

library(interp)
library(ggplot2)

ODV_colours <- c('#fca878', '#d42b28', '#fbbf00', '#2cab1a', 
                 '#19b3e6', '#6f44fc', '#c767fa')


interp(x = df$X, y = df$Y, z = df$Z, nx = 1000, ny = 1000, linear = FALSE) |> 
  interp2xyz() |>
  as.data.frame() |>
  ggplot(aes(x, y, fill = z)) +
  geom_raster() +
  scale_y_reverse() +
  scale_fill_gradientn(colours = rev(ODV_colours), na.value = NA) +
  theme_bw(base_size = 16) + 
  theme(panel.grid = element_blank())

© www.soinside.com 2019 - 2024. All rights reserved.