如何充分使用stat_contour填充轮廓 [英] How to fill in the contour fully using stat_contour
问题描述
我正在寻找方法来完全填充由ggplot2的stat_contour生成的轮廓。目前的结果是这样的:
$ p $ #生成数据
库(ggplot2)
库(reshape2) #(用于熔化)
volcano3d< - 熔化(火山)
名称(volcano3d)< -c(x,y,z)
v< - ggplot(volcano3d,aes(x,y,z = z))
v + stat_contour(geom =polygon,aes(fill = .. level ..))
所需的结果可以通过手动修改代码产生,如下所示。
v + stat_contour(geom =polygon,aes(fill = .. level ..))+
theme(panel.grid = element_blank())+#删除网格线
scale_x_continuous(限制= c(min(volcano3d $ x),max(volcano3d $ x)),expand = c(0,0))+#set x limits
scale_y_continuous(limits = c(min(volcano3d $ y),max (volcano3d $ y)),expand = c(0,0))+#set y limits
theme(panel.background = element_rect(fill =#132B43))#color background
我的问题是:有没有办法在不手动指定颜色或使用 geom_tile()
?
由于@tonytonov建议此线程,可以通过关闭多边形来删除透明区域。
$ b $ $ p $ #check x and y grid
minValue< -sapply(volcano3d,min)
maxValue< - sapply(volcano3d,max)
arbitaryValue = min(volcano3d $ z-10)
test1< -data.frame(x = minValue [1] -1,y = minValue [2] :maxValue [2],z = arbitaryValue)
test2< -data.frame(x = minValue [1]:maxValue [1],y = minValue [2] -1,z = arbitaryValue)
test3< -data.frame(x = maxValue [1] +1,y = minValue [2]:maxValue [2],z = arbitaryValue)
test4< -data.frame(x = minValue [1]: maxValue [1],y = maxValue [2] +1,z = arbitaryValue)
test< -rbind(test1,test2,test3,test4)
vol< -rbind(volcano3d,测试)
w< - ggplot(vol,aes(x,y,z = z))
w + stat_contour(geom =polygon,aes(fill = .. level .. ))#better
#在尝试摆脱不需要的空间时不起作用
w + stat_contour(geom =polygon,aes(fill = .. level ..))+
scale_x_continuous(limits = c(min(volcano3d $ x),max(volcano3d $ x)),expand = c(0,0))+#set x limits
scale_y_continuous(limits = c(min (volcano3d $ Y) max(volcano3d $ y)),expand = c(0,0))#set y limits
#在这里工作!
w + stat_contour(geom =polygon,aes(fill = .. level ..))+
coord_cartesian(xlim = c(min(volcano3d $ x),max(volcano3d $ x)),
ylim = c(min(volcano3d $ y),max(volcano3d $ y)))
问题仍然存在通过这种调整可以找出除了反复试验以外的方法来确定 arbitaryValue
。
[edit from here]
只是一个快速更新来显示我如何确定<$ c $而不必猜测每个数据集。
BINS< -50
BINWIDTH< ;-( diff(range(volcano3d $ z))/ BINS)#引用来自ggplot2代码
arbitaryValue = min(volcano3d $ z)-BINWIDTH * 1.5
这对于我现在正在处理的数据集似乎很有效。不确定是否适用于其他人。另外,请注意,我在这里设置BINS值的事实要求我必须在 stat_contour
中使用 bins = BINS
。
I am looking for ways to fully fill in the contour generated by ggplot2's stat_contour. The current result is like this:
# Generate data
library(ggplot2)
library(reshape2) # for melt
volcano3d <- melt(volcano)
names(volcano3d) <- c("x", "y", "z")
v <- ggplot(volcano3d, aes(x, y, z = z))
v + stat_contour(geom="polygon", aes(fill=..level..))
The desired result can be produced by manually modifying the codes as follows.
v + stat_contour(geom="polygon", aes(fill=..level..)) +
theme(panel.grid=element_blank())+ # delete grid lines
scale_x_continuous(limits=c(min(volcano3d$x),max(volcano3d$x)), expand=c(0,0))+ # set x limits
scale_y_continuous(limits=c(min(volcano3d$y),max(volcano3d$y)), expand=c(0,0))+ # set y limits
theme(panel.background=element_rect(fill="#132B43")) # color background
My question: is there a way to fully fill the plot without manually specifying the color or using geom_tile()
?
As @tonytonov has suggested this thread, the transparent areas can be deleted by closing the polygons.
# check x and y grid
minValue<-sapply(volcano3d,min)
maxValue<-sapply(volcano3d,max)
arbitaryValue=min(volcano3d$z-10)
test1<-data.frame(x=minValue[1]-1,y=minValue[2]:maxValue[2],z=arbitaryValue)
test2<-data.frame(x=minValue[1]:maxValue[1],y=minValue[2]-1,z=arbitaryValue)
test3<-data.frame(x=maxValue[1]+1,y=minValue[2]:maxValue[2],z=arbitaryValue)
test4<-data.frame(x=minValue[1]:maxValue[1],y=maxValue[2]+1,z=arbitaryValue)
test<-rbind(test1,test2,test3,test4)
vol<-rbind(volcano3d,test)
w <- ggplot(vol, aes(x, y, z = z))
w + stat_contour(geom="polygon", aes(fill=..level..)) # better
# Doesn't work when trying to get rid of unwanted space
w + stat_contour(geom="polygon", aes(fill=..level..))+
scale_x_continuous(limits=c(min(volcano3d$x),max(volcano3d$x)), expand=c(0,0))+ # set x limits
scale_y_continuous(limits=c(min(volcano3d$y),max(volcano3d$y)), expand=c(0,0)) # set y limits
# work here!
w + stat_contour(geom="polygon", aes(fill=..level..))+
coord_cartesian(xlim=c(min(volcano3d$x),max(volcano3d$x)),
ylim=c(min(volcano3d$y),max(volcano3d$y)))
The problem remained with this tweak is finding methods aside from trial and error to determine the arbitaryValue
.
[edit from here]
Just a quick update to show how I am determining the arbitaryValue
without having to guess for every datasets.
BINS<-50
BINWIDTH<-(diff(range(volcano3d$z))/BINS) # reference from ggplot2 code
arbitaryValue=min(volcano3d$z)-BINWIDTH*1.5
This seems to work well for the dataset I am working on now. Not sure if applicable with others. Also, note that the fact that I set BINS value here requires that I will have to use bins=BINS
in stat_contour
.
这篇关于如何充分使用stat_contour填充轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!