gpt4 book ai didi

ggplot2中的R绘图填充.contour()输出

转载 作者:行者123 更新时间:2023-12-03 19:23:43 26 4
gpt4 key购买 nike

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

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

最佳答案

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

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

并且还减小了 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()

enter image description here

注意:在一个体面的桌面系统上,这将需要几秒钟的时间。在我相当强大的 MacBook Pro 上,它是:
   user  system elapsed 
6.931 0.655 8.153

关于ggplot2中的R绘图填充.contour()输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28330821/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com