如何使一个ggplot2等值线图模拟格:filled.contour()? [英] How to make a ggplot2 contour plot analogue to lattice:filled.contour()?

查看:1315
本文介绍了如何使一个ggplot2等值线图模拟格:filled.contour()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习ggplot2,并希望将其用于我所有的R图形。然而,我还没有找到一种方法来制作类似于传统轮廓图的轮廓图,就像使用lattice:filled.contour()可以得到的一样。例如:

  #define data 
x <-seq(1,11,1)
y< ; -seq(1,11,1)
xyz.func <-function(x,y){-10.4 + 6.53 * x + 6.53 * y-0.167 * x ^ 2-0.167 * y ^ 2 + 0.0500 * x * y}

#contour使用点阵图形和R Color Brewer的图形
库(点阵)#for filled.contour()
库(RColorBrewer)#for啤酒器。 pal()
z.lattice< -outer(x,y,xyz.func)
filled.contour(x,y,z.lattice,nlevels = 6,col = brewer.pal(6, YlOrRd))

这给我一个很好的轮廓图。





<现在,让我们在ggplot2中尝试同样的事情。基于我读过的所有内容,我可以提出最好的(特别是在ggplot2 中的轮廓线的平面部分上绘制标签)是:

  #contour plot using ggplot2 
library(ggplot2)
library(reshape2)#for melt()
z.molten< -melt(z.lattice)
names(z.molten)< - c(x,y,z)
v < ))+
stat_contour(bins = 6,aes(x,y,z = z),color =black,size = 0.6)+
scale_fill_gradientn(colors = brewer.pal(6, ))
v

该图具有与filled.contour()相同的基本思想。 ,但彩色瓷砖不符合等高线。



我没有成功改变瓷砖的尺寸s

关于如何使ggplot2的输出更接近filled.contour()的输出,有什么建议?

解决方案

你的问题的本质似乎是,如何在ggplot中用离散填充轮廓生成轮廓图,而不是像你一样的连续轮廓使用传统的 geom_tile(...)方法。这是一种方法。

  x <-seq(1,11,.03)#note更精细的网格
y< ; -seq(1,11,.03)
xyz.func <-function(x,y){-10.4 + 6.53 * x + 6.53 * y-0.167 * x ^ 2-0.167 * y ^ 2 + 0.0500 * x * y}
gg < - expand.grid(x = x,y = y)
gg $ z < - with(gg,xyz.func(x,y))#需要格式为ggplot
library(ggplot2)
library(RColorBrewer)#for brewer.pal()
brks< - cut(gg $ z,breaks = seq(0,100,len = 6))
brks< - gsub(,, - ,brks,fixed = TRUE)
gg $ brks< - gsub(\\(| \\ ],,brks)#重新格式化指南标签
ggplot(gg,aes(x,y))+
geom_tile(aes(fill = brks))+
scale_fill_manual(Z ,values = brewer.pal(6,YlOrRd))+
scale_x_continuous(expand = c(0,0))+
scale_y_continuous(expand = c(0,0))+
coord_fixed()



使用例如 scale_x_continuos(...)只是为了摆脱这个问题e额外的空间 ggplot 提供轴限制;对大多数事情都很好,但是在等高线上分散注意力。使用 coord_fixed(...)只是将宽高比设置为1:1。这些是可选的。


I've been learning ggplot2, and hope to use it for all my R graphing. However, I've yet to find a way to make a contour plot that looks analogous to a conventional contour plot, like what can be obtained using lattice:filled.contour(). For example:

#define data
x<-seq(1,11,1)
y<-seq(1,11,1)
xyz.func<-function(x,y) {-10.4+6.53*x+6.53*y-0.167*x^2-0.167*y^2+0.0500*x*y}

#contour plot using lattice graphics and R Color Brewer
library(lattice) #for filled.contour()
library(RColorBrewer) #for brewer.pal()
z.lattice<-outer(x,y,xyz.func)
filled.contour(x,y,z.lattice,nlevels=6,col=brewer.pal(6,"YlOrRd"))

This gives me a nice contour plot.

Now, let's try the same thing in ggplot2. The best I can come up with, based on everything I've read (particularly Drawing labels on flat section of contour lines in ggplot2) is:

#contour plot using ggplot2
library(ggplot2)
library(reshape2) #for melt()
z.molten<-melt(z.lattice) 
names(z.molten) <- c("x", "y", "z")
v<-ggplot(z.molten, aes(x,y,z=z))+
    geom_tile(aes(fill=z))+
    stat_contour(bins=6,aes(x,y,z=z), color="black", size=0.6)+
    scale_fill_gradientn(colours=brewer.pal(6,"YlOrRd"))
v

This graph has the same basic idea as filled.contour(), but the colored tiles don't conform to the contours very well.

I haven't been successful with changing the sizes of the tiles, either.

Any suggestions on how to make ggplot2's output closer to filled.contour()'s output?

解决方案

The essence of your question, it seems, is how to produce a contour plot in ggplot with discrete filled contours, rather than continuous contours as you would get using the conventional geom_tile(...) approach. Here is one way.

x<-seq(1,11,.03)                    # note finer grid
y<-seq(1,11,.03)
xyz.func<-function(x,y) {-10.4+6.53*x+6.53*y-0.167*x^2-0.167*y^2+0.0500*x*y}
gg <- expand.grid(x=x,y=y)
gg$z <- with(gg,xyz.func(x,y))      # need long format for ggplot
library(ggplot2)
library(RColorBrewer)               #for brewer.pal()
brks <- cut(gg$z,breaks=seq(0,100,len=6))
brks <- gsub(","," - ",brks,fixed=TRUE)
gg$brks <- gsub("\\(|\\]","",brks)  # reformat guide labels
ggplot(gg,aes(x,y)) + 
  geom_tile(aes(fill=brks))+
  scale_fill_manual("Z",values=brewer.pal(6,"YlOrRd"))+
  scale_x_continuous(expand=c(0,0))+
  scale_y_continuous(expand=c(0,0))+
  coord_fixed()

The use of, e.g., scale_x_continuos(...) is just to get rid of the extra space ggplot puts around the axis limits; fine for most things but distracting in contour plots. The use of coord_fixed(...) is just to set the aspect ratio to 1:1. These are optional.

这篇关于如何使一个ggplot2等值线图模拟格:filled.contour()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆