【问题标题】:How to customize the shape of an contour made inside a geom_tile in ggplot2?如何自定义ggplot2中geom_tile内的轮廓形状?
【发布时间】:2021-11-22 08:45:48
【问题描述】:

使用geom_tile 绘制了 X、Y 和 Z 数据集。我想在值小于或等于 2 的图块周围画一条等高线。为此,我使用了 stat_contour 函数,但结果不如预期。我怎样才能得到预期的结果? (最后一张)

library(ggplot2)

X <- 1:3
Y <- seq(0,20,10)

df <- expand.grid(X = X, Y = Y)

df$Z <- c(5,4,9,2.1,1.5,1.2,6,7,1.9)  

ggplot(df, aes(X, Y)) +
  geom_tile(aes(fill = Z)) +
  scale_fill_distiller(palette = "RdYlGn") +
  stat_contour(aes(z = Z),
               breaks = 2,
               color = 1)

我想要类似的东西:

【问题讨论】:

    标签: r ggplot2 contour tile geom-tile


    【解决方案1】:

    您可以尝试使用仅包含 Z

    layer <- df %>% filter(Z <= 2)
    
    ggplot(df, aes(X, Y)) +
      geom_tile(aes(fill = Z)) +
      scale_fill_distiller(palette = "RdYlGn") +
    geom_tile(data=layer, alpha = 0.0, color = "black", size = 1, linejoin = "round") +
      geom_tile(data=layer, alpha = 1, aes(fill = Z)) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-07
      • 2022-01-01
      • 2021-03-17
      • 1970-01-01
      相关资源
      最近更新 更多