本文介绍了在ggpplot2中输出R plot filled.contour()输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想绘制用filled.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)附加(df)名称(df) fld 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))) Sn ippet of data: 纬度经度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() (df,interp(x =经度,y =纬度,z = d13C,z = xo = seq(min(经度),max(经度),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 =w hite,alpha = 0.5)+ scale_fill_distiller(palette =Spectral,na.value =white)+ theme_bw() 注意:这将在体面桌面系统上引人注目的几秒钟。在我相当强大的MacBook Pro上: 用户系统已用完 6.931 0.655 8.153 I want to plot this figure created with filled.contour(), but in ggplot2, how do I do this? 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(). 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.519 -65.7 -19.919 -65.5 -18.518 -60.7 -2018 -58.5 -18.218 -57.8 -1917 -55.4 -18.617 -50.8 -1817 -47.1 -18.317 -45.5 -19.416 -43.3 -17.915 -40.7 -18.514 -39.3 -19.912 -36.7 -19.912 -36.2 -19.911 -34.4 -19.210 -32 -18.59 -30.3 -19.38 -29.2 -19.47 -26.6 -18.27 -25.5 -19.36 23.9 -203 -21.3 -20.4 解决方案 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: user system elapsed 6.931 0.655 8.153 这篇关于在ggpplot2中输出R plot filled.contour()输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 09:02