繁体   English   中英

R plot在ggpplot2中输出filled.contour()输出

[英]R plot filled.contour() output in ggpplot2

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

我想使用ggplot2,因为图形约定更容易。 我想使用filled.contour()的原因是因为我尝试了geom_tile()和image.plot()并且它们都创建了类似于输出的平铺,我需要一个类似于filled.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

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

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

并减少箱宽:

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 

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

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

我从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是你的Long和Lat,z是d13C

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM