使用ggplot2绘制R plot filled.contour()的输出结果

9

我想绘制使用filled.contour()创建的这个图形,但在ggplot2中,我该如何做到这一点?

我想使用ggplot2,因为它的图表约定更容易理解。我想使用filled.contour()的原因是,我尝试了geom_tile()和image.plot(),它们都创建了非常类似瓷砖的输出,而我需要一个类似于filled.contour()的输出。

这是我的图像:

enter image description here

代码:

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

你的数据片段中包含重复项,会导致interp出现问题。另外,为什么要附加df? - hrbrmstr
我可能可以想办法去除重复项,但是关于附加 - 只是因为有人告诉我这样做。那么我不应该这样做吗? - Roseanna
你找到了用ggplot模仿filled.contour的方法吗? - Tsingis
3个回答

14

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

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()

enter image description here

通过增加插值的密度,可以减少像素化,但这样会增加一些处理时间。

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

并且减小箱宽度:

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,并且再次使用了您的代码,但它太像素化了。有什么办法可以使它不那么像素化吗?干杯 - Roseanna
嗨@hbrmstr,非常好的答案,但我有另一个问题。是否可以在插值字段上添加geom_path?我无法成功。 - pacomet

3

针对@hrbrmstr的最简示例,您也可以让ggplot2为您计算“z”:

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

2

我从ggplot2官网获取了这个例子。

 # 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是您的经度和纬度,z是d13C。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接