【问题标题】:R plot filled.contour() output in ggpplot2ggplot2中的R绘图填充.contour()输出
【发布时间】:2015-04-04 12:46:35
【问题描述】:

我想绘制这个用 fill.contour() 创建的图形,但是在 ggplot2 中,我该怎么做呢?

我想使用 ggplot2,因为图形约定更容易。我想使用 fill.contour() 的原因是因为我尝试了 geom_tile() 和 image.plot(),它们都创建了非常像平铺的输出,我需要一个类似于 fill.contour() 的输出。

这是我的图:

代码:

library(akima)

df <-read.table("Petra_phytoplankton+POM_xydata_minusNAs_noduplicates.txt",header=T)
attach(df)
names(df)
fld <- with(df, interp(x = longitude, y = latitude, z = d13C))

filled.contour.ungeoreferenced <- 
  (filled.contour(x = fld$x,
                  y = fld$y,
                  z = fld$z,
                  color.palette =
                    colorRampPalette(c("blue", "green", "yellow",
                                       "orange", "red")),
                  xlab = "Longitude",
                  ylab = "Latitude",
                  key.title = title(main = "d13C", 
                                    cex.main = 1)))

数据片段:

latitude    longitude   d13C
-65 -70 -27.7
-61 150 -32.2
-61 150 -28.3
-60 116 -26.8
-60 116 -24.7
-47 38  -24.8
-38 150 -20.5
19  -65.7   -19.9
19  -65.5   -18.5
18  -60.7   -20
18  -58.5   -18.2
18  -57.8   -19
17  -55.4   -18.6
17  -50.8   -18
17  -47.1   -18.3
17  -45.5   -19.4
16  -43.3   -17.9
15  -40.7   -18.5
14  -39.3   -19.9
12  -36.7   -19.9
12  -36.2   -19.9
11  -34.4   -19.2
10  -32 -18.5
9   -30.3   -19.3
8   -29.2   -19.4
7   -26.6   -18.2
7   -25.5   -19.3
6   23.9    -20
3   -21.3   -20.4

【问题讨论】:

  • 您的数据 sn-p 包含重复项并导致 interp 头痛。另外,为什么要附上df
  • 我可能会以某种方式摆脱重复项,但至于附加 - 只是因为有人告诉我这样做。所以我不应该?
  • 那么你有没有找到一种方法来用 ggplot 模拟 fill.contour?

标签: r ggplot2 contour graphing


【解决方案1】:

您可以根据需要调整颜色:

gdat <- interp2xyz(fld, data.frame=TRUE)

ggplot(gdat) + 
  aes(x = x, y = y, z = z, fill = z) + 
  geom_tile() + 
  coord_equal() +
  geom_contour(color = "white", alpha = 0.5) + 
  scale_fill_distiller(palette="Spectral", na.value="white") + 
  theme_bw()

您可以通过增加插值的密度来以一些处理时间为代价来减少像素化:

fld <- with(df, interp(x = longitude, 
                       y = latitude, 
                       z = d13C,
                       xo = seq(min(longitude), max(longitude), length=400),
                       duplicate="mean"))

并且还减少了 bin 宽度:

ggplot(gdat) + 
  aes(x = x, y = y, z = z) + 
  geom_tile(aes(fill=z)) + 
  coord_equal() +
  stat_contour(aes(fill=..level..), geom="polygon", binwidth=0.005) + 
  geom_contour(color="white", alpha=0.5) +
  scale_fill_distiller(palette="Spectral", na.value="white") + 
  theme_bw()

注意:在一个不错的桌面系统上,这将需要几秒钟的时间。在我相当强大的 MacBook Pro 上是:

   user  system elapsed 
  6.931   0.655   8.153 

【讨论】:

  • 谢谢,但正如我在问题中所说,我之前尝试过 geom_tile,并再次使用您的代码,但它太像素化了。有什么想法可以减少像素化吗?干杯
  • 嗨@hbrmstr 非常好的答案,但我还有另一个问题。是否可以在插值字段上添加 geom_path?我不能成功。
【解决方案2】:

要跟进@hrbrmstr 的最小示例,您还可以让 ggplot2 为您计算“z”:

library(ggplot2)
ggplot(data = faithful, aes(x = eruptions, y = waiting)) +
  stat_density2d(aes(colour = ..level.., fill = ..level..), geom = "polygon")

【讨论】:

    【解决方案3】:

    我从ggplot2 website 中拿了这个例子。

     # Generate data
     library(reshape2) # for melt
     volcano3d <- melt(volcano)
     names(volcano3d) <- c("x", "y", "z")
    
     # Basic plot
     v <- ggplot(volcano3d, aes(x, y, z = z)) +  
         stat_contour(geom="polygon", aes(fill=..level..))
    

    x 和 y 是 Long 和 Lat,z 是 d13C

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 2016-07-26
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      • 2013-04-25
      相关资源
      最近更新 更多