I want to plot this figure created with filled.contour(), but in ggplot2, how do I do this?
我想用filled.contour()来绘制这个图形,但是在ggplot2中,我该怎么做呢?
I want to use ggplot2 because the graphing conventions are easier. The reason I want to use filled.contour() is because I tried geom_tile() and image.plot() and they both created very tile like outputs, and I need an output similar to filled.contour().
我想使用ggplot2,因为图形化的约定比较容易。我想使用filled.contour()的原因是我尝试了geo_tile()和image.plot(),它们都创建了非常像tile的输出,我需要一个类似filled.contour()的输出。
This is my figure:
这是我的图:
Code:
代码:
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)))
Snippet of data:
片段的数据:
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
3 个解决方案
#1
12
You can tweak the colors as you need:
你可以根据需要调整颜色:
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()
You can reduce the pixelation at the cost of some processing time by increasing the density of the interpolation:
你可以通过增加插值的密度来减少一些处理时间的像素化:
fld <- with(df, interp(x = longitude,
y = latitude,
z = d13C,
xo = seq(min(longitude), max(longitude), length=400),
duplicate="mean"))
and also reducing the bin width:
也减少了箱子的宽度:
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()
NOTE: that is going to crunch for a noticeable few seconds on a decent desktop system. On my fairly beefy MacBook Pro it was:
注意:在一个不错的桌面系统上,这将会出现几秒钟的时间。在我相当结实的MacBook Pro上,它是:
user system elapsed
6.931 0.655 8.153
#2
2
To follow up on @hrbrmstr's minimal example, you can also have ggplot2 compute "z" for you:
为了跟踪@hrbrmstr的最小示例,您还可以为您使用ggplot2计算“z”:
library(ggplot2)
ggplot(data = faithful, aes(x = eruptions, y = waiting)) +
stat_density2d(aes(colour = ..level.., fill = ..level..), geom = "polygon")
#3
1
I took the example from the ggplot2 website.
我从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..))
Where x and y are your Long and Lat and z is d13C
x和y是你的长,Lat和z是d13C ?
#1
12
You can tweak the colors as you need:
你可以根据需要调整颜色:
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()
You can reduce the pixelation at the cost of some processing time by increasing the density of the interpolation:
你可以通过增加插值的密度来减少一些处理时间的像素化:
fld <- with(df, interp(x = longitude,
y = latitude,
z = d13C,
xo = seq(min(longitude), max(longitude), length=400),
duplicate="mean"))
and also reducing the bin width:
也减少了箱子的宽度:
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()
NOTE: that is going to crunch for a noticeable few seconds on a decent desktop system. On my fairly beefy MacBook Pro it was:
注意:在一个不错的桌面系统上,这将会出现几秒钟的时间。在我相当结实的MacBook Pro上,它是:
user system elapsed
6.931 0.655 8.153
#2
2
To follow up on @hrbrmstr's minimal example, you can also have ggplot2 compute "z" for you:
为了跟踪@hrbrmstr的最小示例,您还可以为您使用ggplot2计算“z”:
library(ggplot2)
ggplot(data = faithful, aes(x = eruptions, y = waiting)) +
stat_density2d(aes(colour = ..level.., fill = ..level..), geom = "polygon")
#3
1
I took the example from the ggplot2 website.
我从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..))
Where x and y are your Long and Lat and z is d13C
x和y是你的长,Lat和z是d13C ?